def test_wrong_video_files_type():
    """ Test VideoManager constructor (__init__ method) with invalid video_files
    argument types to trigger a ValueError exception. """
    with pytest.raises(ValueError): VideoManager([0, 1, 2])
    with pytest.raises(ValueError): VideoManager([0, 'somefile'])
    with pytest.raises(ValueError): VideoManager(['somefile', 1, 2, 'somefile'])
    with pytest.raises(ValueError): VideoManager([-1])
    def detect(self, video_path):
        '''

        :param video_path:

        :return: the boundary of each shot:
        '''
        if not os.path.exists(video_path):
            print('The video does not exist!')

        self.video_manager = VideoManager([video_path])
        self.base_timecode = self.video_manager.get_base_timecode()
        self.video_manager.set_downscale_factor()
        self.video_manager.start()
        self.scene_manager.detect_scenes(frame_source=self.video_manager)

        scene_list = self.scene_manager.get_scene_list(self.base_timecode)

        split_frames = [0]
        split_time = [0]
        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))
            split_frames.append(scene[1].get_frames())
            split_time.append(self.__time_trans(scene[1].get_timecode()))
        self.video_manager.release()

        return split_frames, split_time
def test_wrong_framerate_type(test_video_file):
    """ Test VideoManager constructor (__init__ method) with an invalid framerate
    argument types to trigger a TypeError exception. """
    with pytest.raises(TypeError): VideoManager([test_video_file], framerate=int(0))
    with pytest.raises(TypeError): VideoManager([test_video_file], framerate=int(10))
    with pytest.raises(TypeError): VideoManager([test_video_file], framerate='10')
    VideoManager([test_video_file], framerate=float(10)).release()
def test_api():

    print("Running PySceneDetect API test...")

    print("PySceneDetect version being used: %s" % str(scenedetect.__version__))

    # Create a video_manager point to video file testvideo.mp4. Note that multiple
    # videos can be appended by simply specifying more file paths in the list
    # passed to the VideoManager constructor. Note that appending multiple videos
    # requires that they all have the same frame size, and optionally, framerate.
    video_manager = VideoManager(['testvideo.mp4'])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        # If stats file exists, load it.
        if os.path.exists(STATS_FILE_PATH):
            # Read stats from CSV file opened in read mode:
            with open(STATS_FILE_PATH, 'r') as stats_file:
                stats_manager.load_from_csv(stats_file, base_timecode)

        start_time = base_timecode + 20     # 00:00:00.667
        end_time = base_timecode + 20.0     # 00:00:20.000
        # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
        video_manager.set_duration(start_time=start_time, end_time=end_time)

        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        # Like FrameTimecodes, each scene in the scene_list can be sorted if the
        # list of scenes becomes unsorted.

        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i+1,
                scene[0].get_timecode(), scene[0].get_frames(),
                scene[1].get_timecode(), scene[1].get_frames(),))

        # We only write to the stats file if a save is required:
        if stats_manager.is_save_required():
            with open(STATS_FILE_PATH, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)

    finally:
        video_manager.release()
Exemple #5
0
def detect_scene(path, fps, min_sec):
    video_manager = VideoManager([path], framerate=fps)
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    scene_manager.add_detector(
        ContentDetector(threshold=30, min_scene_len=fps * min_sec))
    base_timecode = video_manager.get_base_timecode()

    video_manager.set_downscale_factor()
    video_manager.start()
    scene_manager.detect_scenes(frame_source=video_manager,
                                show_progress=False)
    scene_list = scene_manager.get_scene_list(base_timecode)

    # Like FrameTimecodes, each scene in the scene_list can be sorted if the
    # list of scenes becomes unsorted.

    # print('List of scenes obtained:')
    # for i, scene in enumerate(scene_list):
    #     print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
    #         i + 1,
    #         scene[0].get_timecode(), scene[0].get_frames(),
    #         scene[1].get_timecode(), scene[1].get_frames(),))
    scene_list = [(round(s[0].get_seconds()), round(s[1].get_seconds()))
                  for s in scene_list]
    video_manager.release()
    return scene_list
Exemple #6
0
    def _init_video_manager(self, input_list, framerate, downscale):

        self.base_timecode = None

        logging.debug('Initializing VideoManager.')
        video_manager_initialized = False
        try:
            self.video_manager = VideoManager(
                video_files=input_list, framerate=framerate, logger=logging)
            video_manager_initialized = True
            self.base_timecode = self.video_manager.get_base_timecode()
            self.video_manager.set_downscale_factor(downscale)
        except VideoOpenFailure as ex:
            error_strs = [
                'could not open video%s.' % get_plural(ex.file_list),
                'Failed to open the following video file%s:' % get_plural(ex.file_list)]
            error_strs += ['  %s' % file_name[0] for file_name in ex.file_list]
            dll_okay, dll_name = check_opencv_ffmpeg_dll()
            if not dll_okay:
                error_strs += [
                    'Error: OpenCV dependency %s not found.' % dll_name,
                    'Ensure that you installed the Python OpenCV module, and that the',
                    '%s file can be found to enable video support.' % dll_name]
            logging.debug('\n'.join(error_strs[1:]))
            if not dll_okay:
                click.echo(click.style(
                    '\nOpenCV dependency missing, video input/decoding not available.\n', fg='red'))
            raise click.BadParameter('\n'.join(error_strs), param_hint='input video')
        except VideoFramerateUnavailable as ex:
            error_strs = ['could not get framerate from video(s)',
                          'Failed to obtain framerate for video file %s.' % ex.file_name]
            error_strs.append('Specify framerate manually with the -f / --framerate option.')
            logging.debug('\n'.join(error_strs))
            raise click.BadParameter('\n'.join(error_strs), param_hint='input video')
        except VideoParameterMismatch as ex:
            error_strs = ['video parameters do not match.', 'List of mismatched parameters:']
            for param in ex.file_list:
                if param[0] == cv2.CAP_PROP_FPS:
                    param_name = 'FPS'
                if param[0] == cv2.CAP_PROP_FRAME_WIDTH:
                    param_name = 'Frame width'
                if param[0] == cv2.CAP_PROP_FRAME_HEIGHT:
                    param_name = 'Frame height'
                error_strs.append('  %s mismatch in video %s (got %.2f, expected %.2f)' % (
                    param_name, param[3], param[1], param[2]))
            error_strs.append(
                'Multiple videos may only be specified if they have the same framerate and'
                ' resolution. -f / --framerate may be specified to override the framerate.')
            logging.debug('\n'.join(error_strs))
            raise click.BadParameter('\n'.join(error_strs), param_hint='input videos')
        except InvalidDownscaleFactor as ex:
            error_strs = ['Downscale value is not > 0.', str(ex)]
            logging.debug('\n'.join(error_strs))
            raise click.BadParameter('\n'.join(error_strs), param_hint='downscale factor')
        return video_manager_initialized
Exemple #7
0
def find_scenes(video_path):
    # type: (str) -> List[Tuple[FrameTimecode, FrameTimecode]]
    video_manager = VideoManager([video_path])
    stats_manager = StatsManager()
    # Construct our SceneManager and pass it our StatsManager.
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (each detector's constructor
    # takes detector options, e.g. threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)

    finally:
        video_manager.release()

    scene_list_new = []
    for cur_scene in scene_list:
        start_frame = cur_scene[0].get_frames()
        end_frame = cur_scene[1].get_frames()
        scene_list_new.append((start_frame, end_frame))

    return scene_list_new
Exemple #8
0
def scene_detect(opt):

    video_manager = VideoManager(
        [os.path.join(opt.avi_dir, opt.reference, 'video.avi')])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    video_manager.set_downscale_factor()

    video_manager.start()

    scene_manager.detect_scenes(frame_source=video_manager)

    scene_list = scene_manager.get_scene_list(base_timecode)

    savepath = os.path.join(opt.work_dir, opt.reference, 'scene.pckl')

    if scene_list == []:
        scene_list = [(video_manager.get_base_timecode(),
                       video_manager.get_current_timecode())]

    with open(savepath, 'wb') as fil:
        pickle.dump(scene_list, fil)

    print('%s - scenes detected %d' % (os.path.join(
        opt.avi_dir, opt.reference, 'video.avi'), len(scene_list)))

    return scene_list
def get_scene_startFrames(filepath):
    # Create a video_manager point to video file testvideo.mp4. Note that multiple
    # videos can be appended by simply specifying more file paths in the list
    # passed to the VideoManager constructor. Note that appending multiple videos
    # requires that they all have the same frame size, and optionally, framerate.
    video_manager = VideoManager([filepath])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)

        scene_startFrames = []
        for i, scene in enumerate(scene_list):
            scene_startFrames.append(scene[0].get_frames())

    finally:
        video_manager.release()

    return scene_startFrames
