Пример #1
0
    def getRandomFrame(self, pathList, info=False, frameMode='L'):
        """
        returns the first frame from a random video of the list

        Args:
            pathList (List of Strings):
                                paths to video files
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:

                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???

        Returns:
            frame (ndarray):
                                decoded video frame
        """
        file = pathList[random.randint(0, len(pathList) - 1)]
        frameNo = random.randint(0, 1600 - 1)

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo, file)

        self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)
        self.frameMode = frameMode

        frame = self.vs.next().ndarray()

        if info:
            return [frame, file]
        else:
            return frame
Пример #2
0
    def setVideoStream(self, file, info=False, frameMode='L'):
        """
        returns the first frame from a random video of the list
        
        Args:
            pathList (List of Strings):
                                paths to video files
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:
                                
                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???
            
        Returns:
            frame (ndarray):
                                decoded video frame
        """
        frameNo = random.randint(0, 1600 - 1)

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo, file)

        if not self.frame is None:
            del self.frame

        if not self.vs is None:
            del self.vs

        self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)
        self.frameMode = frameMode
Пример #3
0
    def create(file):
        video_name = file.filename
        ext = video_name.rsplit('.', 1)[1].lower()
        if '.' in video_name and ext in VIDEO_EXTS:
            gene_name = str(uuid.uuid4())
            new_video_name = gene_name + '.' + ext
            address = os.path.join('video', new_video_name)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], address))

            screenshot_name = gene_name + '.jpeg'
            screenshot_address = os.path.join('video', 'screenshot',
                                              screenshot_name)
            screenshot = VideoStream(
                os.path.join(app.config['UPLOAD_FOLDER'],
                             address)).get_frame_at_sec(2).image()
            screenshot.save(
                os.path.join(app.config['UPLOAD_FOLDER'], screenshot_address))

            db = mysql.get_db()
            cursor = db.cursor()
            cursor.execute(
                "insert into video (name, address, screenshot) "
                "values (%s, %s, %s)",
                (video_name, address, screenshot_address))
            db.commit()
            if cursor.rowcount != 1:
                return False, 'insert into db failed'

            return True, cursor.lastrowid
        else:
            return False, 'invalid extension'
Пример #4
0
def test_videoinfo():
    from ffvideo import VideoStream

    vs = VideoStream(v0)
    assert vs.duration == 15.987
    assert vs.width == vs.frame_width
    assert vs.height == vs.frame_height
    assert vs.width == 320
    assert vs.height == 240
    assert vs.codec_name == 'flv'
    assert vs.frame_mode == 'RGB'

    vs = VideoStream(v0, frame_size=(128, 128), frame_mode='L')
    assert vs.width != vs.frame_width
    assert vs.frame_width == 128
    assert vs.frame_height == 128
    assert vs.frame_mode == 'L'

    vs = VideoStream(v0, frame_size=(129, None), frame_mode='L')
    #    raise AssertionError, "(%d, %d)" % (vs.frame_width, vs.frame_height)
    assert vs.width != vs.frame_width
    assert vs.height != vs.frame_height

    assert vs.frame_width == 130
    assert vs.frame_height == 96
Пример #5
0
def verify_file(orig_filename, filename):
    name, ext = os.path.splitext(orig_filename)
    ext = ext.lower()
    if ext in ('.jpg', '.jpeg'):
        im = Image.open(filename)
        w, h = im.size
        if w > 1920 or h > 1080:
            raise AssetError("image too big: %dx%d" % (w, h))
        if w < 128 or h < 128:
            raise AssetError("image too small: %dx%d" % (w, h))
        if im.mode != 'RGB':
            raise AssetError("invalid image mode: %s" % im.mode)
        return Asset.TYPE_IMAGE, make_thumb(im)
    elif ext in ('.mp4', ):
        vs = VideoStream(filename)
        if vs.codec_name != 'h264':
            raise AssetError("invalid codec %s" % (vs.codec_name, ))
        w, h = vs.frame_width, vs.frame_height
        duration = vs.duration
        log.info("video info: %dx%d (%fs)" % (w, h, duration))
        if w > 1920 or h > 1080:
            raise AssetError("video too big: %dx%d" % (w, h))
        if w < 128 or h < 128:
            raise AssetError("video too small: %dx%d" % (w, h))
        if duration < 1:
            raise AssetError("video too short: %fs" % (duration, ))
        if duration > 10.5:
            raise AssetError("video too long: %fs" % (duration, ))
        frame = vs.get_frame_no(3)
        im = frame.image()
        return Asset.TYPE_VIDEO, make_thumb(im)
    else:
        raise AssetError("invalid asset file extension")
Пример #6
0
def get_frame(filename, sec=3):
    basename = os.path.basename(filename)
    sans_ext = basename.split('.')[0]
    vs = VideoStream(filename)
    frame = vs.get_frame_at_sec(sec)
    frame_location = os.path.join(config.FRAMES_DIRECTORY, sans_ext + '.png')
    frame.image().save(frame_location)
    return frame_location
Пример #7
0
    def __init__(self, input_video_path, output_path, frame_step, resize_factor=2):
        self.input_video_path = input_video_path
        self.output_path = output_path
        self.frame_step = frame_step
        self.resize_factor = resize_factor

        self.vs = VideoStream(input_video_path)
        # self.duration = self.vs.duration
        self.total_frames = int(self.vs.duration * self.vs.framerate)
Пример #8
0
def create_thumbmail(filename, imagename):
    vs = VideoStream(filename,
                     # frame_size=(128, None),  # scale to width 128px
                     # frame_mode='L' # convert to grayscale
                     )
    image_name = imagename.split('.')[0]
    frame = vs.get_frame_at_sec(10)
    img = frame.image()
    img = img.resize((300,300))
    img.save('{0}.jpeg'.format(image_name))
Пример #9
0
 def generatePreviewFromVideo(videoFile):
     if ModuleTools.INSTALLED_FFVIDEO():
         from ffvideo import VideoStream
         fd, path = tempfile.mkstemp('.jpg')
         stream = VideoStream(videoFile)
         stream.get_frame_at_sec(0).image().save(path)
         preview = ImageTools.generatePreviewFromImage(path)
         os.remove(path)
         return preview
     else:
         logger.warn("Python ffvideo library not installed")
Пример #10
0
	def generatePreviewFromVideo(videoFile):
		if ModuleTools.INSTALLED_FFVIDEO():
			from ffvideo import VideoStream
			fd, path = tempfile.mkstemp('.jpg')
			stream = VideoStream(videoFile)
			stream.get_frame_at_sec(0).image().save(path)
			preview = ImageTools.generatePreviewFromImage(path)
			os.remove(path)
			return preview		
		else:
			logger.warn("Python ffvideo library not installed")
Пример #11
0
    def thumbnail(self):
        filename = STATIC_DIR + self.hash + '.png'
        try:
            open(filename)
        except:
            logger.debug("Generating new thumbnail.")

            pil_image = VideoStream(self.full_path)\
                .get_frame_at_sec(60).image()
            pil_image.save(filename)

        return filename
Пример #12
0
def get_barcode_image(video_path, barcode_path):
    if os.path.exists(barcode_path):
        return Image.open(barcode_path)
    vs = VideoStream(video_path)
    barcode = Image.new('RGB', BARCODE_SIZE)
    for i, timepoint in enumerate(np.linspace(0, vs.duration, BARCODE_SIZE[0])):
        try:
            frame = vs.get_frame_at_sec(timepoint)
            frame_image = frame.image()
            frame_image = frame_image.resize((1, BARCODE_SIZE[1]), Image.ANTIALIAS)
            barcode.paste(frame_image, (i, 0))
        except Exception, e:
            print e
        finally:
Пример #13
0
def set_duration_video(sender, instance, **kwargs):
    if instance.file_video:
        video_stream = VideoStream(instance.file_video.path_full)
        seconds = int(video_stream.duration)
        instance.prolongation = datetime.time(hour=seconds / 3600,
                                              minute=seconds / 60,
                                              second=seconds % 60)
Пример #14
0
 def getVideoProperties(videoFile):
     if ModuleTools.INSTALLED_FFVIDEO():
         from ffvideo import VideoStream
         s = VideoStream(videoFile)
         return s.width, s.height, s.bitrate, s.duration  #, s.codec_name
     else:
         logger.warn("Python ffvideo library not installed")
