示例#1
0
    def from_file(path, query, duration=None):
        media_type = guess_type(path)

        if media_type is MEDIA_TYPE_VIDEO:
            if duration is None: duration = get_video_duration(path)
            tmp = create_temporary_file(".snap.mp4")
            output_path = tmp.name
            subprocess.Popen(["ffmpeg", "-y", "-i", path, output_path]).wait()

        elif media_type is MEDIA_TYPE_IMAGE:
            image = Image.open(path)

            draw = ImageDraw.Draw(image)
            font = ImageFont.truetype("aller-font.ttf", 28)
            draw.text((10, 10),
                      'Name:' + query['title'] + ' Artist:' + query['artist'],
                      (255, 0, 0),
                      font=font)
            del draw

            tmp = create_temporary_file(".jpg")
            output_path = tmp.name
            resize_image(image, output_path)
            if not duration:
                duration = DEFAULT_DURATION

        else:
            raise UnknownMediaType(
                "Could not determine media type of the file")

        return Snap(path=output_path, media_type=media_type, duration=duration)
示例#2
0
    def __init__(self, **opts):
        self.uploaded = False
        self.duration = opts['duration']
        self.media_type = opts['media_type']

        if 'sender' in opts:
            self.sender = opts['sender']
            self.snap_id = opts['snap_id']
            self.from_me = False

        else:
            self.snap_id = uuid.uuid4().hex
            self.from_me = True

        if 'data' in opts:
            self.media_type = opts['media_type']

            suffix = "." + file_extension_for_type(opts['media_type'])

            self.file = create_temporary_file(suffix)

            if self.media_type is MEDIA_TYPE_VIDEO:
                self.file.write(opts['data'])
                self.file.flush()

            else:
                image = Image.open(StringIO(opts['data']))
                resize_image(image, self.file.name)

        else:
            path = opts['path']
            self.file = open(path)
示例#3
0
    def from_file(path, duration=None):
        media_type = guess_type(path)

        if media_type is MEDIA_TYPE_VIDEO:
            if duration is None: duration = get_video_duration(path)
            tmp = create_temporary_file(".snap.mp4")
            output_path = tmp.name
            subprocess.Popen(["ffmpeg", "-y", "-i", path, output_path]).wait()

        elif media_type is MEDIA_TYPE_IMAGE:
            image = Image.open(path)
            tmp = create_temporary_file(".jpg")
            output_path = tmp.name
            resize_image(image, output_path)
            if duration is None: duration = DEFAULT_DURATION

        else:
            raise Exception, "Could not determine media type of the file"

        return Snap(path=output_path, media_type=media_type, duration=duration)
示例#4
0
    def from_file(path, duration = None):
        media_type = guess_type(path)

        if media_type is MEDIA_TYPE_VIDEO:
            if duration is None: duration = get_video_duration(path)
            tmp = create_temporary_file(".snap.mp4")
            output_path = tmp.name
            subprocess.Popen(["ffmpeg", "-y", "-i", path, output_path]).wait()

        elif media_type is MEDIA_TYPE_IMAGE:
            image = Image.open(path)
            tmp = create_temporary_file(".jpg")
            output_path = tmp.name
            resize_image(image, output_path)
            if duration is None: duration = DEFAULT_DURATION

        else:
            raise Exception, "Could not determine media type of the file"

        return Snap(path = output_path, media_type = media_type, duration = duration)
示例#5
0
def load_data_from_url(url, language='en'):
    # type: (Text, Optional[Text]) -> TrainingData
    """Load training data from a URL."""

    if not utils.is_url(url):
        raise requests.exceptions.InvalidURL(url)
    try:
        response = requests.get(url)
        response.raise_for_status()
        temp_data_file = utils.create_temporary_file(response.content)
        return _load(temp_data_file, language)
    except Exception as e:
        logger.warning("Could not retrieve training data "
                       "from URL:\n{}".format(e))
示例#6
0
文件: snap.py 项目: rahimpabna/sn
    def __init__(self, **opts):
        self.uploaded = False
        self.duration = opts['duration']
        self.media_type = opts['media_type']

        if opts.get("is_story", False):
            self.story_id = opts['snap_id']

        if 'sender' in opts:
            self.sender = opts['sender']
            self.snap_id = opts['snap_id']
            self.from_me = False

        else:
            self.snap_id = uuid.uuid4().hex
            self.from_me = True

        if 'data' in opts:
            data = opts['data']

            if data[0:2] == 'PK':
                video_filename, _ = extract_zip_components(data)
                self.file = open(video_filename, 'rb+')

            else:
                suffix = file_extension_for_type(opts['media_type'])
                self.file = create_temporary_file(suffix)

            if self.media_type is MEDIA_TYPE_VIDEO or self.media_type is MEDIA_TYPE_VIDEO_WITHOUT_AUDIO:
                self.file.write(data)
                self.file.flush()

            else:
                image = Image.open(StringIO(data))
                resize_image(image, self.file.name)

        else:
            path = opts['path']
            self.file = open(path)
示例#7
0
    def __init__(self, **opts):
        self.uploaded = False
        self.duration = opts['duration']
        self.media_type = opts['media_type']

        if 'sender' in opts:
            self.sender = opts['sender']
            self.snap_id = opts['snap_id']
            self.from_me = False

        else:
            self.snap_id = uuid.uuid4().hex
            self.from_me = True

        if 'data' in opts:
            self.media_type = opts['media_type']

            suffix = "." + get_file_extension(opts['media_type'])

            self.file = create_temporary_file(suffix)

            if (self.media_type is MEDIA_TYPE_VIDEO) or (
                    self.media_type is MEDIA_TYPE_VIDEO_NOAUDIO) or (
                        opts['data'][0:2] == b'\x00\x00'):
                self.file.write(opts['data'])
                self.file.flush()

            else:
                print self.media_type  #, opts
                try:
                    image = Image.open(StringIO(opts['data']))
                    resize_image(image, self.file.name)
                except Exception as e:
                    print "Unable to process image " + str(e)

        else:
            path = opts['path']
            self.file = open(path)
示例#8
0
    def __init__(self, **opts):
        self.uploaded = False
        self.duration = opts['duration']
        self.media_type = opts['media_type']

        if 'sender' in opts:
            self.sender = opts['sender']
            self.snap_id = opts['snap_id']
            self.from_me = False

        else:
            self.snap_id = uuid.uuid4().hex
            self.from_me = True

        if 'data' in opts:
            self.media_type = opts['media_type']

            suffix = "." + get_file_extension(opts['media_type'])

            self.file = create_temporary_file(suffix)

            if (self.media_type is MEDIA_TYPE_VIDEO) or (self.media_type is MEDIA_TYPE_VIDEO_NOAUDIO) or (opts['data'][0:2] == b'\x00\x00'):
                self.file.write(opts['data'])
                self.file.flush()

            else:
                print self.media_type#, opts
                try:
                    image = Image.open(StringIO(opts['data']))
                    resize_image(image, self.file.name)
                except Exception as e:
                    print "Unable to process image "+str(e)

        else:
            path = opts['path']
            self.file = open(path)
示例#9
0
 def from_image(img, duration=DEFAULT_DURATION):
     f = create_temporary_file(".jpg")
     resize_image(img, f.name)
     return Snap(path=f.name,
                 media_type=MEDIA_TYPE_IMAGE,
                 duration=duration)