Exemple #10
0
def main():

    #import csv files as pandas
    movieRuntimes = pd.read_csv(
        'C:\\Users\\hasna\\Desktop\\Smell of Fear\\Numerical Data\\movie_runtimes.csv'
    )
    movieList = list(movieRuntimes['movie'])

    #detect number of scenes within a 30 second period
    for movie in movieList:
        aslList = list()
        effectiveRuntime = movieRuntimes['effective runtime'][movieList.index(
            movie)]
        for j in range(0, effectiveRuntime):
            moviePath = 'C:\\Users\\hasna\\Desktop\\Smell of Fear\\Movie Intervals\\' + movie + '\\' + movie + '_' + str(
                j) + '.mp4'
            video_manager = VideoManager([moviePath])
            stats_manager = StatsManager()
            scene_manager = SceneManager(stats_manager)
            scene_manager.add_detector(ContentDetector(40))
            #Set downscale factor to improve processing speed
            video_manager.set_downscale_factor()
            #Start video_manager.
            video_manager.start()
            #Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager)
            scene_list = scene_manager.get_scene_list(
                video_manager.get_base_timecode())
            aslList.append(len(scene_list))
            video_manager.release()
        #save the ASL List
        savePath = 'C:\\Users\\hasna\\Desktop\\Smell of Fear\\Pickle Objects\\ASL\\' + movie + '.p'
        pickle.dump(aslList, open(savePath, "wb"))
def videodataset_scenedetect(dirpath):
    from scenedetect.video_manager import VideoManager
    from scenedetect.scene_manager import SceneManager
    from scenedetect.stats_manager import StatsManager
    from scenedetect.detectors import ContentDetector
    from scenedetect.detectors import ThresholdDetector
    result_list = []
    for video_name in sorted(os.listdir(dirpath)):
        video_path = os.path.join(dirpath, video_name)
        print(video_path)
        video_info = get_video_info(video_path)
        # print (video_info)
        total_frames = int(video_info['duration_ts'])
        print('总帧数:' + str(total_frames))
        # 创建一个video_manager指向视频文件
        video_manager = VideoManager([video_path])
        stats_manager = StatsManager()
        scene_manager = SceneManager(stats_manager)
        # #添加ContentDetector算法(构造函数采用阈值等检测器选项)
        scene_manager.add_detector(ContentDetector(threshold=20))
        base_timecode = video_manager.get_base_timecode()

        try:
            frames_num = total_frames
            # 设置缩减系数以提高处理速度。
            video_manager.set_downscale_factor()
            # 启动 video_manager.
            video_manager.start()
            # 在video_manager上执行场景检测。
            scene_manager.detect_scenes(frame_source=video_manager)

            # 获取检测到的场景列表。
            scene_list = scene_manager.get_scene_list(base_timecode)
            # 与FrameTimecodes一样,如果是,则可以对scene_list中的每个场景进行排序
            # 场景列表变为未排序。
            # print('List of scenes obtained:')
            # print(scene_list)
            # 如果scene_list不为空,整理结果列表,否则,视频为单场景
            re_scene_list = []
            if scene_list:
                for i, scene in enumerate(scene_list):
                    # print('%d , %d' % (scene[0].get_frames(), scene[1].get_frames()))
                    re_scene = (scene[0].get_frames(),
                                scene[1].get_frames() - 1)
                    re_scene_list.append(re_scene)
            else:
                re_scene = (0, frames_num - 1)
                re_scene_list.append(re_scene)
            # 输出切分场景的列表结果
            print("\n")
            print(re_scene_list)
            print("\n")
            # for item in re_scene_list:
            #     assert item[1] - item[0] + 1 >= 3
            result_list.append(re_scene_list)
        finally:
            video_manager.release()
    with open(os.path.join(dirpath, 'scene.pickle'), 'wb') as f:
        pickle.dump(result_list, f)