Пример #15
0
    def setVideoStream(self, file,  info=False, frameMode='L'):
        """
        returns the first frame from a random video of the list
        
        Args:
            pathList (List of Strings):
                                paths to video files
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:
                                
                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???
            
        Returns:
            frame (ndarray):
                                decoded video frame
        """       
        frameNo = random.randint(0,  1600 - 1)
        
        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo,  file)

        if not self.frame is None:
            del self.frame

        if not self.vs is None:
            del self.vs


        self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)
        self.frameMode = frameMode
Пример #16
0
    def getRandomFrame(self, pathList,  info=False, frameMode='L'):
        """
        returns the first frame from a random video of the list

        Args:
            pathList (List of Strings):
                                paths to video files
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:

                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???

        Returns:
            frame (ndarray):
                                decoded video frame
        """
        file = pathList[random.randint(0,  len(pathList) - 1)]
        frameNo = random.randint(0,  1600 - 1)

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo,  file)

        self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)
        self.frameMode = frameMode

        frame = self.vs.next().ndarray()

        if info:
            return [frame, file]
        else:
            return frame
Пример #17
0
def video_upload():
    path = '/usr/local/nginx/Videos/vod'
    category = request.forms.get('category')
    upload = request.files.get('upload')

    upload.save(path)  # appends upload.filename automatically
    #create thumbnail
    thumbnail = VideoStream(path + '/' +
                            str(upload.filename)).get_frame_at_sec(3).image()
    thumbnail.save('./index/static/images/thumbnails/' +
                   str(upload.filename).replace('.flv', '') + '.jpeg')
    #run transcode module
    subprocess.call('cd ' + path + ' && ' + './transcode.sh ' +
                    str(upload.filename) + ' ' +
                    str(upload.filename).replace('.flv', ''),
                    shell=True)
    redirect("/home")
Пример #18
0
def search(dirname):
    try:
        filenames = os.listdir(dirname)
        for filename in filenames:
            if dirname[-1] == "/":
                pass
            else:
                dirname = dirname + "/"
            full_path = os.path.join(dirname, filename)
            if os.path.isdir(full_path):
                search(full_path)
            else:
                ext = os.path.splitext(full_path)[-1]
                thum = "%s_" % (datetime.today().strftime('%Y%m%d')) + str(
                    uuid.uuid4())[:8] + ".jpg"
                small_thum_path = "/DATA/reference_library/thumnail/small_thum/%s" % thum

                if ext.lower() == ".mp4" or ext.lower() == ".mov" or ext.lower(
                ) == ".mkv" or ext.lower() == ".avi":

                    try:
                        thumbnail = VideoStream(full_path).get_frame_at_sec(
                            1).image()
                        thumbnail.save(small_thum_path)

                        full_path = full_path[5:]

                        select_query = "select * from img where full_path = '%s'" % full_path
                        cur.execute("set names utf8")
                        cur.execute(select_query)
                        if cur.rowcount == 0:
                            query = "insert into img(img_path,img_name,full_path,thumnail,type) values('%s','%s','%s','%s','%s')" % (
                                dirname, filename, full_path, thum, 'video')
                            cur.execute("set names utf8")
                            cur.execute(query)
                            db.commit()
                        else:
                            pass

                    except Exception, e:
                        print e

                else:
                    pass
Пример #19
0
    def setMovie(self, fileName):
        """Open a video file.
        
        :param filename: a string containing the name of the file to be opened.
            
        :raise: TypeError: The fileName is not a string.
        """
        if (isinstance(fileName, basestring)):
            self.fileName = u''.join(fileName).encode('utf-8')
            self.source = VideoStream(self.fileName)
        else:
            raise TypeError('fileName must be a string')

        self.frameRate = self.source.framerate
        self.frameNumber = self.source.duration * 1000 / self.source.framerate
        self.timer = QtCore.QTimer()
        self.timer.setInterval(1000.0 / self.frameRate)
        self.timer.setSingleShot(False)
        self.timer.timeout.connect(self.frameMustChange)
Пример #20
0
    def update_thumbnail(self, commit=False):
        """Generates the thumbnail for video and image resources

		:param commit: States whether to perform self.save() at the end of this method
		:type commit: bool
		"""
        if not self.mime_type:
            self.update_mimetype()

        if self.mime_type.startswith("video"):
            # FIXME: There's a problem with seeking in ogg files
            s = VideoStream(self.file.path)
            i = s.get_frame_at_sec(1).image()
            self._set_thumbnail(i)
        elif self.mime_type.startswith("image"):
            i = Image.open(self.file.path)
            self._set_thumbnail(i)
            # TODO/FEATUE: possibly create a soundwave thumbnail out of the audio files

        if commit:
            self.save()
Пример #21
0
    def update_thumbnail(self, commit=False):
        """Generates the thumbnail for video and image resources

		:param commit: States whether to perform self.save() at the end of this method
		:type commit: bool
		"""
        if not self.mime_type:
            self.update_mimetype()

        if self.mime_type.startswith('video'):
            # FIXME: There's a problem with seeking in ogg files
            s = VideoStream(self.file.path)
            i = s.get_frame_at_sec(1).image()
            self._set_thumbnail(i)
        elif self.mime_type.startswith('image'):
            i = Image.open(self.file.path)
            self._set_thumbnail(i)
        # TODO/FEATUE: possibly create a soundwave thumbnail out of the audio files

        if commit:
            self.save()
Пример #22
0
    def getFrame(self, file, frameNo=0, info=False, frameMode='L'):
        """
        returns the given frame from a given video

        Args:
            file (String):
                                path to video file
            frameNo (int):
                                frame number to be returned
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:

                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???


        Returns:
            frame (ndarray):
                                decoded video frame
        """

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo, file)

        if not self.vs \
        or self.frameMode != frameMode \
        or self.vs.filename != file:
            self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)

            self.frameMode = frameMode

        frame = self.vs.get_frame_no(frameNo).ndarray()

        if info:
            return [frame, file]
        else:
            return frame