Exemple #12
0
def find_scenes(video_path):
    """
    based on changes between frames in the HSV color space
    """
    # three main calsses: VideoManager, SceneManager, StatsManager
    video_manager = VideoManager([video_path])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (each detector's constructor
    # takes detector options, e.g. threshold).
    scene_manager.add_detector(ContentDetector())

    base_timecode = video_manager.get_base_timecode()

    # We save our stats file to {VIDEO_PATH}.stats.csv.
    stats_file_path = '%s.stats.csv' % video_path

    scene_list = []

    try:
        # If stats file exists, load it.
        if os.path.exists(stats_file_path):
            # Read stats from CSV file opened in read mode:
            with open(stats_file_path, 'r') as stats_file:
                stats_manager.load_from_csv(stats_file, base_timecode)

        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        # Each scene is a tuple of (start, end) FrameTimecodes.

        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            print('Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))

        # We only write to the stats file if a save is required:
        if stats_manager.is_save_required():
            with open(stats_file_path, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)

    finally:
        video_manager.release()

    return scene_list
def test_video_open_failure():
    """ Test VideoManager constructor (__init__ method) with invalid filename(s)
    and device IDs to trigger an IOError/VideoOpenFailure exception. """
    # Attempt to open non-existing video files should raise an IOError.
    with pytest.raises(IOError): VideoManager(['fauxfile.mp4'])
    with pytest.raises(IOError): VideoManager(['fauxfile.mp4', 'otherfakefile.mp4'])
    # Attempt to open 99th video device should raise a VideoOpenFailure since
    # the OpenCV VideoCapture open() method will likely fail (unless the test
    # case computer has 100 webcams or more...)
    with pytest.raises(VideoOpenFailure): VideoManager([99])
    # Test device IDs > 100.
    with pytest.raises(VideoOpenFailure): VideoManager([120])
    with pytest.raises(VideoOpenFailure): VideoManager([255])
def test_video_params(test_video_file):
    """ Test VideoManager get_framerate/get_framesize methods on test_video_file. """
    try:
        cap = cv2.VideoCapture(test_video_file)
        video_manager = VideoManager([test_video_file] * 2)
        assert cap.isOpened()
        assert video_manager.get_framerate() == pytest.approx(cap.get(cv2.CAP_PROP_FPS))
        assert video_manager.get_framesize() == (
            pytest.approx(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            pytest.approx(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    finally:
        cap.release()
        video_manager.release()
Exemple #15
0
def detect(fnm):
    # Create a video_manager point to video file testvideo.mp4. Note that multiple
    # videos can be appended by simply specifying more file paths in the list
    # passed to the VideoManager constructor. Note that appending multiple videos
    # requires that they all have the same frame size, and optionally, framerate.
    video_manager = VideoManager([fnm])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        # If stats file exists, load it.
        #if os.path.exists(STATS_FILE_PATH):
        # Read stats from CSV file opened in read mode:
        #    with open(STATS_FILE_PATH, 'r') as stats_file:
        #        stats_manager.load_from_csv(stats_file, base_timecode)

        start_time = base_timecode + 20  # 00:00:00.667
        end_time = base_timecode + 20.0  # 00:00:20.000
        # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
        #video_manager.set_duration(start_time=start_time, end_time=end_time)

        # Set downscale factor to improve processing speed (no args means default).
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)  #,
        #start_time=start_time)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        # Like FrameTimecodes, each scene in the scene_list can be sorted if the
        # list of scenes becomes unsorted.

        ls = []
        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            ls.append((scene[0].get_frames(), scene[1].get_frames() - 1))
            #print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
            #    i+1,
            #    scene[0].get_timecode(), scene[0].get_frames(),
            #    scene[1].get_timecode(), scene[1].get_frames(),))

        # We only write to the stats file if a save is required:
        #if stats_manager.is_save_required():
        #    with open(STATS_FILE_PATH, 'w') as stats_file:
        #        stats_manager.save_to_csv(stats_file, base_timecode)

    finally:
        video_manager.release()
    return ls
def test_start_release(test_video_file):
    """ Test VideoManager start/release methods on 3 appended videos. """
    video_manager = VideoManager([test_video_file] * 2)
    # *must* call release() after start() or video manager process will be rogue.
    #
    # The start method is the only big usage differences between the
    # VideoManager and cv2.VideoCapture objects from the point of view
    # of a SceneManager (the other VideoManager methods function
    # independently of it's job as a frame source).
    try:
        video_manager.start()
        # even if exception thrown here, video manager process will stop.
    finally:
        video_manager.release()
Exemple #17
0
def find_scenes(video_path):
    # type: (str) -> List[Tuple[FrameTimecode, FrameTimecode]]

    url = "https://www.youtube.com/watch?v=BGLTLitLUAo"
    videoPafy = pafy.new(url)
    best = videoPafy.getbest(preftype="webm")

    video = cv2.VideoCapture(best.url)

    video_manager = VideoManager([video])

    # Construct our SceneManager and pass it our StatsManager.
    scene_manager = SceneManager()

    # Add ContentDetector algorithm (each detector's constructor
    # takes detector options, e.g. threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    scene_list = []

    try:

        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        #scene_manager.detect_scenes(frame_source=video_manager)
        scene_manager.detect_scenes(frame_source=video_manager)
        #vcap =

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)

        timecodes = []
        # Each scene is a tuple of (start, end) FrameTimecodes.

        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            timecodes.append(scene[0].get_timecode())
            print('Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))

    finally:
        video_manager.release()

    return timecodes
Exemple #18
0
def test_read(test_video_file):
    """ Test VideoManager read method. """
    video_manager = VideoManager([test_video_file] * 2)
    base_timecode = video_manager.get_base_timecode()
    try:
        video_manager.start()
        assert video_manager.get_current_timecode() == base_timecode
        for i in range(1, 10):
            # VideoManager.read() -> Tuple[bool, numpy.ndarray]
            ret_val, frame_image = video_manager.read()
            assert ret_val
            assert frame_image.shape[0] > 0
            assert video_manager.get_current_timecode() == base_timecode + i
    finally:
        video_manager.release()
Exemple #19
0
    def scenedetect(self, video: Path):
        # Skip scene detection if the user choosed to
        if self.skip_scenes:
            return ''

        try:

            # PySceneDetect used split video by scenes and pass it to encoder
            # Optimal threshold settings 15-50

            video_manager = VideoManager([str(video)])
            scene_manager = SceneManager()
            scene_manager.add_detector(
                ContentDetector(threshold=self.threshold))
            base_timecode = video_manager.get_base_timecode()

            # If stats file exists, load it.
            if self.scenes and self.scenes.exists():
                # Read stats from CSV file opened in read mode:
                with self.scenes.open() as stats_file:
                    stats = stats_file.read()
                    return stats

            # Work on whole video
            video_manager.set_duration()

            # Set downscale factor to improve processing speed.
            video_manager.set_downscale_factor()

            # Start video_manager.
            video_manager.start()

            # Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager,
                                        show_progress=True)

            # Obtain list of detected scenes.
            scene_list = scene_manager.get_scene_list(base_timecode)
            # Like FrameTimecodes, each scene in the scene_list can be sorted if the
            # list of scenes becomes unsorted.

            scenes = [scene[0].get_timecode() for scene in scene_list]
            scenes = ','.join(scenes[1:])

            # We only write to the stats file if a save is required:
            if self.scenes:
                self.scenes.write_text(scenes)
            return scenes

        except Exception:

            print('Error in PySceneDetect')
            sys.exit()
Exemple #20
0
def frameExtraction(video, method, treshold):

    video_manager = VideoManager([video])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    if method == 'ContentDetector':
        scene_manager.add_detector(ContentDetector(treshold))
    if method == 'threshold_detector':
        scene_manager.add_detector(ThresholdDetector(treshold))

    base_timecode = video_manager.get_base_timecode()

    # Set downscale factor to improve processing speed (no args means default).
    video_manager.set_downscale_factor()

    # Start video_manager.
    video_manager.start()

    # Perform scene detection on video_manager.
    scene_manager.detect_scenes(frame_source=video_manager)

    # Obtain list of detected scenes.
    scene_list = scene_manager.get_scene_list(base_timecode)

    cap = cv2.VideoCapture(video)

    for i, scene in enumerate(scene_list):
        i = i + 1
        cut_frame = scene[0].get_frames()
        cap.set(1, cut_frame)
        ret, frame = cap.read()
        frame_name = "shot " + str(i) + ".jpg"
        cv2.imwrite(frame_name, frame)
def find_shots(video_path, stats_file, threshold):
    video_manager = VideoManager([video_path])
    stats_manager = StatsManager()
    # Construct our SceneManager and pass it our StatsManager.
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (each detector's constructor
    # takes detector options, e.g. threshold).
    scene_manager.add_detector(ContentDetector(threshold=threshold))
    base_timecode = video_manager.get_base_timecode()

    scene_list = []

    try:
        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)

        # Each scene is a tuple of (start, end) FrameTimecodes.
        print('List of shots obtained:')
        for i, scene in enumerate(scene_list):
            print('Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))

        # Save a list of stats to a csv
        if stats_manager.is_save_required():
            with open(stats_file, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)
    except Exception as err:
        print(
            "Failed to find shots for: video: " + video_path + ", stats: " +
            stats_file + ", threshold: " + threshold, err)
        traceback.print_exc()
    finally:
        video_manager.release()

    return scene_list
    def scenedetect(self, video):
        # PySceneDetect used split video by scenes and pass it to encoder
        # Optimal threshold settings 15-50
        video_manager = VideoManager([video])
        scene_manager = SceneManager()
        scene_manager.add_detector(ContentDetector(threshold=self.threshold))
        base_timecode = video_manager.get_base_timecode()

        try:
            # If stats file exists, load it.
            if self.scenes and os.path.exists(join(self.here, self.scenes)):
                # Read stats from CSV file opened in read mode:
                with open(join(self.here, self.scenes), 'r') as stats_file:
                    stats = stats_file.read()
                    return stats

            # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
            video_manager.set_duration()

            # Set downscale factor to improve processing speed.
            video_manager.set_downscale_factor()

            # Start video_manager.
            video_manager.start()

            # Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager)

            # Obtain list of detected scenes.
            scene_list = scene_manager.get_scene_list(base_timecode)
            # Like FrameTimecodes, each scene in the scene_list can be sorted if the
            # list of scenes becomes unsorted.

            scenes = []
            for i, scene in enumerate(scene_list):
                scenes.append(scene[0].get_timecode())

            scenes = ','.join(scenes[1:])

            # We only write to the stats file if a save is required:
            if self.scenes:
                with open(join(self.here, self.scenes), 'w') as stats_file:
                    stats_file.write(scenes)
                    return scenes
            else:
                return scenes
        except:
            print('Error in PySceneDetect')
            sys.exit()
Exemple #23
0
def detect_scenes(video_pth: Path,
                  scene_detection_threshold: int,
                  min_scene_length: int,
                  frame_skip: Optional[int],
                  show_progress: bool = False):
    """
    Detect scenes in a video
    :param video_pth: Path to video file to process
    :param scene_detection_threshold: Threshold for detection of change in scene
    :param min_scene_length: Minimum allowed length of scene, in frames
    :param frame_skip:  Number of frames to skip, peeding up the detection time at the expense of accuracy
    :param show_progress: If true, will show a tqdm progress bar of scene detection
    :return:
    """
    time_start = time.time()
    logger.info(
        f"Starting detect_scene with args: \n"
        f"{video_pth=}\n{scene_detection_threshold=}\n{min_scene_length=}\n{frame_skip=}"
    )
    video_manager = VideoManager([str(video_pth)])
    scene_manager = SceneManager()
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(
        ContentDetector(threshold=scene_detection_threshold,
                        min_scene_len=min_scene_length))
    base_timecode = video_manager.get_base_timecode()

    try:
        video_manager.set_downscale_factor()  # no args means default
        video_manager.start()

        scene_manager.detect_scenes(frame_source=video_manager,
                                    frame_skip=frame_skip,
                                    show_progress=show_progress)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        time_taken = time.time() - time_start
        logger.info(
            f"Detection of scenes complete in {round(time_taken, 1)} seconds")
        logger.info('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            logger.info(
                f'    Scene {i + 1}: Start {scene[0].get_timecode()} / Frame {scene[0].get_frames()}, '
                f'End {scene[1].get_timecode()} / Frame {scene[1].get_frames()}'
            )
        return scene_list
    finally:
        video_manager.release()
    def run(self):
        self.logger.info('run(): started')
        self.logger.info("Running PySceneDetect API test...")
        self.logger.info("PySceneDetect version being used: %s" %
                         str(scenedetect.__version__))

        # Create a video_manager point to video file testvideo.mp4
        video_manager = VideoManager([self.video_filename])
        stats_manager = StatsManager()
        scene_manager = SceneManager(stats_manager)
        # Add ContentDetector algorithm (constructor takes detector options like threshold).
        scene_manager.add_detector(ContentDetector())
        base_timecode = video_manager.get_base_timecode()
        print('base_timecode', base_timecode)

        try:
            # If stats file exists, load it.
            if os.path.exists(self.stats_filename):
                # Read stats from CSV file opened in read mode:
                with open(self.stats_filename, 'r') as stats_file:
                    stats_manager.load_from_csv(stats_file, base_timecode)

            # Set video_manager duration
            start_time = base_timecode

            # Set downscale factor to improve processing speed.
            video_manager.set_downscale_factor()

            # Start video_manager.
            video_manager.start()

            # Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager,
                                        start_time=start_time)

            # Obtain list of detected scenes.
            self.scene_list = scene_manager.get_scene_list(base_timecode)
            # Like FrameTimecodes, each scene in the scene_list can be sorted if the
            # list of scenes becomes unsorted.
            """
      # We only write to the stats file if a save is required:
      if stats_manager.is_save_required():
        with open(self.stats_filename, 'w') as stats_file:
          stats_manager.save_to_csv(stats_file, base_timecode)
      """

        finally:
            video_manager.release()
            self.logger.info('run(): finished')
Exemple #25
0
def test_get_property(test_video_file):
    """ Test VideoManager get method on test_video_file. """
    video_manager = VideoManager([test_video_file] * 3)
    video_framerate = video_manager.get_framerate()
    assert video_manager.get(
        cv2.CAP_PROP_FPS) == pytest.approx(video_framerate)
    assert video_manager.get(cv2.CAP_PROP_FPS,
                             1) == pytest.approx(video_framerate)
    assert video_manager.get(cv2.CAP_PROP_FPS,
                             2) == pytest.approx(video_framerate)
    video_manager.release()
    def detect_scenes(self, video_id):
        if video_id in self.cache:
            return self.cache[video_id]
        scenes_file_path = self._get_scenes_path(video_id)
        if exists(scenes_file_path):
            print('loading scenes from ', scenes_file_path)
            with open(scenes_file_path, 'rb') as f:
                scenes = pickle.load(f)
            self.cache[video_id] = scenes
            return scenes

        print('Detecting scenes for {}'.format(video_id))

        stats_file_path = self._get_stats_path(video_id)

        video_manager = VideoManager([get_video_path(video_id)])
        stats_manager = StatsManager()
        scene_manager = SceneManager(stats_manager)
        scene_manager.add_detector(self._create_detector())
        base_timecode = video_manager.get_base_timecode()

        try:
            if exists(stats_file_path):
                with open(stats_file_path, 'r') as stats_file:
                    stats_manager.load_from_csv(stats_file, base_timecode)

            # Set downscale factor to improve processing speed (no args means default).
            video_manager.set_downscale_factor()

            video_manager.start()
            scene_manager.detect_scenes(frame_source=video_manager)
            scenes_list = scene_manager.get_scene_list(base_timecode)
            scenes = [(scene[0].get_seconds(), scene[1].get_seconds())
                      for scene in scenes_list]
            self.cache[video_id] = scenes
            if self.save_scenes:
                scenes_file_path = self._get_scenes_path(video_id)
                print('saving scenes to ', scenes_file_path)
                with open(scenes_file_path, 'wb') as f:
                    pickle.dump(scenes, f)

            # We only write to the stats file if a save is required:
            if stats_manager.is_save_required():
                with open(stats_file_path, 'w') as stats_file:
                    stats_manager.save_to_csv(stats_file, base_timecode)

            return self.cache[video_id]
        finally:
            video_manager.release()
def test_read(test_video_file):
    """ Test VideoManager read method. """
    video_manager = VideoManager([test_video_file] * 2)
    base_timecode = video_manager.get_base_timecode()
    try:
        video_manager.start()
        assert video_manager.get_current_timecode() == base_timecode
        for i in range(1, 10):
            # VideoManager.read() -> Tuple[bool, numpy.ndarray]
            ret_val, frame_image = video_manager.read()
            assert ret_val
            assert frame_image.shape[0] > 0
            assert video_manager.get_current_timecode() == base_timecode + i
    finally:
        video_manager.release()
Exemple #28
0
 def get_scene_frames(self, video_file_path):
     scene_list = self._get_frames_timecodes(video_file_path)
     video_manager = VideoManager([video_file_path])
     video_manager.start()
     for frame_timecode, _ in tqdm(scene_list):
         video_manager.seek(frame_timecode)
         _, frame = video_manager.read()
         yield frame
Exemple #29
0
 def _get_frames_timecodes(self, video_file_path):
     video_manager = VideoManager([video_file_path])
     base_timecode = video_manager.get_base_timecode()
     video_manager.set_downscale_factor()
     video_manager.start()
     self.scene_manager.detect_scenes(frame_source=video_manager)
     scene_list = self.scene_manager.get_scene_list(base_timecode)
     return scene_list
Exemple #30
0
def sceneDetect(inFile, scenesFile):
    from scenedetect.video_manager import VideoManager
    from scenedetect.stats_manager import StatsManager
    from scenedetect.scene_manager import SceneManager
    from scenedetect.detectors import ContentDetector

    videoManager = VideoManager([str(inFile)])
    statsManager = StatsManager()
    sceneManager = SceneManager(statsManager)
    sceneManager.add_detector(ContentDetector())
    baseTimecode = videoManager.get_base_timecode()

    videoManager.start()
    sceneManager.detect_scenes(frame_source=videoManager)
    sceneList = sceneManager.get_scene_list(baseTimecode)

    splitList = [str(scene[0].get_frames()) for scene in sceneList][1:]

    return splitList
def detect_scenes(filepath):
    video_manager = VideoManager([filepath])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector(threshold=40, min_scene_len=30))
    base_timecode = video_manager.get_base_timecode()

    STATS_FILE_PATH = f"{filepath.split('/')[-1]}.stats.csv"

    try:
        # If stats file exists, load it.
        if os.path.exists(STATS_FILE_PATH):
            # Read stats from CSV file opened in read mode:
            with open(STATS_FILE_PATH, 'r') as stats_file:
                stats_manager.load_from_csv(stats_file, base_timecode)

        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        # Like FrameTimecodes, each scene in the scene_list can be sorted if the
        # list of scenes becomes unsorted.

        # We only write to the stats file if a save is required:
        if stats_manager.is_save_required():
            with open(STATS_FILE_PATH, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)

        # print('List of scenes obtained:')
        # for i, scene in enumerate(scene_list):
        #     print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
        #         i+1,
        #         scene[0].get_timecode(), scene[0].get_frames(),
        #         scene[1].get_timecode(), scene[1].get_frames(),))

        return scene_list

    finally:
        video_manager.release()
Exemple #32
0
    def _init_video_manager(self, input_list, framerate, downscale):

        self.base_timecode = None

        logging.debug('Initializing VideoManager.')
        video_manager_initialized = False
        try:
            self.video_manager = VideoManager(
                video_files=input_list, framerate=framerate, logger=logging)
            video_manager_initialized = True
            self.base_timecode = self.video_manager.get_base_timecode()
            self.video_manager.set_downscale_factor(downscale)
        except VideoOpenFailure as ex:
            error_strs = [
                'could not open video%s.' % get_plural(ex.file_list),
                'Failed to open the following video file%s:' % get_plural(ex.file_list)]
            error_strs += ['  %s' % file_name[0] for file_name in ex.file_list]
            dll_okay, dll_name = check_opencv_ffmpeg_dll()
            if not dll_okay:
                error_strs += [
                    'Error: OpenCV dependency %s not found.' % dll_name,
                    'Ensure that you installed the Python OpenCV module, and that the',
                    '%s file can be found to enable video support.' % dll_name]
            logging.debug('\n'.join(error_strs[1:]))
            if not dll_okay:
                click.echo(click.style(
                    '\nOpenCV dependency missing, video input/decoding not available.\n', fg='red'))
            raise click.BadParameter('\n'.join(error_strs), param_hint='input video')
        except VideoFramerateUnavailable as ex:
            error_strs = ['could not get framerate from video(s)',
                          'Failed to obtain framerate for video file %s.' % ex.file_name]
            error_strs.append('Specify framerate manually with the -f / --framerate option.')
            logging.debug('\n'.join(error_strs))
            raise click.BadParameter('\n'.join(error_strs), param_hint='input video')
        except VideoParameterMismatch as ex:
            error_strs = ['video parameters do not match.', 'List of mismatched parameters:']
            for param in ex.file_list:
                if param[0] == cv2.CAP_PROP_FPS:
                    param_name = 'FPS'
                if param[0] == cv2.CAP_PROP_FRAME_WIDTH:
                    param_name = 'Frame width'
                if param[0] == cv2.CAP_PROP_FRAME_HEIGHT:
                    param_name = 'Frame height'
                error_strs.append('  %s mismatch in video %s (got %.2f, expected %.2f)' % (
                    param_name, param[3], param[1], param[2]))
            error_strs.append(
                'Multiple videos may only be specified if they have the same framerate and'
                ' resolution. -f / --framerate may be specified to override the framerate.')
            logging.debug('\n'.join(error_strs))
            raise click.BadParameter('\n'.join(error_strs), param_hint='input videos')
        except InvalidDownscaleFactor as ex:
            error_strs = ['Downscale value is not > 0.', str(ex)]
            logging.debug('\n'.join(error_strs))
            raise click.BadParameter('\n'.join(error_strs), param_hint='downscale factor')
        return video_manager_initialized
class Shot_Detector(object):
    '''
    This is for detect shot in videos. In other words, the frames in each shot are very similar.
    '''
    def __init__(self):
        '''
        Init the detection manager.
        '''

        print("Init the shot detector")
        print("PySceneDetect version being used: %s" %
              str(scenedetect.__version__))
        self.stats_manager = StatsManager()
        self.scene_manager = SceneManager(self.stats_manager)
        self.scene_manager.add_detector(ContentDetector())

    def detect(self, video_path):
        '''

        :param video_path:

        :return: the boundary of each shot:
        '''
        if not os.path.exists(video_path):
            print('The video does not exist!')

        self.video_manager = VideoManager([video_path])
        self.base_timecode = self.video_manager.get_base_timecode()
        self.video_manager.set_downscale_factor()
        self.video_manager.start()
        self.scene_manager.detect_scenes(frame_source=self.video_manager)

        scene_list = self.scene_manager.get_scene_list(self.base_timecode)

        split_frames = [0]
        split_time = [0]
        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))
            split_frames.append(scene[1].get_frames())
            split_time.append(self.__time_trans(scene[1].get_timecode()))
        self.video_manager.release()

        return split_frames, split_time

    def __time_trans(self, second_time):
        # TODO better time transform
        temp = datetime.strptime(second_time, '%H:%M:%S.%f')
        milliseconds = (temp -
                        datetime(1900, 1, 1)) // timedelta(milliseconds=1)
        return milliseconds
def test_get_aspect_ratio(test_video_file):
    """ Test get_aspect_ratio function. """
    expected_value = 1.0
    epsilon = 0.01

    video_manager = VideoManager([test_video_file])
    assert abs(get_aspect_ratio(video_manager) - expected_value) < epsilon

    # Ensure non-open VideoCapture returns 1.0.
    blank_cap = cv2.VideoCapture()
    assert abs(get_aspect_ratio(blank_cap) - expected_value) < epsilon
def test_content_detect(test_video_file):
    """ Test SceneManager with VideoManager and ContentDetector. """
    vm = VideoManager([test_video_file])
    sm = SceneManager()
    sm.add_detector(ContentDetector())

    try:
        video_fps = vm.get_framerate()
        start_time = FrameTimecode('00:00:00', video_fps)
        end_time = FrameTimecode('00:00:05', video_fps)

        vm.set_duration(start_time=start_time, end_time=end_time)
        vm.set_downscale_factor()

        vm.start()
        num_frames = sm.detect_scenes(frame_source=vm)
        assert num_frames == end_time.get_frames() + 1

    finally:
        vm.release()
Exemple #36
0
def sVideoEkle(request):
    result = []
    STATS_FILE_PATH = 'G:/dddddd/Django-2.1.15/django/bin/ilkProjem/ilkProjem/static/denemevideolar/testvideo2.stats.csv'
    if request.method == 'POST' and request.FILES['videoName']:
        myfile = request.FILES['videoName']
        fs = FileSystemStorage(
            location=
            'G:/dddddd/Django-2.1.15/django/bin/ilkProjem/ilkProjem/static/denemevideolar/'
        )
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = "G:/dddddd/Django-2.1.15/django/bin/ilkProjem/ilkProjem/static/denemevideolar/" + filename
        result.append([uploaded_file_url])
        video_manager = VideoManager(uploaded_file_url)
        """
        
        stats_manager = StatsManager()
        scene_manager = SceneManager(stats_manager)
        # Add ContentDetector algorithm (constructor takes detector options like threshold).
        scene_manager.add_detector(ContentDetector())
        base_timecode = video_manager.get_base_timecode()
        start_time = base_timecode + 20     # 00:00:00.667
        end_time = base_timecode + 20.0     # 00:00:20.000
            # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
        video_manager.set_duration(start_time=start_time, end_time=end_time)

            # Set downscale factor to improve processing speed (no args means default).
        video_manager.set_downscale_factor()

            # Start video_manager.
        video_manager.start()

            # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

            # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
            # Like FrameTimecodes, each scene in the scene_list can be sorted if the
            # list of scenes becomes unsorted.

        for i, scene in enumerate(scene_list):
            result.append('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i+1,
                scene[0].get_timecode(), scene[0].get_frames(),
                scene[1].get_timecode(), scene[1].get_frames(),))

            # We only write to the stats file if a save is required:
        if stats_manager.is_save_required():
            with open(STATS_FILE_PATH, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)
    """
    if request.method == 'POST':
        return render(request, 'sVideoSonuc.html', locals())
    return render(request, 'sVideoEkle.html', locals())