Пример #23
0
def convert():
    from videolearn.models import Vlesson
    from subprocess import call
    import subprocess
    from ffvideo import VideoStream
    from django.core.files import File
    from co.settings import BASE_DIR
    for v in Vlesson.objects.filter(is_converted=False):
        ex = 'avconv -i %s -ar 22050 -crf 28 %s.flv' % (v.video.path,
                                                        v.video.path)
        print ex
        return_code = call(ex, shell=True)
        print return_code
        if (return_code == 1):
            p = subprocess.Popen(ex,
                                 shell=True,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.STDOUT)
            v.is_error = True
            v.error_desc = str(p.stdout.read())
            v.save()
        else:
            v.is_converted = True

            with open(v.video.path):
                vs = VideoStream(v.video.path + '.flv', frame_size=(800, None))
                token = 'screen_%s' % v.id
                thumb = BASE_DIR + '/media/vcourse_images/' + token + '.jpg'
                print 'Time:::: %s' % vs.duration
                #duration = int(vs.duration)
                duration = 2
                frame = vs.get_frame_at_sec(duration)
                frame.image().save(thumb)
                f = File(open(thumb))
                v.image.save(thumb, f)
            dur = int(vs.duration / 60)
            durs = ' %s sec (%s min)' % (vs.duration, str(dur))
            v.duration = durs
            v.save()
        ''''  
Пример #24
0
def search(dirname):
    try:
        filenames = os.listdir(dirname)
        for filename in filenames:
            if dirname[-1] == "/":
                pass
            else:
                dirname = dirname + "/"
            full_path = os.path.join(dirname, filename)
            if os.path.isdir(full_path):
                search(full_path)
            else:

                ext = os.path.splitext(full_path)[-1]
                thum = "%s_" % (datetime.today().strftime('%Y%m%d')) + str(
                    uuid.uuid4())[:8] + ".jpg"
                small_thum_path = "/DATA/reference_library/thumnail/small_thum/%s" % thum

                if ext.lower() == ".mp4" or ext.lower() == ".mov" or ext.lower(
                ) == ".mkv" or ext.lower() == ".avi" or ext.lower() == ".webm":

                    try:
                        duration = int(VideoStream(full_path).duration / 2)
                        thumbnail = VideoStream(full_path).get_frame_at_sec(
                            duration).image()
                        thumbnail.save(small_thum_path)
                    except Exception, e:
                        print e

                    select_query = 'select * from img where full_path = "%s"' % full_path

                    cur.execute("set names utf8")
                    cur.execute(select_query)

                    if cur.rowcount == 0:
                        try:
                            codec = VideoStream(full_path).codec_name
                            if codec == "h264" or codec == "aac":
                                full_path = full_path[5:]
                                query_o = 'insert into img(img_path,img_name,full_path,thumnail,type) values("%s","%s","%s","%s","%s")' % (
                                    dirname, filename, full_path, thum,
                                    'video')
                                cur.execute("set names utf8")
                                cur.execute(query_o)
                                db.commit()

                            else:
                                full_path = full_path[5:]
                                query = 'insert into img(img_path,img_name,full_path,thumnail,type) values("%s","%s","%s","%s","%s")' % (
                                    dirname, filename, full_path, 'codec.png',
                                    'video')
                                cur.execute("set names utf8")
                                cur.execute(query)
                                db.commit()

                        except Exception, e:
                            pass
                    else:
                        pass
                else:
Пример #25
0
def new_video(title, file_name, uploader_ip, file_hash, description):
	#this methods assumes the video is in the temp folder
	try:

		temp_file = os.path.join(tempfile.gettempdir(), file_name)

		vs = VideoStream(temp_file)
		thumb = vs.get_frame_at_sec(int(vs.duration/2)).image()
		thumb.save(os.path.join(app.config["thumbs_dir"], file_name) + '.jpg')

		def_file = os.path.join(app.config["upload_dir"] + file_name)

		shutil.move(temp_file, def_file)

		video = Video(title, file_name, file_hash, uploader_ip, description)

		db.session.add(video)
		db.session.commit()

	except Exception, e:
		logging.error(e)
		abort(500)
Пример #26
0
def make_thumb(user):
    from ffvideo import VideoStream
    from co.settings import FLV_PATH
    from co.settings import BASE_DIR
    from shutil import copyfile
    from django.core.files import File
    token = 'my_%s' % user.id
    filename = FLV_PATH+'/'+token+'.flv'
    print filename
    #filenametmp = MEDIA_ROOT+'/video/'+token+'_tmp.flv'
    thumb = BASE_DIR+'/media/profile_images/'+token+'.jpg'
    #try:
    with open(filename):
        print ('converting:'+filename+' to '+thumb)
        #copyfile(filename,filenametmp)
        vs = VideoStream(filename,frame_size=(800, None))
        duration = int(vs.duration)
        duration = 1
        print('get frame number'+str(duration))
        #try:
        frame = vs.get_frame_at_sec(duration)
        frame.image().save(thumb)
        f = File(open(thumb))
        user.image.save(thumb,f)
Пример #27
0
def test_open():
    from ffvideo import VideoStream, DecoderError, FAST_BILINEAR, BILINEAR
    try:
        VideoStream('non-existing-file')
    except DecoderError:
        pass
    else:
        raise AssertionError, "expected IOError"

    try:
        VideoStream('tests/test_ffvideo.py')
    except DecoderError:
        pass
    else:
        raise AssertionError, "expected DecodeError"

    VideoStream(v0)
    VideoStream(v0, frame_size=(128, 128), frame_mode='L')
    VideoStream(v0, frame_size=(None, 128), frame_mode='L')
    VideoStream(v0,
                frame_size=(None, 128),
                frame_mode='L',
                scale_mode=BILINEAR)
    VideoStream(v0, scale_mode=FAST_BILINEAR, frame_size=(None, 128))

    try:
        VideoStream(v0, frame_size=(None, 128, 33), frame_mode='L')
    except ValueError:
        pass
    else:
        raise AssertionErrror, "expected ValueError"

    try:
        VideoStream(v0, frame_size=344, frame_mode='L')
    except ValueError:
        pass
    else:
        raise AssertionErrror, "expected ValueError"
Пример #28
0
 def setMovie(self, fileName):
     """Open a video file.
     
     :param filename: a string containing the name of the file to be opened.
         
     :raise: TypeError: The fileName is not a string.
     """
     if(isinstance(fileName, basestring)):
         self.fileName =  u''.join(fileName).encode('utf-8')
         self.source = VideoStream(self.fileName)
     else: 
         raise TypeError('fileName must be a string')
     
     self.frameRate = self.source.framerate
     self.frameNumber = self.source.duration*1000/self.source.framerate
     self.timer = QtCore.QTimer()
     self.timer.setInterval(1000.0/self.frameRate)
     self.timer.setSingleShot(False)
     self.timer.timeout.connect(self.frameMustChange)
Пример #29
0
def test_frames_getting():
    from ffvideo import VideoStream

    vs = VideoStream(v0)

    f1 = vs.current()  # first frame
    f2 = vs.next()
    assert f2.timestamp > f1.timestamp

    f = vs[0]  # first frame
    assert f.frameno == 0
    f = vs.get_frame_no(100)
    #    f = vs[100]
    #    assert f.frameno == 100

    f = vs.get_frame_no(100)
    f = vs.get_frame_at_sec(1)
    assert f.timestamp - 1 < 0.1
    f = vs.get_frame_at_pts(133000)

    assert f.width == vs.frame_width
    assert f.height == vs.frame_height
    assert f.mode == vs.frame_mode
Пример #30
0
    def getFrame(self, file, frameNo=0, info=False, frameMode='L'):
        """
        returns the given frame from a given video

        Args:
            file (String):
                                path to video file
            frameNo (int):
                                frame number to be returned
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:

                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???


        Returns:
            frame (ndarray):
                                decoded video frame
        """

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo,  file)

        if not self.vs \
        or self.frameMode != frameMode \
        or self.vs.filename != file:
            self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)

            self.frameMode = frameMode
        
        frame = self.vs.get_frame_no(frameNo).ndarray()
        
        if info:
            return [frame, file]
        else:
            return frame
Пример #31
0
 def getVideoProperties(videoFile):
     if ModuleTools.INSTALLED_FFVIDEO():
         from ffvideo import VideoStream
         s = VideoStream(videoFile)
         return s.width, s.height, s.bitrate, s.duration  #, s.codec_name
     elif ModuleTools.INSTALLED_EXIFTOOL():
         try:
             result = json.loads(
                 check_output(["exiftool", "-j", "-n", videoFile]))[0]
         except CalledProcessError:
             logger.warn("exiftool returned non-zero status for video %s",
                         videoFile)
         except (IndexError, ValueError):
             logger.warn("Failed reading exiftool result for video %s",
                         videoFile)
         else:
             try:
                 return result["ImageWidth"], result["ImageHeight"], \
                         result["AvgBitrate"], result["Duration"]
             except KeyError:
                 logger.warn(
                     "Failed reading video properties from exiftool JSON")
     else:
         logger.warn("None of [Python ffvideo library, exiftool] installed")
Пример #32
0
def get_video_link_with_thumbnail(item):
    outfile = item.thumbnail_local
    try:
        if not os.path.exists(outfile):
            # make video thumbnail
            try:
                pil_image = VideoStream(item.fullpath).get_frame_at_sec(5).image()
            except:
                try:
                    pil_image = VideoStream(item.fullpath).get_frame_at_sec(0.5).image()
                except:
                    pil_image = VideoStream(item.fullpath).get_frame_at_sec(0).image()
            size = 250,250
            pil_image.thumbnail(size)
            pil_image.save(outfile)
        fileOk=True
    except:
        fileOk=False
    out = ""
    imgPath = item.thumbnail_web if fileOk else ERROR_THUMBNAIL
    out += "<br/>"+GetLink(item.web_original, "<img style=\"max-width:95%;border:3px solid black;\" src=\""+\
            imgPath+"\"><br/>"+("" if fileOk else "ERROR: ")+item.basename_short+"</img>"+"\n", newTab=True)
    return out
Пример #33
0
def test_frames_getting():
    from ffvideo import VideoStream

    vs = VideoStream(v0)

    f1 = vs.current() # first frame
    f2 = vs.next()
    assert f2.timestamp > f1.timestamp

    f = vs[0] # first frame
    assert f.frameno == 0
    f = vs.get_frame_no(100)
#    f = vs[100]
#    assert f.frameno == 100

    f = vs.get_frame_no(100)
    f = vs.get_frame_at_sec(1)
    assert f.timestamp - 1 < 0.1
    f = vs.get_frame_at_pts(133000)

    assert f.width == vs.frame_width
    assert f.height == vs.frame_height
    assert f.mode == vs.frame_mode
Пример #34
0
def slurp_video(path):
    video = VideoStream(path)
    return video.get_frame_at_sec(2)
Пример #35
0
class videoExplorer(object):
    """
    Class for access of recorded video files

    Allows to filter video files in a root folder to be retrieved by date

    """
    def __init__(self,  verbose=False, rootPath="/run/media/peter/Elements/peter/data/"):
        """

        Args:
            verbose (bool):
                                switch verbosity on (prints some status messages)
            rootPath (string):
                                root directory to be scanned for video files
        """
        self.rootPath = rootPath
        self.start = 0
        self.end = 0
        self.fileList = []
        self.nightList = []
        self.dayList = []
        self.verbose = verbose
        self.vs = None
        self.frameMode = None
        self.frame = None

    def setTimeRange(self,  start,  end):
        """
        set range of video explorer (works similar to a slice())

        :note:`The range is inclusive for the lower and upper end of the range`

        Args:
            start (datetime object):
                                beginning of time range
            end(datetime object):
                                end of time range
        """
        self.start = start
        self.end = end

    def setRootPath(self,  root):
        """
        Set root path of object

        Test:
            asdad
        """
        self.rootPath = root

    def parseFiles(self):
        """
        parses files in the main path and returns list of all files fitting
        the start - end criteria

        make sure that before calling this function:

            - start and end datetimes were set
            - mainPath is set

        .. seealso::
            :func:`setTimeRange()`
            :func:`setRootPath()`
        """
        if self.start == 0 or self.end == 0:
            raise ValueError("start or end value not set properly")

        self.fileList = []

        for root,  dirs,  files in os.walk(self.rootPath):
            for fn in files:
                ################################################################ TODO: sort files by file name
                fileDT = self.fileName2DateTime(fn)

                if fileDT == -1:
                    ## file is no mp4 file of interest
                    continue
                ################################################################ TODO: add option to load files without filter

                if self.start <= fileDT and self.end >= fileDT:
                    ## if timestamp is within the given range, add to list
                    self.fileList.append([fileDT, root + r'/' + fn])

        self.filterDayVideos()
        self.filterNightVideos()


    def filterDayVideos(self):
        """
        generates list (self.nightList) of videos that were recorded during
        day (between 10am and 11pm)
        """
        self.dayList = []

        for vid in self.fileList:
            hour = vid[0].hour
            if hour >= 10 and hour < 23:
                self.dayList.append(vid)

    def filterNightVideos(self):
        """
        generates list (self.nightList) of videos that were recorded during
        night (between 11pm and 10am)
        """
        self.nightList = []

        for vid in self.fileList:
            hour = vid[0].hour
            if hour < 10 or hour >= 23:
                self.nightList.append(vid)

    def getPathsOfList(self,  list):
        """
        returns a list of pure filenames without the corresponding datetime
        """
        out = []
        for item in list:
            out.append(item[1])

        return out

    def getDatesOfList(self,  list):
        """
        returns a list of pure datetime without the corresponding paths
        """
        out = [item[0] for item in list]

        return out


    def fileName2DateTime(self,  fn, ending="mp4"):
        """
        converts filename of video file to python datetime object

        Args:
            fn (string):
                                filename

        Returns:
            out (datetime object):
                                conversion of filename
        """

        basename = os.path.basename(fn)
        parts = re.split("[.]",  basename)
        date = re.split("[-]", parts[0])
        time = re.split("[-]", parts[1])

        if fn[-len(ending):] != ending:
            return -1

        if len(date) != 3:
            raise ValueError("mp4 file without proper date part discovered")
            return -1

        if len(time) < 3:
            raise ValueError("mp4 file without proper time part discovered")
            return -1

        out = dt.datetime(int(date[0]), int(date[1]), int(date[2]),
                          int(time[0]), int(time[1]), int(time[2]))

        return out


    def findInteruptions(self, fileList):
        """
        separates the filelist into ranges of consequent films, i.e. each
        range represents a batch of videos that share the same background model

        :note:`it is assumed that each consequtive file is 1 min long`

        Args:
            fileList (list of [datetime, path])
                                each element of this list can be generated by
                                :func:`fileName2DateTime`

        Returns:
            ranges of datetime that can be passed directly into
            :func:`setTimeRange`
        """
        # set file chunk size to 1 minute
        chunkSize = dt.time(0, 1)

        fileList = sorted(fileList)

        rngs = []
        start = None
        stop = None

        if len(fileList) < 2:
            raise ValueError("There are less then 2 files in the list. That usually means that a wrong folder was selected.")

        for i in range(len(fileList) - 1):
            if start is None:
                start = fileList[i][0]
                if (not (fileList[i + 1][0] - fileList[i][0]) <= dt.timedelta(minutes=1))\
                    or fileList[i + 1][0].second != 0:
                    stop = fileList[i][0]

            elif (not (fileList[i + 1][0] - fileList[i][0]) == dt.timedelta(minutes=1))\
                    or fileList[i + 1][0].second != 0:
                stop = fileList[i][0]

            if stop is not None:
                rngs += [[start, stop, i]]
                start = None
                stop = None

        stop = fileList[i+1][0]
        rngs += [[start, stop, i]]

        return rngs

    def getRandomFrame(self, pathList,  info=False, frameMode='L'):
        """
        returns the first frame from a random video of the list

        Args:
            pathList (List of Strings):
                                paths to video files
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:

                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???

        Returns:
            frame (ndarray):
                                decoded video frame
        """
        file = pathList[random.randint(0,  len(pathList) - 1)]
        frameNo = random.randint(0,  1600 - 1)

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo,  file)

        self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)
        self.frameMode = frameMode

        frame = self.vs.next().ndarray()

        if info:
            return [frame, file]
        else:
            return frame

    def getFrame(self, file, frameNo=0, info=False, frameMode='L'):
        """
        returns the given frame from a given video

        Args:
            file (String):
                                path to video file
            frameNo (int):
                                frame number to be returned
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:

                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???


        Returns:
            frame (ndarray):
                                decoded video frame
        """

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo,  file)

        if not self.vs \
        or self.frameMode != frameMode \
        or self.vs.filename != file:
            self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)

            self.frameMode = frameMode
        
        frame = self.vs.get_frame_no(frameNo).ndarray()
        
        if info:
            return [frame, file]
        else:
            return frame
                
    def next(self):
        """
        returns next frame in opened video file
        Call getFrame() or getRandomFrame() first
        
        Args:
            info (bool):
                                return frame and filename
                                
        .. seealso::
            :func:`getFrame`
            :func:`getRandomFrame`
        """
        
        if self.vs is None:
            raise AttributeError("no video stream defined. (It might be that" +\
                                 " the last frame was captured before)")
        
        try:
            frame = self.vs.next().ndarray()
        except ffvideo.NoMoreData:
            self.vs = None
            frame = None
            raise StopIteration
            
        return frame
        
    
    def setVideoStream(self, file,  info=False, frameMode='L'):
        """
        returns the first frame from a random video of the list
        
        Args:
            pathList (List of Strings):
                                paths to video files
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:
                                
                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???
            
        Returns:
            frame (ndarray):
                                decoded video frame
        """       
        frameNo = random.randint(0,  1600 - 1)
        
        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo,  file)

        if not self.frame is None:
            del self.frame

        if not self.vs is None:
            del self.vs


        self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)
        self.frameMode = frameMode
        
    def __iter__(self):
        # rewind ffvideo thingy
        if self.vs is not None:
            self.vs.__iter__()
        
        return self
    
    @staticmethod
    def splitFolders(path):
        """
        splits path into its folders and returns them 
        in a list. 

        .. note::
            Last entry of the returned list will be the
            basename of the path if applicable

        Args:
            path (string):
                            path to split

        Returns:
            folders (list of strings):
                            list of folders in path
                            
        Source:
            http://stackoverflow.com/questions/3167154/how-to-split-a-dos-path-into-its-components-in-python
        """
        folders=[]
        while 1:
            path,folder=os.path.split(path)

            if folder!="":
                folders.append(folder)
            else:
                if path!="":
                    folders.append(path)

                break

        folders.reverse()

        return folders

    @staticmethod
    def retrieveVideoLength(filename, initialStepSize=10000):
        """
        Finds video length by accessing it bruteforce

        """
        idx = initialStepSize
        modi = initialStepSize
        vE = videoExplorer()

        reached_end = False

        while modi > 0:
            while True:
                try:
                    vE.getFrame(filename, frameNo=idx, frameMode='RGB')
                except ffvideo.FFVideoError:
                    if modi == 1:
                        idx -= modi

                    reached_end = True
                    break
                except StopIteration:
                    if modi == 1:
                        idx -= modi

                    reached_end = True
                    break

                if reached_end and modi > 1:
                    modi /= 2

                idx += modi

            modi /= 2
            idx -= modi



        return idx + 1 # + 1 to make it the same behaviour as len()


    @staticmethod
    def findFileInList(lst, filename):
        """
        returns the position of the file within the list of paths
        """
        return [a for a,b in enumerate(lst) if b[1].endswith(filename)]
        
    @staticmethod
    def findClosestDate(dateList, date):
		min(DTlist,key=lambda date : abs(dt-date))
Пример #36
0
import PIL

from ffvideo import VideoStream


VIDEO_FILE = '/Users/drautb/Desktop/haystack-samples/moto-x/VID_20151207_081126527.mp4'
THUMBNAIL = '/Users/drautb/Desktop/haystack-samples/moto-x/test-thumbnail.jpg'

vs = VideoStream(VIDEO_FILE,
                 frame_size=(128, None),  # scale to width 128px
                 frame_mode='RGB')  # convert to grayscale

# vs = VideoStream(VIDEO_FILE)

print "frame size: %dx%d" % (vs.frame_width, vs.frame_height)
print "Width: %d" % (vs.width)
print "Height: %d" % (vs.height)

frame = vs.get_frame_at_sec(0)
frame.image().transpose(PIL.Image.ROTATE_270).save(THUMBNAIL)
Пример #37
0
 def _generate_thumbnail(self, image_filepath, at_second=60):
     vs = VideoStream(self.video_full_path)
     try:
         vs.get_frame_at_sec(int(at_second)).image().save(image_filepath)
     except Exception:
         pass
Пример #38
0
def insert(dirname):
    select_query = 'select * from img where img_path="%s"'%path
    cur.execute("set names utf8")
    cur.execute(select_query)

    results = cur.fetchall()
    db_path_arr = []

    for i in results:
        db_path_arr.append(i[3])
    diff_arr = []

    for a in path_arr:
        if a not in db_path_arr:
            diff_arr.append(a)

    for full_path in diff_arr:

        ext = os.path.splitext(full_path)[-1]
        thum = "%s_" % (datetime.today().strftime('%Y%m%d')) + str(uuid.uuid4())[:8] + ".jpg"
        small_thum_path = "/DATA/reference_library/thumnail/small_thum/%s"%thum
        filename = os.path.split(full_path)[-1]

        if ext.lower() == ".mp4" or ext.lower() == ".mov" or ext.lower() == ".mkv" or ext.lower() == ".avi" or ext.lower() == ".webm":

            try:
                duration = int(VideoStream(full_path).duration/2)
                thumbnail = VideoStream(full_path).get_frame_at_sec(duration).image()
                thumbnail.save(small_thum_path)

            except Exception, e:
                print e

            select_query = 'select * from img where full_path = "%s"' % full_path[5:]
            cur.execute("set names utf8")
            cur.execute(select_query)

            if cur.rowcount == 0:
                try:
                    codec = VideoStream(full_path).codec_name
                    print codec
                    if codec == "h264" or codec =="aac":
                        full_path = full_path[5:]
                        query_o = 'insert into img(img_path,img_name,full_path,thumnail,type) values("%s","%s","%s","%s","%s")' % (path,filename,full_path,thum,'video')
                        # print query_o
                        cur.execute("set names utf8")
                        cur.execute(query_o)
                        db.commit()

                    else:
                        full_path = full_path[5:]
                        query = 'insert into img(img_path,img_name,full_path,thumnail,type) values("%s","%s","%s","%s","%s")'%(path,filename,full_path,'codec.png','video')
                        # print query
                        cur.execute("set names utf8")
                        cur.execute(query)
                        db.commit()

                except Exception,e:
                    pass
            else:
                pass
Пример #39
0
def prep_sql_shows(Shows_mnt, conn):
    cur = conn.cursor()
    while 1:
        ii = 0
        for (dirpath, dirnames, filenames) in os.walk(os.path.normpath(Shows_mnt)):
            if filenames:
                for file in filenames:
                    if ".DS_Store" in file:
                        continue
                    file_parts = file.split(".")
                    ext = file_parts[-1].lower()
                    del file_parts[-1]
                    if ext == "nfo" or ext == "jpg" or ext == "jpeg" or ext == "srt" or ext == "ifo" or ext == "bup" or ext == "nzb" or ext == "idx" or ext == "sfv" or ext == "txt" or ext == "db" or ext == "part":
                        continue
                    paths = []
                    parse = ""
                    i = 0
                    while Shows_mnt != parse:
                        if i == 0:
                            paths.append(file)
                            parse = os.path.dirname(os.path.normpath(dirpath + "/" + file))
                        else:
                            if parse in paths:
                                parse = os.path.dirname(parse)
                        i += 1
                        paths.append(parse)

                    #print paths
                    path_string = ''.join(paths)
                    path_hash = hashlib.sha256(path_string).hexdigest()
                    #print path_hash
                    cur.execute("SELECT `id` FROM `tongue`.`video_files` WHERE `path_hash` = %s LIMIT 1", str(path_hash))
                    row = cur.fetchone()
                    #print row
                    if not row:
                        if ext != "rm":
                            filepath = dirpath+"/"+file
                            print filepath
                            file_parts = file.split(".")
                            ext = file_parts[-1].lower()
                            del file_parts[-1]
                            file_name_no_ext = "-".join(file_parts)
                            try:
                                vs = VideoStream(filepath)
                            except DecoderError:
                                #pass
                                codec = "unknown"
                                length = 0
                                dimensions = "0x0"
                                print "Decoder Error :("
                            except NoMoreData:
                                codec = "unknown"
                                length = 0
                                dimensions = "0x0"
                                print "File corrupt??"
                            else:
                                if vs.duration < 20:
                                    frame = vs.get_frame_at_sec(vs.duration/2)
                                else:
                                    frame = vs.get_frame_at_sec(20)
                                codec = vs.codec_name
                                hours = vs.duration/3600
                                minuets = (vs.duration/60) - (int(hours) * 60)
                                rg = re.compile('.*?\\d+.*?(\\d+)',re.IGNORECASE|re.DOTALL)
                                m = rg.search(str(minuets))
                                seconds = int(float("0."+ m.group(1)) * 60)

                                # print vs.duration, minuets, hours
                                length = "%dh:%02d:%02d" % (hours, minuets, seconds)
                                dimensions = "%dx%d" % (vs.frame_width, vs.frame_height)
                        if paths:
                            plen = len(paths)
                            if plen == 3:
                                video = str(paths[0])

                                show_folder = str(os.path.basename(paths[plen-2]))
                                show_id = select_show_id(show_folder.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), conn)
                                if show_id == 0:
                                    show_id = insert_show(show_folder.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), conn)

                                season_folder = str(os.path.basename(paths[plen-2]))
                                season_id = select_season_id(season_folder.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), conn)
                                if season_id == 0:
                                    season_id = insert_season(season_folder.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), show_id, conn)

                                try:
                                    cur.execute("INSERT INTO `tongue`.`video_files` (`id`, `video`, `season_id`, `show_id`, `path_hash`, `runtime`, `dimensions`) VALUES (NULL, %s, %s, %s, %s, %s, %s )", (video.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), season_id, show_id, str(path_hash), length, dimensions))
                                except cymysql.MySQLError, e:
                                    print e
                                else:
                                    #print paths[0]+"|=|"+os.path.basename(paths[plen-2])+"|=|"+os.path.basename(paths[plen-2])
                                    #print show_id, season_id
                                    if ii == 100:
                                        sys.stdout.write("\n")
                                        ii = 0
                                    ii += 1
                                    sys.stdout.write(".")
                                    sys.stdout.flush()
                            else:
                                video = str(paths[0])

                                show_folder = str(os.path.basename(paths[plen-2]))
                                show_id = select_show_id(show_folder.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), conn)
                                if show_id == 0:
                                    show_id = insert_show(show_folder.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), conn)

                                season_folder = str(paths[plen-3].strip(Shows_mnt))
                                season_id = select_season_id(season_folder.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), conn)
                                if season_id == 0:
                                    season_id = insert_season(season_folder.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), show_id, conn)
                                try:
                                    cur.execute("INSERT INTO `tongue`.`video_files` (`id`, `video`, `season_id`, `show_id`, `path_hash`) VALUES (NULL, %s, %s, %s, %s )", (video.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\("), season_id, show_id, str(path_hash)))
                                except cymysql.MySQLError, e:
                                    print e
                                sys.stdout.write(".")
                                sys.stdout.flush()
                            conn.commit()
Пример #40
0
def prep_sql_movies(Movies_mnt, conn):
    cur = conn.cursor()
    #print [name for name in os.listdir(Movies_mnt) if os.path.isdir(Movies_mnt)]
    mnt_path = Movies_mnt.split('/')
    mnt_num = (len(mnt_path) - 1)
    while 1:
        dvd_flag = 0
        grouped = 0
        group = 1
        prev_folder = ""
        for root, dirs, files in os.walk(Movies_mnt):
            path = root.split('/')
            #print path
            root_num = (len(path) - 1)
            sub = os.path.basename(root)
            if prev_folder != sub:
                grouped = 0
            for file in files:
                filepath = root+"/"+file
                #print filepath
                path_hash = hashlib.sha256(filepath).hexdigest()
                file_parts = file.split(".")
                ext = file_parts[-1].lower()
                if ext == "nfo" or ext == "jpg" or ext == "jpeg" or ext == "srt" or ext == "ifo" or ext == "bup" or ext == "nzb" or ext == "idx" or ext == "sfv" or ext == "txt" or ext == "db" or ext == "DS_Store":
                    continue
                cur.execute("SELECT `id` FROM `tongue`.`movie_files` WHERE `path_hash` = %s LIMIT 1", str(path_hash))
                row = cur.fetchone()
                #print filepath
                #print row
                if not row:
                    del file_parts[-1]
                    file_name_no_ext = "-".join(file_parts)
                else:
                    continue
                #print len(path)*'---'
                if mnt_num == root_num-1:
                    #print "In root: " + root
                    ii = 0
                else:
                    #print "Sub of root: " + sub + " : " + file
                    if sub.upper() == "VIDEO_TS":
                        #print "Is DVD Video: " + file
                        if dvd_flag == 1:
                            print "flag already set"
                            #continue
                        else:
                            dvd_flag = 1
                        file = sub
                    else:
                        dvd_flag = 0
                    lower_file = file.lower()

                    if "cd1" in lower_file or "cd2" in lower_file:
                        #print lower_file
                        if prev_folder != sub:
                            prev_folder = sub
                            grouped = 1
                            group += 1
                            #print
                    else:
                        grouped = 0
                if not dvd_flag:
                    try:
                        vs = VideoStream(filepath)
                    except DecoderError:
                        #pass
                        codec = "unknown"
                        length = 0
                        dimensions = "0x0"
                        print "Decoder Error :("
                    except NoMoreData:
                        codec = "unknown"
                        length = 0
                        dimensions = "0x0"
                        print "File corrupt??"
                    else:
                        frame = vs.get_frame_at_sec(200)
                        codec = vs.codec_name
                        hours = vs.duration/3600
                        minuets = (vs.duration/60) - (int(hours) * 60)
                        rg = re.compile('.*?\\d+.*?(\\d+)',re.IGNORECASE|re.DOTALL)
                        m = rg.search(str(minuets))
                        seconds = int(float("0."+ m.group(1)) * 60)

                        # print vs.duration, minuets, hours
                        length = "%dh:%02d:%02d" % (hours, minuets, seconds)
                        dimensions = "%dx%d" % (vs.frame_width, vs.frame_height)
                else:
                    codec = "RAWDVD"
                    length = 0
                    dimensions = "0x0"

                fullpath = filepath.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\(")
                file_ = file.replace("'", "\\'").replace(" ", "\\ ").replace("-", "\-").replace("&", "\&").replace(")", "\)").replace("(", "\(")
                #print "DVD Flag: " + str(dvd_flag)
                if dvd_flag == 1:
                    file_ = path[-2]
                if grouped == 0:
                    group_ins = 0
                else:
                    group_ins = group
                #print grouped, group_ins
                try:
                    cur.execute("INSERT INTO `tongue`.`movie_files` (`id`, `fullpath`, `filename`, `path_hash`, `grouped`, `group`, `dvd_raw`, `runtime`, `dimensions`, `codec`) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (fullpath.replace(os.path.dirname(root) ,""), file_, str(path_hash), grouped, group_ins, dvd_flag, length, dimensions, codec))
                except cymysql.MySQLError, e:
                    print e
                sys.stdout.write("`")
                sys.stdout.flush()
                conn.commit()
                #print len(path)*'---', file
        time.sleep(900)
Пример #41
0
import sys
from ffvideo import VideoStream
from vidextract import bwextract, enc, c64asm

infile = sys.argv[1]
outfile = sys.argv[2]

vs = VideoStream(infile,
                 frame_size=(320, 200),
                 frame_mode='L') # convert to grayscale

frame = vs.get_frame_at_sec(0)
img = bwextract.tobw(frame.ndarray())
bmp = enc.conv_c64_hires(img)
asm = c64asm.asm_byte_frame(bmp)

with open(outfile, 'w') as f:
    f.write(asm)
Пример #42
0
 def resetMovie(self):
     """Reset the video stream."""
     self.source = VideoStream(self.fileName)
imgs = []

files = []
for ext in ["3gp", "mp4"]:
	files += glob.glob(workingdir + "/*." + ext)

for media in files:
	filename = os.path.basename(media)
	ext = os.path.splitext(filename)
	if ext[1].lower() not in [".mp4", ".3gp"]:
		filename = ext[0] + ".mp4"
		cmd = "avconv -i " + media + " " + imagedir + filename
		subprocess.check_call(cmd) 
	else:
		shutil.copy2(media, imagedir + filename)
	stream = VideoStream(media)
	im = stream.get_frame_at_sec(min(stream.duration / 2, 5)).image()
	thumb_file_name = ext[0] + ".jpeg"
	im.save(imagedir + thumb_file_name)
	im = getThumb(im)
	im.save(thumbdir + thumb_file_name)
	imgs.append({"thumbnail": thumbdir_base + thumb_file_name, 
				"filename": filename,
				"poster": imagedir_base + thumb_file_name, 
				"href": imagedir_base + filename,
				"type": "video/mp4"})

files = []
for ext in ["png", "jpg", "JPG"]:
	files += glob.glob(workingdir + "/*." + ext)
Пример #44
0
class videoExplorer(object):
    """
    Class for access of recorded video files

    Allows to filter video files in a root folder to be retrieved by date

    """
    def __init__(self,
                 verbose=False,
                 rootPath="/run/media/peter/Elements/peter/data/"):
        """

        Args:
            verbose (bool):
                                switch verbosity on (prints some status messages)
            rootPath (string):
                                root directory to be scanned for video files
        """
        self.rootPath = rootPath
        self.start = 0
        self.end = 0
        self.fileList = []
        self.nightList = []
        self.dayList = []
        self.verbose = verbose
        self.vs = None
        self.frameMode = None
        self.frame = None

    def setTimeRange(self, start, end):
        """
        set range of video explorer (works similar to a slice())

        :note:`The range is inclusive for the lower and upper end of the range`

        Args:
            start (datetime object):
                                beginning of time range
            end(datetime object):
                                end of time range
        """
        self.start = start
        self.end = end

    def setRootPath(self, root):
        """
        Set root path of object

        Test:
            asdad
        """
        self.rootPath = root

    def parseFiles(self):
        """
        parses files in the main path and returns list of all files fitting
        the start - end criteria

        make sure that before calling this function:

            - start and end datetimes were set
            - mainPath is set

        .. seealso::
            :func:`setTimeRange()`
            :func:`setRootPath()`
        """
        if self.start == 0 or self.end == 0:
            raise ValueError("start or end value not set properly")

        self.fileList = []

        for root, dirs, files in os.walk(self.rootPath):
            for fn in files:
                ################################################################ TODO: sort files by file name
                fileDT = self.fileName2DateTime(fn)

                if fileDT == -1:
                    ## file is no mp4 file of interest
                    continue
                ################################################################ TODO: add option to load files without filter

                if self.start <= fileDT and self.end >= fileDT:
                    ## if timestamp is within the given range, add to list
                    self.fileList.append([fileDT, root + r'/' + fn])

        self.filterDayVideos()
        self.filterNightVideos()

    def filterDayVideos(self):
        """
        generates list (self.nightList) of videos that were recorded during
        day (between 10am and 11pm)
        """
        self.dayList = []

        for vid in self.fileList:
            hour = vid[0].hour
            if hour >= 10 and hour < 23:
                self.dayList.append(vid)

    def filterNightVideos(self):
        """
        generates list (self.nightList) of videos that were recorded during
        night (between 11pm and 10am)
        """
        self.nightList = []

        for vid in self.fileList:
            hour = vid[0].hour
            if hour < 10 or hour >= 23:
                self.nightList.append(vid)

    def getPathsOfList(self, list):
        """
        returns a list of pure filenames without the corresponding datetime
        """
        out = []
        for item in list:
            out.append(item[1])

        return out

    def getDatesOfList(self, list):
        """
        returns a list of pure datetime without the corresponding paths
        """
        out = [item[0] for item in list]

        return out

    def fileName2DateTime(self, fn, ending="mp4"):
        """
        converts filename of video file to python datetime object

        Args:
            fn (string):
                                filename

        Returns:
            out (datetime object):
                                conversion of filename
        """

        basename = os.path.basename(fn)
        parts = re.split("[.]", basename)
        date = re.split("[-]", parts[0])
        time = re.split("[-]", parts[1])

        if fn[-len(ending):] != ending:
            return -1

        if len(date) != 3:
            raise ValueError("mp4 file without proper date part discovered")
            return -1

        if len(time) < 3:
            raise ValueError("mp4 file without proper time part discovered")
            return -1

        out = dt.datetime(int(date[0]), int(date[1]), int(date[2]),
                          int(time[0]), int(time[1]), int(time[2]))

        return out

    def findInteruptions(self, fileList):
        """
        separates the filelist into ranges of consequent films, i.e. each
        range represents a batch of videos that share the same background model

        :note:`it is assumed that each consequtive file is 1 min long`

        Args:
            fileList (list of [datetime, path])
                                each element of this list can be generated by
                                :func:`fileName2DateTime`

        Returns:
            ranges of datetime that can be passed directly into
            :func:`setTimeRange`
        """
        # set file chunk size to 1 minute
        chunkSize = dt.time(0, 1)

        fileList = sorted(fileList)

        rngs = []
        start = None
        stop = None

        if len(fileList) < 2:
            raise ValueError(
                "There are less then 2 files in the list. That usually means that a wrong folder was selected."
            )

        for i in range(len(fileList) - 1):
            if start is None:
                start = fileList[i][0]
                if (not (fileList[i + 1][0] - fileList[i][0]) <= dt.timedelta(minutes=1))\
                    or fileList[i + 1][0].second != 0:
                    stop = fileList[i][0]

            elif (not (fileList[i + 1][0] - fileList[i][0]) == dt.timedelta(minutes=1))\
                    or fileList[i + 1][0].second != 0:
                stop = fileList[i][0]

            if stop is not None:
                rngs += [[start, stop, i]]
                start = None
                stop = None

        stop = fileList[i + 1][0]
        rngs += [[start, stop, i]]

        return rngs

    def getRandomFrame(self, pathList, info=False, frameMode='L'):
        """
        returns the first frame from a random video of the list

        Args:
            pathList (List of Strings):
                                paths to video files
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:

                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???

        Returns:
            frame (ndarray):
                                decoded video frame
        """
        file = pathList[random.randint(0, len(pathList) - 1)]
        frameNo = random.randint(0, 1600 - 1)

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo, file)

        self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)
        self.frameMode = frameMode

        frame = self.vs.next().ndarray()

        if info:
            return [frame, file]
        else:
            return frame

    def getFrame(self, file, frameNo=0, info=False, frameMode='L'):
        """
        returns the given frame from a given video

        Args:
            file (String):
                                path to video file
            frameNo (int):
                                frame number to be returned
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:

                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???


        Returns:
            frame (ndarray):
                                decoded video frame
        """

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo, file)

        if not self.vs \
        or self.frameMode != frameMode \
        or self.vs.filename != file:
            self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)

            self.frameMode = frameMode

        frame = self.vs.get_frame_no(frameNo).ndarray()

        if info:
            return [frame, file]
        else:
            return frame

    def next(self):
        """
        returns next frame in opened video file
        Call getFrame() or getRandomFrame() first
        
        Args:
            info (bool):
                                return frame and filename
                                
        .. seealso::
            :func:`getFrame`
            :func:`getRandomFrame`
        """

        if self.vs is None:
            raise AttributeError("no video stream defined. (It might be that" +\
                                 " the last frame was captured before)")

        try:
            frame = self.vs.next().ndarray()
        except ffvideo.NoMoreData:
            self.vs = None
            frame = None
            raise StopIteration

        return frame

    def setVideoStream(self, file, info=False, frameMode='L'):
        """
        returns the first frame from a random video of the list
        
        Args:
            pathList (List of Strings):
                                paths to video files
            info (bool):
                                return frame and filename
            frameMode (String):
                                Switch of color mode:
                                
                                - 'RGB': color representation
                                - 'L':   gray-scale representation
                                - 'F':   ???
            
        Returns:
            frame (ndarray):
                                decoded video frame
        """
        frameNo = random.randint(0, 1600 - 1)

        if self.verbose:
            print "processing frame {0} of video {1}".format(frameNo, file)

        if not self.frame is None:
            del self.frame

        if not self.vs is None:
            del self.vs

        self.vs = VideoStream(file, frame_mode=frameMode, exact_seek=True)
        self.frameMode = frameMode

    def __iter__(self):
        # rewind ffvideo thingy
        if self.vs is not None:
            self.vs.__iter__()

        return self

    @staticmethod
    def splitFolders(path):
        """
        splits path into its folders and returns them 
        in a list. 

        .. note::
            Last entry of the returned list will be the
            basename of the path if applicable

        Args:
            path (string):
                            path to split

        Returns:
            folders (list of strings):
                            list of folders in path
                            
        Source:
            http://stackoverflow.com/questions/3167154/how-to-split-a-dos-path-into-its-components-in-python
        """
        folders = []
        while 1:
            path, folder = os.path.split(path)

            if folder != "":
                folders.append(folder)
            else:
                if path != "":
                    folders.append(path)

                break

        folders.reverse()

        return folders

    @staticmethod
    def retrieveVideoLength(filename, initialStepSize=10000):
        """
        Finds video length by accessing it bruteforce

        """
        idx = initialStepSize
        modi = initialStepSize
        vE = videoExplorer()

        reached_end = False

        while modi > 0:
            while True:
                try:
                    vE.getFrame(filename, frameNo=idx, frameMode='RGB')
                except ffvideo.FFVideoError:
                    if modi == 1:
                        idx -= modi

                    reached_end = True
                    break
                except StopIteration:
                    if modi == 1:
                        idx -= modi

                    reached_end = True
                    break

                if reached_end and modi > 1:
                    modi /= 2

                idx += modi

            modi /= 2
            idx -= modi

        return idx + 1  # + 1 to make it the same behaviour as len()

    @staticmethod
    def findFileInList(lst, filename):
        """
        returns the position of the file within the list of paths
        """
        return [a for a, b in enumerate(lst) if b[1].endswith(filename)]

    @staticmethod
    def findClosestDate(dateList, date):
        min(DTlist, key=lambda date: abs(dt - date))
Пример #45
0
 def video_thumbnail(temp_file, uploaded_file):
     from ffvideo import VideoStream
     vs = VideoStream(uploaded_file)
     frame = vs.get_frame_at_sec(2)
     frame.image().save(temp_file)
Пример #46
0
def get_video_dimensions(path):
    from ffvideo import VideoStream

    vs = VideoStream(path)
    return (vs.window_width, vs.window_height)
Пример #47
0

def scale_to_range(image):  # skalira elemente slike na opseg od 0 do 1
    ''' Elementi matrice image su vrednosti 0 ili 255.
        Potrebno je skalirati sve elemente matrica na opseg od 0 do 1
    '''
    return image / 255


def matrix_to_vector(image):
    '''Sliku koja je zapravo matrica 28x28 transformisati u vektor sa 784 elementa'''
    return image.flatten()

i = 0

for frame in VideoStream('videos/video-9.avi'):

    i = i + 1
    if not i % 40 == 0:
        continue

    frame.image().save('temp.png')
    image_color = load_image('temp.png')
    img = invert(image_bin(image_gray(image_color)))
    img_bin = erode(dilate(img))
    selected_regions, numbers = select_roi(image_color.copy(), img)
    new = cv2.addWeighted(image_color, 1, selected_regions, 1, 0)
    new = cv2.cvtColor(new, cv2.COLOR_BGR2RGB)
    if i % 40 == 0:
#        cv2.imwrite('i2v/'+str(i)+'.png', new)
        plt.imshow(new)
Пример #48
0
class Movie(QtCore.QObject):
    """This class represent the video data stream"""

    frameChanged = QtCore.pyqtSignal()
    endOfVideo = QtCore.pyqtSignal()
    
    def __init__(self, fileName=None):
        """Initialize the video stream and open the video file if a fileName is provided.
        
        :param filename: a string containing the name of the file to be opened.
        """
        self.rawBuffer=None
        self.source=None
        super(Movie, self).__init__()
        if(fileName is not None):
            self.setMovie(fileName)
        self.timer = None
        self.frame = None
        self.isPlaying = False
        self.frameRate = 0
        self.frameNumber = 0
    
    def reset(self):
        """Reset the movie object. Remove the source."""
        self.rawBuffer=None
        self.source=None
        self.timer = None
        self.frame = None
        self.isPlaying = False
        self.frameRate = 0
        self.frameNumber = 0
    def setMovie(self, fileName):
        """Open a video file.
        
        :param filename: a string containing the name of the file to be opened.
            
        :raise: TypeError: The fileName is not a string.
        """
        if(isinstance(fileName, basestring)):
            self.fileName =  u''.join(fileName).encode('utf-8')
            self.source = VideoStream(self.fileName)
        else: 
            raise TypeError('fileName must be a string')
        
        self.frameRate = self.source.framerate
        self.frameNumber = self.source.duration*1000/self.source.framerate
        self.timer = QtCore.QTimer()
        self.timer.setInterval(1000.0/self.frameRate)
        self.timer.setSingleShot(False)
        self.timer.timeout.connect(self.frameMustChange)
    
    def resetMovie(self):
        """Reset the video stream."""
        self.source = VideoStream(self.fileName)
        
    def play(self): 
        """Start to read the video stream."""
        self.isPlaying = True
        self.timer.start()
    
    def pause(self):
        """Pause the reading of the video stream."""
        self.isPlaying = False
        self.timer.stop()
        
    def frameMustChange(self):
        """Slot called when it is time to load the next frame.
        
        :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed.
        """
        self.readNextFrame()
    
    def toggleState(self):
        """Toggle between playing and pausing the video."""
        if(self.isPlaying==True):
            self.pause()
        else:
            self.play()
            
    def jumpToFrame(self, position):
        """Modify the position in the video.
        
        :param position: a value between 0 and 1 corresponding to the position in the video. 0 is the beginning and 1 is the end.
        """
        if(position>1.0):
            position = 1.0
        elif(position<0.001):
            position = 0.001
        frame = self.source.get_frame_at_sec(position*self.source.duration).ndarray
        return frame
    
    def readNextFrame(self):
        """Load the next frame.
        :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed.
        """
        try:
            self.rawBuffer = self.source.next().ndarray()
        except ffvideo.NoMoreData:
            self.isPlaying = False
            self.pause()
            self.rawBuffer = None
            self.endOfVideo.emit()
        self.frameChanged.emit()
        
    def readNCFrame(self, number):
        """Load the next frame.
        :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed.
        """
        position = self.source.current().frameno
        try:
                self.rawBuffer = self.source.get_frame_no(position+number).ndarray()
        except ffvideo.NoMoreData:
            self.isPlaying = False
            self.pause()
            self.endOfVideo.emit()
        if(self.source.current().frameno>=self.source.duration*self.source.framerate):
            self.isPlaying = False
            self.pause()
            self.endOfVideo.emit()
        self.frameChanged.emit()

        
    def currentPositionRatio(self):
        """Returns the position in the video.
        
        :return: a value between 0 and 1 representing the position in the video. 0 is the beginning and 1 is the end.
        """
        if(self.source is not None and self.source.current() is not None):
            result = self.source.current().timestamp/self.source.duration
            if(result>1.0):
                return 1.0
            elif(result<0.0):
                return 0.0
            else:
                return result
        else:
            return 1.0
    
    def getFrameNumber(self):
        """Returns the number of frame in the video.
        
        :return: An integer representing the number of frame in the video.
        """
        return int(self.source.duration*self.source.framerate)
    
    def getFrameSize(self):
        return (self.source.frame_width, self.source.frame_height)
    
    def getEllapsedTime(self):
        """Returns the number ellapsed time in the video.
        
        :return: An integer representing the number of frame in the video.
        """
        return self.source.current().timestamp
Пример #49
0
def createVideoThumbnail(fileId, fullPath, thumbnailSize, thumbnailsRoot):
    videoImage = VideoStream(fullPath).get_frame_at_sec(int(getVideoDuration(fullPath) / 2)).image()
    videoImage.save('videoPreviewTemp.jpg')
    createImageThumbnail(fileId, 'videoPreviewTemp.jpg', thumbnailSize, thumbnailsRoot)
Пример #50
0
 def resetMovie(self):
     """Reset the video stream."""
     self.source = VideoStream(self.fileName)
Пример #51
0
class Movie(QtCore.QObject):
    """This class represent the video data stream"""

    frameChanged = QtCore.pyqtSignal()
    endOfVideo = QtCore.pyqtSignal()

    def __init__(self, fileName=None):
        """Initialize the video stream and open the video file if a fileName is provided.
        
        :param filename: a string containing the name of the file to be opened.
        """
        self.rawBuffer = None
        self.source = None
        super(Movie, self).__init__()
        if (fileName is not None):
            self.setMovie(fileName)
        self.timer = None
        self.frame = None
        self.isPlaying = False
        self.frameRate = 0
        self.frameNumber = 0

    def reset(self):
        """Reset the movie object. Remove the source."""
        self.rawBuffer = None
        self.source = None
        self.timer = None
        self.frame = None
        self.isPlaying = False
        self.frameRate = 0
        self.frameNumber = 0

    def setMovie(self, fileName):
        """Open a video file.
        
        :param filename: a string containing the name of the file to be opened.
            
        :raise: TypeError: The fileName is not a string.
        """
        if (isinstance(fileName, basestring)):
            self.fileName = u''.join(fileName).encode('utf-8')
            self.source = VideoStream(self.fileName)
        else:
            raise TypeError('fileName must be a string')

        self.frameRate = self.source.framerate
        self.frameNumber = self.source.duration * 1000 / self.source.framerate
        self.timer = QtCore.QTimer()
        self.timer.setInterval(1000.0 / self.frameRate)
        self.timer.setSingleShot(False)
        self.timer.timeout.connect(self.frameMustChange)

    def resetMovie(self):
        """Reset the video stream."""
        self.source = VideoStream(self.fileName)

    def play(self):
        """Start to read the video stream."""
        self.isPlaying = True
        self.timer.start()

    def pause(self):
        """Pause the reading of the video stream."""
        self.isPlaying = False
        self.timer.stop()

    def frameMustChange(self):
        """Slot called when it is time to load the next frame.
        
        :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed.
        """
        self.readNextFrame()

    def toggleState(self):
        """Toggle between playing and pausing the video."""
        if (self.isPlaying == True):
            self.pause()
        else:
            self.play()

    def jumpToFrame(self, position):
        """Modify the position in the video.
        
        :param position: a value between 0 and 1 corresponding to the position in the video. 0 is the beginning and 1 is the end.
        """
        if (position > 1.0):
            position = 1.0
        elif (position < 0.001):
            position = 0.001
        frame = self.source.get_frame_at_sec(position *
                                             self.source.duration).ndarray
        return frame

    def readNextFrame(self):
        """Load the next frame.
        :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed.
        """
        try:
            self.rawBuffer = self.source.next().ndarray()
        except ffvideo.NoMoreData:
            self.isPlaying = False
            self.pause()
            self.rawBuffer = None
            self.endOfVideo.emit()
        self.frameChanged.emit()

    def readNCFrame(self, number):
        """Load the next frame.
        :raise: Exception: The file cannot be read because the codec is not supported or the video is compressed.
        """
        position = self.source.current().frameno
        try:
            self.rawBuffer = self.source.get_frame_no(position +
                                                      number).ndarray()
        except ffvideo.NoMoreData:
            self.isPlaying = False
            self.pause()
            self.endOfVideo.emit()
        if (self.source.current().frameno >=
                self.source.duration * self.source.framerate):
            self.isPlaying = False
            self.pause()
            self.endOfVideo.emit()
        self.frameChanged.emit()

    def currentPositionRatio(self):
        """Returns the position in the video.
        
        :return: a value between 0 and 1 representing the position in the video. 0 is the beginning and 1 is the end.
        """
        if (self.source is not None and self.source.current() is not None):
            result = self.source.current().timestamp / self.source.duration
            if (result > 1.0):
                return 1.0
            elif (result < 0.0):
                return 0.0
            else:
                return result
        else:
            return 1.0

    def getFrameNumber(self):
        """Returns the number of frame in the video.
        
        :return: An integer representing the number of frame in the video.
        """
        return int(self.source.duration * self.source.framerate)

    def getFrameSize(self):
        return (self.source.frame_width, self.source.frame_height)

    def getEllapsedTime(self):
        """Returns the number ellapsed time in the video.
        
        :return: An integer representing the number of frame in the video.
        """
        return self.source.current().timestamp