def test_get_property(test_video_file):
    """ Test VideoManager get method on test_video_file. """
    video_manager = VideoManager([test_video_file] * 3)
    video_framerate = video_manager.get_framerate()
    assert video_manager.get(cv2.CAP_PROP_FPS) == pytest.approx(video_framerate)
    assert video_manager.get(cv2.CAP_PROP_FPS, 1) == pytest.approx(video_framerate)
    assert video_manager.get(cv2.CAP_PROP_FPS, 2) == pytest.approx(video_framerate)
    video_manager.release()
def test_detector_metrics(test_video_file):
    """ Test passing StatsManager to a SceneManager and using it for storing the frame metrics
    from a ContentDetector.
    """
    video_manager = VideoManager([test_video_file])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    #base_timecode = video_manager.get_base_timecode()

    assert not stats_manager._registered_metrics
    scene_manager.add_detector(ContentDetector())
    # add_detector should trigger register_metrics in the StatsManager.
    assert stats_manager._registered_metrics

    try:
        video_fps = video_manager.get_framerate()
        start_time = FrameTimecode('00:00:00', video_fps)
        duration = FrameTimecode('00:00:20', video_fps)

        video_manager.set_duration(start_time=start_time, end_time=duration)
        video_manager.set_downscale_factor()
        video_manager.start()
        scene_manager.detect_scenes(frame_source=video_manager)

        # Check that metrics were written to the StatsManager.
        assert stats_manager._frame_metrics
        frame_key = min(stats_manager._frame_metrics.keys())
        assert stats_manager._frame_metrics[frame_key]
        assert stats_manager.metrics_exist(frame_key, list(stats_manager._registered_metrics))

        # Since we only added 1 detector, the number of metrics from get_metrics
        # should equal the number of metric keys in _registered_metrics.
        assert len(stats_manager.get_metrics(
            frame_key, list(stats_manager._registered_metrics))) == len(
                stats_manager._registered_metrics)

    finally:
        video_manager.release()
def test_multiple_videos(test_video_file):
    """ Test VideoManager handling decoding frames across video boundaries. """

    NUM_FRAMES = 10
    NUM_VIDEOS = 3
    # Open VideoManager and get base timecode.
    video_manager = VideoManager([test_video_file] * NUM_VIDEOS)
    base_timecode = video_manager.get_base_timecode()

    # List of NUM_VIDEOS VideoManagers pointing to test_video_file.
    vm_list = [
        VideoManager([test_video_file]),
        VideoManager([test_video_file]),
        VideoManager([test_video_file])]

    # Set duration of all VideoManagers in vm_list to NUM_FRAMES frames.
    for vm in vm_list: vm.set_duration(duration=base_timecode+NUM_FRAMES)
    # (FOR TESTING PURPOSES ONLY) Manually override _cap_list with the
    # duration-limited VideoManager objects in vm_list
    video_manager._cap_list = vm_list

    try:
        for vm in vm_list: vm.start()
        video_manager.start()
        assert video_manager.get_current_timecode() == base_timecode

        curr_time = video_manager.get_base_timecode()
        while True:
            ret_val, frame_image = video_manager.read()
            if not ret_val:
                break
            assert frame_image.shape[0] > 0
            curr_time += 1
        assert curr_time == base_timecode + ((NUM_FRAMES+1) * NUM_VIDEOS)

    finally:
        # Will release the VideoManagers in vm_list as well.
        video_manager.release()
def test_scene_list(test_video_file):
    """ Test SceneManager get_scene_list method with VideoManager/ContentDetector. """
    vm = VideoManager([test_video_file])
    sm = SceneManager()
    sm.add_detector(ContentDetector())

    try:
        base_timecode = vm.get_base_timecode()
        video_fps = vm.get_framerate()
        start_time = FrameTimecode('00:00:00', video_fps)
        end_time = FrameTimecode('00:00:10', video_fps)

        vm.set_duration(start_time=start_time, end_time=end_time)
        vm.set_downscale_factor()

        vm.start()
        num_frames = sm.detect_scenes(frame_source=vm)

        assert num_frames == end_time.get_frames() + 1

        scene_list = sm.get_scene_list(base_timecode)

        for i, _ in enumerate(scene_list):
            if i > 0:
                # Ensure frame list is sorted (i.e. end time frame of
                # one scene is equal to the start time of the next).
                assert scene_list[i-1][1] == scene_list[i][0]

    finally:
        vm.release()
def test_save_load_from_video(test_video_file):
    """ Test generating and saving some frame metrics from TEST_VIDEO_FILE to a file on disk, and
    loading the file back to ensure the loaded frame metrics agree with those that were saved.
    """
    video_manager = VideoManager([test_video_file])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)

    base_timecode = video_manager.get_base_timecode()

    scene_manager.add_detector(ContentDetector())

    try:
        video_fps = video_manager.get_framerate()
        start_time = FrameTimecode('00:00:00', video_fps)
        duration = FrameTimecode('00:00:20', video_fps)

        video_manager.set_duration(start_time=start_time, end_time=duration)
        video_manager.set_downscale_factor()
        video_manager.start()
        scene_manager.detect_scenes(frame_source=video_manager)

        with open(TEST_STATS_FILES[0], 'w') as stats_file:
            stats_manager.save_to_csv(stats_file, base_timecode)

        stats_manager_new = StatsManager()

        with open(TEST_STATS_FILES[0], 'r') as stats_file:
            stats_manager_new.load_from_csv(stats_file, base_timecode)

        # Choose the first available frame key and compare all metrics in both.
        frame_key = min(stats_manager._frame_metrics.keys())
        metric_keys = list(stats_manager._registered_metrics)

        assert stats_manager.metrics_exist(frame_key, metric_keys)
        orig_metrics = stats_manager.get_metrics(frame_key, metric_keys)
        new_metrics = stats_manager_new.get_metrics(frame_key, metric_keys)

        for i, metric_val in enumerate(orig_metrics):
            assert metric_val == pytest.approx(new_metrics[i])

    finally:
        os.remove(TEST_STATS_FILES[0])

        video_manager.release()
Exemple #42
0
class CliContext(object):
    """ Context of the command-line interface passed between the various sub-commands.

    Pools all options, processing the main program options as they come in (e.g. those
    not passed to a command), followed by parsing each sub-command's options, preparing
    the actions to be executed in the process_input() method, which is called after the
    whole command line has been processed (successfully nor not).

    This class and the cli.__init__ module make up the bulk of the PySceneDetect
    application logic for the command line.
    """

    def __init__(self):
        # Properties for main scenedetect command options (-i, -s, etc...) and CliContext logic.
        self.options_processed = False          # True when CLI option parsing is complete.
        self.scene_manager = None               # detect-content, detect-threshold, etc...
        self.video_manager = None               # -i/--input, -d/--downscale
        self.base_timecode = None               # -f/--framerate
        self.start_frame = 0                    # time -s/--start
        self.stats_manager = None               # -s/--stats
        self.stats_file_path = None             # -s/--stats
        self.output_directory = None            # -o/--output
        self.quiet_mode = False                 # -q/--quiet or -v/--verbosity quiet
        self.frame_skip = 0                     # -fs/--frame-skip
        # Properties for save-images command.
        self.save_images = False                # save-images command
        self.image_extension = 'jpg'            # save-images -j/--jpeg, -w/--webp, -p/--png
        self.image_directory = None             # save-images -o/--output

        self.image_param = None                 # save-images -q/--quality if -j/-w,
                                                #   -c/--compression if -p


        self.image_name_format = (              # save-images -f/--name-format
            '$VIDEO_NAME-Scene-$SCENE_NUMBER-$IMAGE_NUMBER')
        self.num_images = 2                     # save-images -n/--num-images
        self.imwrite_params = get_cv2_imwrite_params()
        # Properties for split-video command.
        self.split_video = False                # split-video command
        self.split_mkvmerge = False             # split-video -c/--copy
        self.split_args = None                  # split-video -a/--override-args
        self.split_directory = None             # split-video -o/--output
        self.split_name_format = '$VIDEO_NAME-Scene-$SCENE_NUMBER'  # split-video -f/--filename
        self.split_quiet = False                # split-video -q/--quiet
        # Properties for list-scenes command.
        self.list_scenes = False                # list-scenes command
        self.print_scene_list = False           # list-scenes --quiet/-q
        self.scene_list_directory = None        # list-scenes -o/--output
        self.scene_list_name_format = None      # list-scenes -f/--filename
        self.scene_list_output = False          # list-scenes -n/--no-output

        self.export_html = False                # export-html command
        self.html_name_format = None            # export-html -f/--filename
        self.html_include_images = True         # export-html --no-images
        self.image_filenames = None             # export-html used for embedding images
        self.image_width = None                 # export-html -w/--image-width
        self.image_height = None                # export-html -h/--image-height


    def cleanup(self):
        # type: () -> None
        """ Cleanup: Releases all resources acquired by the CliContext (esp. the VideoManager). """
        try:
            logging.debug('Cleaning up...\n\n')
        finally:
            if self.video_manager is not None:
                self.video_manager.release()


    def _generate_images(self, scene_list, video_name,
                         image_name_template='$VIDEO_NAME-Scene-$SCENE_NUMBER-$IMAGE_NUMBER',
                         output_dir=None):
        # type: (List[Tuple[FrameTimecode, FrameTimecode]) -> None

        if not scene_list:
            return
        if not self.options_processed:
            return
        if self.num_images <= 0:
            raise ValueError()
        self.check_input_open()

        imwrite_param = []
        if self.image_param is not None:
            imwrite_param = [self.imwrite_params[self.image_extension], self.image_param]

        # Reset video manager and downscale factor.
        self.video_manager.release()
        self.video_manager.reset()
        self.video_manager.set_downscale_factor(1)
        self.video_manager.start()

        # Setup flags and init progress bar if available.
        completed = True
        logging.info('Generating output images (%d per scene)...', self.num_images)
        progress_bar = None
        if tqdm and not self.quiet_mode:
            progress_bar = tqdm(
                total=len(scene_list) * self.num_images, unit='images')

        filename_template = Template(image_name_template)


        scene_num_format = '%0'
        scene_num_format += str(max(3, math.floor(math.log(len(scene_list), 10)) + 1)) + 'd'
        image_num_format = '%0'
        image_num_format += str(math.floor(math.log(self.num_images, 10)) + 2) + 'd'

        timecode_list = dict()
        self.image_filenames = dict()

        for i in range(len(scene_list)):
            timecode_list[i] = []
            self.image_filenames[i] = []

        if self.num_images == 1:
            for i, (start_time, end_time) in enumerate(scene_list):
                duration = end_time - start_time
                timecode_list[i].append(start_time + int(duration.get_frames() / 2))

        else:
            middle_images = self.num_images - 2
            for i, (start_time, end_time) in enumerate(scene_list):
                timecode_list[i].append(start_time)

                if middle_images > 0:
                    duration = (end_time.get_frames() - 1) - start_time.get_frames()
                    duration_increment = None
                    duration_increment = int(duration / (middle_images + 1))
                    for j in range(middle_images):
                        timecode_list[i].append(start_time + ((j+1) * duration_increment))

                # End FrameTimecode is always the same frame as the next scene's start_time
                # (one frame past the end), so we need to subtract 1 here.
                timecode_list[i].append(end_time - 1)

        for i in timecode_list:
            for j, image_timecode in enumerate(timecode_list[i]):
                self.video_manager.seek(image_timecode)
                self.video_manager.grab()
                ret_val, frame_im = self.video_manager.retrieve()
                if ret_val:
                    file_path = '%s.%s' % (filename_template.safe_substitute(
                        VIDEO_NAME=video_name,
                        SCENE_NUMBER=scene_num_format % (i + 1),
                        IMAGE_NUMBER=image_num_format % (j + 1)),
                                           self.image_extension)
                    self.image_filenames[i].append(file_path)
                    cv2.imwrite(
                        self.get_output_file_path(file_path,
                                                  output_dir=output_dir),
                        frame_im, imwrite_param)
                else:
                    completed = False
                    break
                if progress_bar:
                    progress_bar.update(1)

        if not completed:
            logging.error('Could not generate all output images.')


    def get_output_file_path(self, file_path, output_dir=None):
        # type: (str, Optional[str]) -> str
        """ Get Output File Path: Gets full path to output file passed as argument, in
        the specified global output directory (scenedetect -o/--output) if set, creating
        any required directories along the way.

        Arguments:
            file_path (str): File name to get path for.  If file_path is an absolute
                path (e.g. starts at a drive/root), no modification of the path
                is performed, only ensuring that all output directories are created.
            output_dir (Optional[str]): An optional output directory to override the
                global output directory option, if set.

        Returns:
            (str) Full path to output file suitable for writing.

        """
        if file_path is None:
            return None
        output_dir = self.output_directory if output_dir is None else output_dir
        # If an output directory is defined and the file path is a relative path, open
        # the file handle in the output directory instead of the working directory.
        if output_dir is not None and not os.path.isabs(file_path):
            file_path = os.path.join(output_dir, file_path)
        # Now that file_path is an absolute path, let's make sure all the directories
        # exist for us to start writing files there.
        try:
            os.makedirs(os.path.split(os.path.abspath(file_path))[0])
        except OSError:
            pass
        return file_path

    def _open_stats_file(self):

        if self.stats_manager is None:
            self.stats_manager = StatsManager()

        if self.stats_file_path is not None:
            if os.path.exists(self.stats_file_path):
                logging.info('Loading frame metrics from stats file: %s',
                             os.path.basename(self.stats_file_path))
                try:
                    with open(self.stats_file_path, 'rt') as stats_file:
                        self.stats_manager.load_from_csv(stats_file, self.base_timecode)
                except StatsFileCorrupt:
                    error_strs = [
                        'Could not load stats file.', 'Failed to parse stats file:',
                        'Could not load frame metrics from stats file - file is corrupt or not a'
                        ' valid PySceneDetect stats file. If the file exists, ensure that it is'
                        ' a valid stats file CSV, otherwise delete it and run PySceneDetect again'
                        ' to re-generate the stats file.']
                    logging.error('\n'.join(error_strs))
                    raise click.BadParameter(
                        '\n  Could not load given stats file, see above output for details.',
                        param_hint='input stats file')
                except StatsFileFramerateMismatch as ex:
                    error_strs = [
                        'could not load stats file.', 'Failed to parse stats file:',
                        'Framerate differs between stats file (%.2f FPS) and input'
                        ' video%s (%.2f FPS)' % (
                            ex.stats_file_fps,
                            's' if self.video_manager.get_num_videos() > 1 else '',
                            ex.base_timecode_fps),
                        'Ensure the correct stats file path was given, or delete and re-generate'
                        ' the stats file.']
                    logging.error('\n'.join(error_strs))
                    raise click.BadParameter(
                        'framerate differs between given stats file and input video(s).',
                        param_hint='input stats file')


    def process_input(self):
        # type: () -> None
        """ Process Input: Processes input video(s) and generates output as per CLI commands.

        Run after all command line options/sub-commands have been parsed.
        """
        logging.debug('Processing input...')
        if not self.options_processed:
            logging.debug('Skipping processing, CLI options were not parsed successfully.')
            return
        self.check_input_open()
        if not self.scene_manager.get_num_detectors() > 0:
            logging.error(
                'No scene detectors specified (detect-content, detect-threshold, etc...),\n'
                '  or failed to process all command line arguments.')
            return

        # Handle scene detection commands (detect-content, detect-threshold, etc...).
        self.video_manager.start()
        base_timecode = self.video_manager.get_base_timecode()

        start_time = time.time()
        logging.info('Detecting scenes...')

        num_frames = self.scene_manager.detect_scenes(
            frame_source=self.video_manager, frame_skip=self.frame_skip,
            show_progress=not self.quiet_mode)

        duration = time.time() - start_time
        logging.info('Processed %d frames in %.1f seconds (average %.2f FPS).',
                     num_frames, duration, float(num_frames)/duration)

        # Handle -s/--statsfile option.
        if self.stats_file_path is not None:
            if self.stats_manager.is_save_required():
                with open(self.stats_file_path, 'wt') as stats_file:
                    logging.info('Saving frame metrics to stats file: %s',
                                 os.path.basename(self.stats_file_path))
                    self.stats_manager.save_to_csv(
                        stats_file, base_timecode)
            else:
                logging.debug('No frame metrics updated, skipping update of the stats file.')

        # Get list of detected cuts and scenes from the SceneManager to generate the required output
        # files with based on the given commands (list-scenes, split-video, save-images, etc...).
        cut_list = self.scene_manager.get_cut_list(base_timecode)
        scene_list = self.scene_manager.get_scene_list(base_timecode)
        video_paths = self.video_manager.get_video_paths()
        video_name = os.path.basename(video_paths[0])
        if video_name.rfind('.') >= 0:
            video_name = video_name[:video_name.rfind('.')]

        # Ensure we don't divide by zero.
        if scene_list:
            logging.info('Detected %d scenes, average shot length %.1f seconds.',
                         len(scene_list),
                         sum([(end_time - start_time).get_seconds()
                              for start_time, end_time in scene_list]) / float(len(scene_list)))
        else:
            logging.info('No scenes detected.')

        # Handle list-scenes command.
        if self.scene_list_output:
            scene_list_filename = Template(self.scene_list_name_format).safe_substitute(
                VIDEO_NAME=video_name)
            if not scene_list_filename.lower().endswith('.csv'):
                scene_list_filename += '.csv'
            scene_list_path = self.get_output_file_path(
                scene_list_filename, self.scene_list_directory)
            logging.info('Writing scene list to CSV file:\n  %s', scene_list_path)
            with open(scene_list_path, 'wt') as scene_list_file:
                write_scene_list(scene_list_file, scene_list, cut_list)
        # Handle `list-scenes`.
        if self.print_scene_list:
            logging.info("""Scene List:
-----------------------------------------------------------------------
 | Scene # | Start Frame |  Start Time  |  End Frame  |   End Time   |
-----------------------------------------------------------------------
%s
-----------------------------------------------------------------------
""", '\n'.join(
    [' |  %5d  | %11d | %s | %11d | %s |' % (
        i+1,
        start_time.get_frames(), start_time.get_timecode(),
        end_time.get_frames(), end_time.get_timecode())
     for i, (start_time, end_time) in enumerate(scene_list)]))


        if cut_list:
            logging.info('Comma-separated timecode list:\n  %s',
                         ','.join([cut.get_timecode() for cut in cut_list]))

        # Handle save-images command.
        if self.save_images:
            self._generate_images(scene_list=scene_list, video_name=video_name,
                                  image_name_template=self.image_name_format,
                                  output_dir=self.image_directory)

        # Handle export-html command.
        if self.export_html:
            html_filename = Template(self.html_name_format).safe_substitute(
                VIDEO_NAME=video_name)
            if not html_filename.lower().endswith('.html'):
                html_filename += '.html'
            html_path = self.get_output_file_path(
                html_filename, self.image_directory)
            logging.info('Exporting to html file:\n %s:', html_path)
            if not self.html_include_images:
                self.image_filenames = None
            write_scene_list_html(html_path, scene_list, cut_list,
                                  image_filenames=self.image_filenames,
                                  image_width=self.image_width,
                                  image_height=self.image_height)

        # Handle split-video command.
        if self.split_video:
            # Add proper extension to filename template if required.
            dot_pos = self.split_name_format.rfind('.')
            if self.split_mkvmerge and not self.split_name_format.endswith('.mkv'):
                self.split_name_format += '.mkv'
            # Don't add if we find an extension between 2 and 4 characters
            elif not (dot_pos >= 0) or (
                    dot_pos >= 0 and not
                    ((len(self.split_name_format) - (dot_pos+1) <= 4 >= 2))):
                self.split_name_format += '.mp4'

            output_file_prefix = self.get_output_file_path(
                self.split_name_format, output_dir=self.split_directory)
            mkvmerge_available = is_mkvmerge_available()
            ffmpeg_available = is_ffmpeg_available()
            if mkvmerge_available and (self.split_mkvmerge or not ffmpeg_available):
                if not self.split_mkvmerge:
                    logging.warning(
                        'ffmpeg not found, falling back to fast copy mode (split-video -c/--copy).')
                split_video_mkvmerge(video_paths, scene_list, output_file_prefix, video_name,
                                     suppress_output=self.quiet_mode or self.split_quiet)
            elif ffmpeg_available:
                if self.split_mkvmerge:
                    logging.warning('mkvmerge not found, falling back to normal splitting'
                                    ' mode (split-video).')
                split_video_ffmpeg(video_paths, scene_list, output_file_prefix,
                                   video_name, arg_override=self.split_args,
                                   hide_progress=self.quiet_mode,
                                   suppress_output=self.quiet_mode or self.split_quiet)
            else:
                if not (mkvmerge_available or ffmpeg_available):
                    error_strs = ["ffmpeg/mkvmerge is required for split-video [-c/--copy]."]
                else:
                    error_strs = [
                        "{EXTERN_TOOL} is required for split-video{EXTRA_ARGS}.".format(
                            EXTERN_TOOL='mkvmerge' if self.split_mkvmerge else 'ffmpeg',
                            EXTRA_ARGS=' -c/--copy' if self.split_mkvmerge else '')]
                error_strs += ["Install one of the above tools to enable the split-video command."]
                error_str = '\n'.join(error_strs)
                logging.debug(error_str)
                raise click.BadParameter(error_str, param_hint='split-video')
            if scene_list:
                logging.info('Video splitting completed, individual scenes written to disk.')



    def check_input_open(self):
        # type: () -> None
        """ Check Input Open: Ensures that the CliContext's VideoManager was initialized,
        started, and at *least* one input video was successfully opened - otherwise, an
        exception is raised.

        Raises:
            click.BadParameter
        """
        if self.video_manager is None or not self.video_manager.get_num_videos() > 0:
            error_strs = ["No input video(s) specified.",
                          "Make sure '--input VIDEO' is specified at the start of the command."]
            error_str = '\n'.join(error_strs)
            logging.debug(error_str)
            raise click.BadParameter(error_str, param_hint='input video')


    def add_detector(self, detector):
        """ Add Detector: Adds a detection algorithm to the CliContext's SceneManager. """
        self.check_input_open()
        options_processed_orig = self.options_processed
        self.options_processed = False
        try:
            self.scene_manager.add_detector(detector)
        except scenedetect.stats_manager.FrameMetricRegistered:
            raise click.BadParameter(message='Cannot specify detection algorithm twice.',
                                     param_hint=detector.cli_name)
        self.options_processed = options_processed_orig


    def _init_video_manager(self, input_list, framerate, downscale):

        self.base_timecode = None

        logging.debug('Initializing VideoManager.')
        video_manager_initialized = False
        try:
            self.video_manager = VideoManager(
                video_files=input_list, framerate=framerate, logger=logging)
            video_manager_initialized = True
            self.base_timecode = self.video_manager.get_base_timecode()
            self.video_manager.set_downscale_factor(downscale)
        except VideoOpenFailure as ex:
            error_strs = [
                'could not open video%s.' % get_plural(ex.file_list),
                'Failed to open the following video file%s:' % get_plural(ex.file_list)]
            error_strs += ['  %s' % file_name[0] for file_name in ex.file_list]
            dll_okay, dll_name = check_opencv_ffmpeg_dll()
            if not dll_okay:
                error_strs += [
                    'Error: OpenCV dependency %s not found.' % dll_name,
                    'Ensure that you installed the Python OpenCV module, and that the',
                    '%s file can be found to enable video support.' % dll_name]
            logging.debug('\n'.join(error_strs[1:]))
            if not dll_okay:
                click.echo(click.style(
                    '\nOpenCV dependency missing, video input/decoding not available.\n', fg='red'))
            raise click.BadParameter('\n'.join(error_strs), param_hint='input video')
        except VideoFramerateUnavailable as ex:
            error_strs = ['could not get framerate from video(s)',
                          'Failed to obtain framerate for video file %s.' % ex.file_name]
            error_strs.append('Specify framerate manually with the -f / --framerate option.')
            logging.debug('\n'.join(error_strs))
            raise click.BadParameter('\n'.join(error_strs), param_hint='input video')
        except VideoParameterMismatch as ex:
            error_strs = ['video parameters do not match.', 'List of mismatched parameters:']
            for param in ex.file_list:
                if param[0] == cv2.CAP_PROP_FPS:
                    param_name = 'FPS'
                if param[0] == cv2.CAP_PROP_FRAME_WIDTH:
                    param_name = 'Frame width'
                if param[0] == cv2.CAP_PROP_FRAME_HEIGHT:
                    param_name = 'Frame height'
                error_strs.append('  %s mismatch in video %s (got %.2f, expected %.2f)' % (
                    param_name, param[3], param[1], param[2]))
            error_strs.append(
                'Multiple videos may only be specified if they have the same framerate and'
                ' resolution. -f / --framerate may be specified to override the framerate.')
            logging.debug('\n'.join(error_strs))
            raise click.BadParameter('\n'.join(error_strs), param_hint='input videos')
        except InvalidDownscaleFactor as ex:
            error_strs = ['Downscale value is not > 0.', str(ex)]
            logging.debug('\n'.join(error_strs))
            raise click.BadParameter('\n'.join(error_strs), param_hint='downscale factor')
        return video_manager_initialized


    def parse_options(self, input_list, framerate, stats_file, downscale, frame_skip):
        # type: (List[str], float, str, int, int) -> None
        """ Parse Options: Parses all global options/arguments passed to the main
        scenedetect command, before other sub-commands (e.g. this function processes
        the [options] when calling scenedetect [options] [commands [command options]].

        This method calls the _init_video_manager(), _open_stats_file(), and
        check_input_open() methods, which may raise a click.BadParameter exception.

        Raises:
            click.BadParameter
        """
        if not input_list:
            return

        logging.debug('Parsing program options.')

        self.frame_skip = frame_skip

        video_manager_initialized = self._init_video_manager(
            input_list=input_list, framerate=framerate, downscale=downscale)

        # Ensure VideoManager is initialized, and open StatsManager if --stats is specified.
        if not video_manager_initialized:
            self.video_manager = None
            logging.info('VideoManager not initialized.')
        else:
            logging.debug('VideoManager initialized.')
            self.stats_file_path = self.get_output_file_path(stats_file)
            if self.stats_file_path is not None:
                self.check_input_open()
                self._open_stats_file()

        # Init SceneManager.
        self.scene_manager = SceneManager(self.stats_manager)

        self.options_processed = True


    def time_command(self, start=None, duration=None, end=None):
        # type: (Optional[str], Optional[str], Optional[str]) -> None
        """ Time Command: Parses all options/arguments passed to the time command,
        or with respect to the CLI, this function processes [time options] when calling:
        scenedetect [global options] time [time options] [other commands...].

        Raises:
            click.BadParameter, VideoDecodingInProgress
        """
        logging.debug('Setting video time:\n    start: %s, duration: %s, end: %s',
                      start, duration, end)

        self.check_input_open()

        if duration is not None and end is not None:
            raise click.BadParameter(
                'Only one of --duration/-d or --end/-e can be specified, not both.',
                param_hint='time')

        self.video_manager.set_duration(start_time=start, duration=duration, end_time=end)

        if start is not None:
            self.start_frame = start.get_frames()


    def list_scenes_command(self, output_path, filename_format, no_output_mode, quiet_mode):
        # type: (str, str, bool, bool) -> None
        """ List Scenes Command: Parses all options/arguments passed to the list-scenes command,
        or with respect to the CLI, this function processes [list-scenes options] when calling:
        scenedetect [global options] list-scenes [list-scenes options] [other commands...].

        Raises:
            click.BadParameter
        """
        self.check_input_open()

        self.print_scene_list = True if quiet_mode is None else not quiet_mode
        self.scene_list_directory = output_path
        self.scene_list_name_format = filename_format
        if self.scene_list_name_format is not None and not no_output_mode:
            logging.info('Scene list CSV file name format:\n  %s', self.scene_list_name_format)
        self.scene_list_output = False if no_output_mode else True
        if self.scene_list_directory is not None:
            logging.info('Scene list output directory set:\n  %s', self.scene_list_directory)


    def export_html_command(self, filename, no_images, image_width, image_height):
        # type: (str, bool) -> None
        """Export HTML command: Parses all options/arguments passed to the export-html command,
        or with respect to the CLI, this function processes [export-html] options when calling:
        scenedetect [global options] export-html [export-html options] [other commands...].

        Raises:
            click.BadParameter
        """
        self.check_input_open()

        self.html_name_format = filename
        if self.html_name_format is not None:
            logging.info('Scene list html file name format:\n %s', self.html_name_format)
        self.html_include_images = False if no_images else True
        self.image_width = image_width
        self.image_height = image_height


    def save_images_command(self, num_images, output, name_format, jpeg, webp, quality,
                            png, compression):
        # type: (int, str, str, bool, bool, int, bool, int) -> None
        """ Save Images Command: Parses all options/arguments passed to the save-images command,
        or with respect to the CLI, this function processes [save-images options] when calling:
        scenedetect [global options] save-images [save-images options] [other commands...].

        Raises:
            click.BadParameter
        """
        self.check_input_open()

        num_flags = sum([True if flag else False for flag in [jpeg, webp, png]])
        if num_flags <= 1:

            # Ensure the format exists.
            extension = 'jpg'   # Default is jpg.
            if png:
                extension = 'png'
            elif webp:
                extension = 'webp'
            if not extension in self.imwrite_params or self.imwrite_params[extension] is None:
                error_strs = [
                    'Image encoder type %s not supported.' % extension.upper(),
                    'The specified encoder type could not be found in the current OpenCV module.',
                    'To enable this output format, please update the installed version of OpenCV.',
                    'If you build OpenCV, ensure the the proper dependencies are enabled. ']
                logging.debug('\n'.join(error_strs))
                raise click.BadParameter('\n'.join(error_strs), param_hint='save-images')

            self.save_images = True
            self.image_directory = output
            self.image_extension = extension
            self.image_param = compression if png else quality
            self.image_name_format = name_format
            self.num_images = num_images

            image_type = 'JPEG' if self.image_extension == 'jpg' else self.image_extension.upper()
            image_param_type = ''
            if self.image_param:
                image_param_type = 'Compression' if image_type == 'PNG' else 'Quality'
                image_param_type = ' [%s: %d]' % (image_param_type, self.image_param)
            logging.info('Image output format set: %s%s', image_type, image_param_type)
            if self.image_directory is not None:
                logging.info('Image output directory set:\n  %s',
                             os.path.abspath(self.image_directory))
        else:
            self.options_processed = False
            logging.error('Multiple image type flags set for save-images command.')
            raise click.BadParameter(
                'Only one image type (JPG/PNG/WEBP) can be specified.', param_hint='save-images')
def test_reset(test_video_file):
    """ Test VideoManager reset method. """
    video_manager = VideoManager([test_video_file] * 2)
    base_timecode = video_manager.get_base_timecode()
    try:
        video_manager.start()
        assert video_manager.get_current_timecode() == base_timecode
        ret_val, frame_image = video_manager.read()
        assert ret_val
        assert frame_image.shape[0] > 0
        assert video_manager.get_current_timecode() == base_timecode + 1

        video_manager.release()
        video_manager.reset()

        video_manager.start()
        assert video_manager.get_current_timecode() == base_timecode
        ret_val, frame_image = video_manager.read()
        assert ret_val
        assert frame_image.shape[0] > 0
        assert video_manager.get_current_timecode() == base_timecode + 1

    finally:
        video_manager.release()