def test_metrics():
    """ Test StatsManager metric registration/setting/getting with a set of pre-defined
    key-value pairs (metric_dict).
    """
    metric_dict = {'some_metric': 1.2345, 'another_metric': 6.7890}
    metric_keys = list(metric_dict.keys())

    stats = StatsManager()
    frame_key = 100
    assert not stats.is_save_required()

    stats.register_metrics(metric_keys)

    assert not stats.is_save_required()
    with pytest.raises(FrameMetricRegistered):
        stats.register_metrics(metric_keys)

    assert not stats.metrics_exist(frame_key, metric_keys)
    assert stats.get_metrics(frame_key, metric_keys) == [None] * len(metric_keys)

    stats.set_metrics(frame_key, metric_dict)

    assert stats.is_save_required()

    assert stats.metrics_exist(frame_key, metric_keys)
    assert stats.metrics_exist(frame_key, metric_keys[1:])

    assert stats.get_metrics(frame_key, metric_keys) == [
        metric_dict[metric_key] for metric_key in metric_keys]
def test_metrics():
    """ Test StatsManager metric registration/setting/getting with a set of pre-defined
    key-value pairs (metric_dict).
    """
    metric_dict = {'some_metric': 1.2345, 'another_metric': 6.7890}
    metric_keys = list(metric_dict.keys())

    stats = StatsManager()
    frame_key = 100
    assert not stats.is_save_required()

    stats.register_metrics(metric_keys)

    assert not stats.is_save_required()
    with pytest.raises(FrameMetricRegistered):
        stats.register_metrics(metric_keys)

    assert not stats.metrics_exist(frame_key, metric_keys)
    assert stats.get_metrics(frame_key, metric_keys) == [None] * len(metric_keys)

    stats.set_metrics(frame_key, metric_dict)

    assert stats.is_save_required()

    assert stats.metrics_exist(frame_key, metric_keys)
    assert stats.metrics_exist(frame_key, metric_keys[1:])

    assert stats.get_metrics(frame_key, metric_keys) == [
        metric_dict[metric_key] for metric_key in metric_keys]
Пример #3
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 main():

    # 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(['00Lty3r6JLE.mp4'])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    # scene_manager.add_detector(ThresholdDetector())
    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)
        video_manager.set_duration()

        # 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,
                                    show_progress=False)

        # 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, Second %f, End %s / Frame %d, Second %f'
                % (i + 1, scene[0].get_timecode(), scene[0].get_frames(),
                   scene[0].get_seconds(), scene[1].get_timecode(),
                   scene[1].get_frames(), scene[1].get_seconds()))

        # 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()
Пример #5
0
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()
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 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()
Пример #8
0
def video_scene_detect(video_name, stats_file_name, file_name, video_path):
    """
    returns a list of scenes
    https://pyscenedetect.readthedocs.io/en/latest/examples/usage-python/
    https://github.com/Breakthrough/PySceneDetect/blob/master/scenedetect/video_manager.py
    """
    os.chdir(video_path)
    os.system(f"scenedetect --input \
    '{video_name}' --stats {stats_file_name} \
    detect-content --threshold 27")
    video_manager = VideoManager([video_name])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        if os.path.exists(stats_file_name):
            with open(stats_file_name, 'r') as stats_file:
                stats_manager.load_from_csv(stats_file, base_timecode)
        start_time = base_timecode + 10  # set start time
        end_time = base_timecode + 200000.0  #set end_time
        video_manager.set_duration(start_time=start_time, end_time=end_time)
        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)
        final_scene_list = []
        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(),
            ))
            final_scene_list.append({
                "scene_num": i + 1,
                "start": scene[0].get_timecode(),
                "end": scene[0].get_frames(),
                "start_frame": scene[0].get_frames(),
                "end_frame": scene[1].get_frames()
            })
        if stats_manager.is_save_required():
            with open(file_name, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)
    finally:
        video_manager.release()
    return final_scene_list
Пример #9
0
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()
Пример #10
0
def test_api(test_video_file):
    # (str) -> None
    """ Test overall PySceneDetect API functionality.

    Can be considered a high level integration/black-box test.

    """

    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([test_video_file])
    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()
Пример #11
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()

    # TODO: Replace with scenedetect.scene_manager.save_images
    def _generate_images(
            self,
            scene_list,
            video_name,
            image_name_template='$VIDEO_NAME-Scene-$SCENE_NUMBER-$IMAGE_NUMBER',
            output_dir=None,
            downscale_factor=1):
        # 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()

        fps = scene_list[0][0].framerate

        timecode_list = [
            [
                FrameTimecode(int(f), fps=fps) for f in [
                    # middle frames
                    a[len(a) // 2] if (
                        0 < j < self.num_images - 1) or self.num_images == 1

                    # first frame
                    else min(a[0] + self.image_frame_margin, a[-1]) if j == 0

                    # last frame
                    else max(a[-1] - self.image_frame_margin, a[0])

                    # for each evenly-split array of frames in the scene list
                    for j, a in enumerate(np.array_split(r, self.num_images))
                ]
            ] for i, r in enumerate([
                # pad ranges to number of images
                r if r.stop - r.start >= self.num_images else list(r) +
                [r.stop - 1] * (self.num_images - len(r))
                # create range of frames in scene
                for r in (
                    range(start.get_frames(), end.get_frames())
                    # for each scene in scene list
                    for start, end in scene_list)
            ])
        ]

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

        for i, tl in enumerate(timecode_list):
            for j, image_timecode in enumerate(tl):
                self.video_manager.seek(image_timecode)
                self.video_manager.grab()
                ret_val, frame_im = self.video_manager.retrieve()

                if downscale_factor != 1:
                    logging.info("resizing thumb")
                    scale_percent = 1 / downscale_factor
                    width = int(frame_im.shape[1] * scale_percent)
                    height = int(frame_im.shape[0] * scale_percent)
                    resized = cv2.resize(frame_im, (width, height),
                                         interpolation=cv2.INTER_AREA)
                    frame_im = resized

                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),
                        FRAME_NUMBER=image_timecode.get_frames()),
                                           self.image_extension)
                    self.image_filenames[i].append(file_path)
                    cv2.imwrite(
                        get_and_create_path(
                            file_path, output_dir if output_dir is not None
                            else self.output_directory), 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 _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 = get_and_create_path(
                scene_list_filename,
                self.scene_list_directory if self.scene_list_directory
                is not None else self.output_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,
                downscale_factor=self.video_manager.get_downscale_factor())

        # 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 = get_and_create_path(
                html_filename, self.image_directory
                if self.image_directory is not None else self.output_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 = get_and_create_path(
                self.split_name_format, self.split_directory
                if self.split_directory is not None else self.output_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 = get_and_create_path(stats_file,
                                                       self.output_directory)
            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, image_frame_margin):
        # 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
            self.image_frame_margin = image_frame_margin

            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')
Пример #12
0
def spliter(file_name, frame_rate=25, threshold=55):
    print('detecting scenes...')
    STATS_FILE_PATH = './' + os.path.basename(
        os.path.splitext(file_name)[0]) + '.csv'
    # print(STATS_FILE_PATH)
    # print(file_name)
    scenes = list()

    # 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([file_name])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(
        ContentDetector(threshold=55.0, min_scene_len=288))
    base_timecode = video_manager.get_base_timecode()
    # print(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,
                                    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.

        # 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(),))
        # print('scene[0] is {}, scene[1] is {}, i+1 is {}'.format(scene[0], scene[1], i+1) )
        # scenes.append((scene[0], scene[1]))
        # print('-------------------------------------------------------------------------------------------------')
        # print('ffmpeg -ss {} -i testvideo.mp4 -c:v libx264 -t {} -an test-scene-{}.mp4'.format(scene[0],
        #                                                                                        scene[1], i + 1))

        # 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)

        # is_ffmpeg_available()
        # print(scene_list)

        scenes = split_video_ffmpeg(
            [file_name], scene_list, '$VIDEO_NAME-Scene-$SCENE_NUMBER.mp4',
            os.path.basename(os.path.splitext(file_name)[0]) + '-reference',
            '-c:v libx264 -preset fast -crf 18 -an')

    finally:
        video_manager.release()

    return scenes
Пример #13
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
        self.drop_short_scenes = False  # --drop-short-scenes
        self.min_scene_len = None  # -m/--min-scene-len

        # 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 = 3  # save-images -n/--num-images
        self.frame_margin = 1  # save-images -m/--frame-margin
        self.scale = None  # save-images -s/--scale
        self.height = None  # save-images -h/--height
        self.width = None  # save-images -w/--width

        # 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.skip_cuts = False  # list-scenes -s/--skip-cuts

        # Properties for export-html command.
        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_width = None  # export-html -w/--image-width
        self.image_height = None  # export-html -h/--image-height

        # Logger for CLI output.
        self.logger = logging.getLogger('pyscenedetect')

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

    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):
                self.logger.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)
                except StatsFileCorrupt:
                    error_info = (
                        'Could not load frame metrics from stats file - file is either 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.')
                    error_strs = [
                        'Could not load stats file.',
                        'Failed to parse stats file:', error_info
                    ]
                    self.logger.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')

    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.
        """
        self.logger.debug('Processing input...')
        if not self.options_processed:
            self.logger.debug(
                'Skipping processing, CLI options were not parsed successfully.'
            )
            return
        self.check_input_open()
        assert self.scene_manager.get_num_detectors() >= 0
        if self.scene_manager.get_num_detectors() == 0:
            self.logger.error(
                'No scene detectors specified (detect-content, detect-threshold, etc...),\n'
                ' or failed to process all command line arguments.')
            return

        # Display a warning if the video codec type seems unsupported (#86).
        if int(abs(self.video_manager.get(cv2.CAP_PROP_FOURCC))) == 0:
            self.logger.error(
                'Video codec detection failed, output may be incorrect.\nThis could be caused'
                ' by using an outdated version of OpenCV, or using codecs that currently are'
                ' not well supported (e.g. VP9).\n'
                'As a workaround, consider re-encoding the source material before processing.\n'
                'For details, see https://github.com/Breakthrough/PySceneDetect/issues/86'
            )

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

        start_time = time.time()
        self.logger.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)

        # Handle case where video fails with multiple audio tracks (#179).
        # TODO: Using a different video backend as per #213 may also resolve this issue,
        # as well as numerous other timing related issues.
        if num_frames <= 0:
            self.logger.critical(
                'Failed to read any frames from video file. This could be caused'
                ' by the video having multiple audio tracks. If so, please try'
                ' removing the audio tracks or muxing to mkv via:\n'
                '      ffmpeg -i input.mp4 -c copy -an output.mp4\n'
                'or:\n'
                '      mkvmerge -o output.mkv input.mp4\n'
                'For details, see https://pyscenedetect.readthedocs.io/en/latest/faq/'
            )
            return

        duration = time.time() - start_time
        self.logger.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:
                    self.logger.info('Saving frame metrics to stats file: %s',
                                     os.path.basename(self.stats_file_path))
                    base_timecode = self.video_manager.get_base_timecode()
                    self.stats_manager.save_to_csv(stats_file, base_timecode)
            else:
                self.logger.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()
        scene_list = self.scene_manager.get_scene_list()

        # Handle --drop-short-scenes.
        if self.drop_short_scenes and self.min_scene_len > 0:
            scene_list = [
                s for s in scene_list if (s[1] - s[0]) >= self.min_scene_len
            ]

        video_paths = self.video_manager.get_video_paths()
        video_name = self.video_manager.get_video_name()

        if scene_list:  # Ensure we don't divide by zero.
            self.logger.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:
            self.logger.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 = get_and_create_path(
                scene_list_filename,
                self.scene_list_directory if self.scene_list_directory
                is not None else self.output_directory)
            self.logger.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(output_csv_file=scene_list_file,
                                 scene_list=scene_list,
                                 include_cut_list=not self.skip_cuts,
                                 cut_list=cut_list)

        if self.print_scene_list:
            self.logger.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:
            self.logger.info(
                'Comma-separated timecode list:\n  %s',
                ','.join([cut.get_timecode() for cut in cut_list]))

        # Handle save-images command.

        if self.save_images:
            image_output_dir = self.output_directory
            if self.image_directory is not None:
                image_output_dir = self.image_directory

            image_filenames = save_images(
                scene_list=scene_list,
                video_manager=self.video_manager,
                num_images=self.num_images,
                frame_margin=self.frame_margin,
                image_extension=self.image_extension,
                encoder_param=self.image_param,
                image_name_template=self.image_name_format,
                output_dir=image_output_dir,
                show_progress=not self.quiet_mode,
                scale=self.scale,
                height=self.height,
                width=self.width)

        # 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 = get_and_create_path(
                html_filename, self.image_directory
                if self.image_directory is not None else self.output_directory)
            self.logger.info('Exporting to html file:\n %s:', html_path)
            if not self.html_include_images:
                image_filenames = None
            write_scene_list_html(html_path,
                                  scene_list,
                                  cut_list,
                                  image_filenames=image_filenames,
                                  image_width=self.image_width,
                                  image_height=self.image_height)

        # Handle split-video command.
        if self.split_video:
            output_path_template = self.split_name_format
            # Add proper extension to filename template if required.
            dot_pos = output_path_template.rfind('.')
            extension_length = 0 if dot_pos < 0 else len(
                output_path_template) - (dot_pos + 1)
            # If using mkvmerge, force extension to .mkv.
            if self.split_mkvmerge and not output_path_template.endswith(
                    '.mkv'):
                output_path_template += '.mkv'
            # Otherwise, if using ffmpeg, only add an extension if one doesn't exist.
            elif not 2 <= extension_length <= 4:
                output_path_template += '.mp4'
            output_path_template = get_and_create_path(
                output_path_template, self.split_directory
                if self.split_directory is not None else self.output_directory)
            # Ensure the appropriate tool is available before handling split-video.
            check_split_video_requirements(self.split_mkvmerge)
            if self.split_mkvmerge:
                split_video_mkvmerge(video_paths,
                                     scene_list,
                                     output_path_template,
                                     video_name,
                                     suppress_output=self.quiet_mode
                                     or self.split_quiet)
            else:
                split_video_ffmpeg(video_paths,
                                   scene_list,
                                   output_path_template,
                                   video_name,
                                   arg_override=self.split_args,
                                   hide_progress=self.quiet_mode,
                                   suppress_output=self.quiet_mode
                                   or self.split_quiet)
            if scene_list:
                self.logger.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)
            self.logger.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

        self.logger.debug('Initializing VideoManager.')
        video_manager_initialized = False
        try:
            self.video_manager = VideoManager(video_files=input_list,
                                              framerate=framerate,
                                              logger=self.logger)
            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
                ]
            self.logger.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.')
            self.logger.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.'
            )
            self.logger.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)]
            self.logger.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, min_scene_len, drop_short_scenes):
        # 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

        self.logger.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
            self.logger.info('VideoManager not initialized.')
        else:
            self.logger.debug('VideoManager initialized.')
            self.stats_file_path = get_and_create_path(stats_file,
                                                       self.output_directory)
            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.drop_short_scenes = drop_short_scenes
        self.min_scene_len = parse_timecode(self, min_scene_len)

        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
        """
        self.logger.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, skip_cuts):
        # 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:
            self.logger.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:
            self.logger.info('Scene list output directory set:\n  %s',
                             self.scene_list_directory)
        self.skip_cuts = skip_cuts

    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:
            self.logger.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, frame_margin, scale,
                            height, width):
        # type: (int, str, str, bool, bool, int, bool, int, float, int, 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()

        if contains_sequence_or_url(self.video_manager.get_video_paths()):
            self.options_processed = False
            error_str = '\nThe save-images command is incompatible with image sequences/URLs.'
            self.logger.error(error_str)
            raise click.BadParameter(error_str, param_hint='save-images')

        num_flags = sum([1 if flag else 0 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'
            valid_params = get_cv2_imwrite_params()
            if not extension in valid_params or valid_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. '
                ]
                self.logger.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
            self.frame_margin = frame_margin
            self.scale = scale
            self.height = height
            self.width = width

            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)
            self.logger.info('Image output format set: %s%s', image_type,
                             image_param_type)
            if self.image_directory is not None:
                self.logger.info('Image output directory set:\n  %s',
                                 os.path.abspath(self.image_directory))
        else:
            self.options_processed = False
            self.logger.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')
Пример #14
0
    def find_scene_changes(self,
                           video_path,
                           method='threshold',
                           new_stat_file=True):
        """
        Detect scene changes in given video.

        Args:
            video_path: Path to video to analyze
            method: Method for detecting scene changes
            new_stat_file: Option to save results

        Returns:
            Scene changes + their corresponding time codes

        """
        # 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. thresholsd).
        if method == 'content':
            scene_manager.add_detector(
                ContentDetector(threshold=30, min_scene_len=40))
        else:
            scene_manager.add_detector(
                ThresholdDetector(min_scene_len=40,
                                  threshold=125,
                                  min_percent=0.5))

        base_timecode = video_manager.get_base_timecode()

        # We save our stats file to {VIDEO_PATH}.{CONTENT}.stats.csv.
        stats_file_path = '%s.%s.stats.csv' % (video_path, method)

        scene_list = []

        try:
            # If stats file exists, load it.
            if not new_stat_file and 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(2)

            # 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.

            # 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
Пример #15
0
def find_scenes(video_id, video_path, scenes_path):
    # type: (str) -> List[Tuple[FrameTimecode, FrameTimecode]]
    video_manager = VideoManager([video_path + video_id + ".mp4"])
    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()

    # We save our stats file to {VIDEO_PATH}.stats.csv.
    stats_file_path = scenes_path + video_id + "_stats.csv"
    scenes_file_path = scenes_path + video_id + ".csv"

    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.

        df = pd.DataFrame(
            columns=['scene', 'start', 'start_frame', 'end', 'end_frame'])

        #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(),))

            df = df.append(
                {
                    'scene': i + 1,
                    'start': scene[0].get_timecode(),
                    'start_frame': scene[0].get_frames(),
                    'end': scene[1].get_timecode(),
                    'end_frame': scene[1].get_frames()
                },
                ignore_index=True)

        # 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)

        df.to_csv(scenes_file_path, index=False)

    finally:
        video_manager.release()

    return
Пример #16
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 [start_frame]
        self.stats_manager = StatsManager()  # -s/--stats
        self.stats_file_path = None  # -s/--stats [stats_file_path]
        self.output_directory = None  # -o/--output [output_directory]
        self.quiet_mode = False  # -q/--quiet or -v/--verbosity quiet
        self.frame_skip = 0  # -fs/--frame-skip [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 [image_directory]
        self.image_param = None  # save-images -q/--quality if -j/-w, -c/--compression if -p
        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 -m/--mkvmerge (or split-video without ffmpeg)
        self.split_args = None  # split-video -f/--ffmpeg-args [split_args]
        self.split_directory = None  # split-video -o/--output [split_directory]
        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_path = None  # list-scenes -o [scene_list_path]

    def cleanup(self):
        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, image_prefix, output_dir=None):
        # type: (List[Tuple[FrameTimecode, FrameTimecode]) -> None

        if self.num_images != 2:
            raise NotImplementedError()

        if not scene_list:
            return
        if not self.options_processed:
            return
        self.check_input_open()

        imwrite_param = []
        if self.image_param is not None:
            imwrite_param = [
                self.imwrite_params[self.image_extension], self.image_param
            ]
        click.echo(imwrite_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) * 2, unit='images')

        for i, (start_time, end_time) in enumerate(scene_list):
            # TODO: Interpolate timecodes if num_frames_per_scene != 2.
            self.video_manager.seek(start_time)
            self.video_manager.grab()
            ret_val, frame_im = self.video_manager.retrieve()
            if ret_val:
                cv2.imwrite(
                    self.get_output_file_path(
                        '%s-Scene-%03d-00.%s' %
                        (image_prefix, i + 1, self.image_extension),
                        output_dir=output_dir), frame_im, imwrite_param)
            else:
                completed = False
                break
            if progress_bar:
                progress_bar.update(1)
            self.video_manager.seek(end_time)
            self.video_manager.grab()
            ret_val, frame_im = self.video_manager.retrieve()
            if ret_val:
                cv2.imwrite(
                    self.get_output_file_path(
                        '%s-Scene-%03d-01.%s' %
                        (image_prefix, i + 1, self.image_extension),
                        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
        '''Returns path to output file_path passed as argument, and creates directories if necessary.'''
        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.
        os.makedirs(os.path.split(os.path.abspath(file_path))[0],
                    exist_ok=True)
        return file_path

    def _open_stats_file(self):

        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...).'
            )
            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,
            start_time=self.start_frame,
            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('.')]

        # Handle list-scenes command.
        # Handle `list-scenes -o`.
        if self.scene_list_path is not None:
            with open(self.scene_list_path, 'wt') as scene_list_file:
                write_scene_list(scene_list_file, cut_list, scene_list)
        # Handle `list-scenes`.
        list_length = len(scene_list) if len(scene_list) else 1
        logging.info(
            'Detected %d scenes, average shot length %.1f seconds.',
            list_length,
            sum([(end_time - start_time).get_seconds()
                 for start_time, end_time in scene_list]) / float(list_length))
        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,
                                  image_prefix=video_name,
                                  output_dir=self.image_directory)

        # Handle split-video command.
        if self.split_video:
            output_file_name = self.get_output_file_path(
                video_name, 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.info('ffmpeg not found.')
                logging.info('Splitting input video%s using mkvmerge...',
                             's' if len(video_paths) > 1 else '')
                split_video_mkvmerge(video_paths,
                                     scene_list,
                                     output_file_name,
                                     suppress_output=self.quiet_mode
                                     or self.split_quiet)
            elif ffmpeg_available:
                logging.info('Splitting input video%s using ffmpeg...',
                             's' if len(video_paths) > 1 else '')
                split_video_ffmpeg(video_paths,
                                   scene_list,
                                   output_file_name,
                                   arg_override=self.split_args,
                                   hide_progress=self.quiet_mode
                                   or self.split_quiet,
                                   suppress_output=self.quiet_mode
                                   or self.split_quiet)
            else:
                error_strs = [
                    "ffmpeg/mkvmerge is required for video splitting.",
                    "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')

            logging.info(
                'Video splitting completed, individual scenes written to disk.'
            )

    def check_input_open(self):
        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):
        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):
        """ Parse Options: Parses all CLI arguments passed to scenedetect [options]. """
        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):

        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, quiet_mode):
        self.check_input_open()

        self.print_scene_list = True if quiet_mode is None else not quiet_mode
        self.scene_list_path = self.get_output_file_path(output_path)
        if self.scene_list_path is not None:
            logging.info('Output scene list CSV file set:\n  %s',
                         self.scene_list_path)

    def save_images_command(self, num_images, output, jpeg, webp, quality, png,
                            compression):
        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.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')
        if os.path.exists(vidDirectory+ID+'/'+ID+'stats.csv'):
            # Read stats from CSV file opened in read mode:
            with open(vidDirectory+ID+'/'+ID+'stats.csv', '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()

        # 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.
        
        #Save the intervals
        numpy.savetxt(vidDirectory+ID+'/'+ID+'scenes.csv', [a for a in scene_list], delimiter=",")

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

    finally:
        video_manager.release()

Пример #18
0
def main():

    for root, dirs, files in os.walk('material'):
        for file in files:
            file = os.path.join(root, file)

            video_manager = VideoManager([file])
            stats_manager = StatsManager()
            scene_manager = SceneManager(stats_manager)
            scene_manager.add_detector(ContentDetector())
            base_timecode = video_manager.get_base_timecode()
            end_timecode = video_manager.get_duration()

            start_time = base_timecode
            end_time = end_timecode[2]

            video_manager.set_duration(start_time=start_time,
                                       end_time=end_time)
            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)

            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(),
                      ))

                raw = ffmpeg.input(file)

                start = scene[0].get_timecode()
                end = scene[1].get_timecode()

                audio = (raw.filter_('atrim', start=start,
                                     end=end).filter_('asetpts',
                                                      'PTS-STARTPTS'))

                raw = ffmpeg.trim(raw, start=start, end=end)
                raw = raw.setpts('PTS-STARTPTS')

                joined = ffmpeg.concat(raw, audio, v=1, a=1).node
                stream = ffmpeg.output(joined[0], joined[1],
                                       'scene%d.mp4' % (i + 1))
                stream.run()

            shuffled = sorted(scene_list, key=lambda k: random.random())

            stream = 0
            video_list = []
            audio_list = []
            merge_list = []
            raw = ffmpeg.input(file)

            for i, scene in enumerate(shuffled):
                start = scene[0].get_timecode()
                end = scene[1].get_timecode()

                audio = (raw.filter_('atrim', start=start,
                                     end=end).filter_('asetpts',
                                                      'PTS-STARTPTS'))

                video = ffmpeg.trim(raw, start=start, end=end)
                video = video.setpts('PTS-STARTPTS')

                video_list.append(video)
                audio_list.append(audio)

                if (i == len(shuffled) - 1):
                    for i in range(len(video_list)):
                        merge_list.append(video_list[i])
                        merge_list.append(audio_list[i])

                    stream = ffmpeg.concat(*merge_list, v=1, a=1)
                    stream = ffmpeg.output(stream, 'new.mp4')
                    stream.run()
Пример #19
0
def make_dataset(video_path, video_name, timecodes, save_dir):
    # 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()

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

    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)

        # 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)

        start_timecode = ""
        start_content_val = 0
        end_timecode = ""
        end_content_val = 0
        metric_keys = sorted(
            list(
                stats_manager._registered_metrics.union(
                    stats_manager._loaded_metrics)))
        frame_keys = sorted(stats_manager._frame_metrics.keys())
        for frame_key in frame_keys:
            frame_timecode = base_timecode + frame_key
            timecode = frame_timecode.get_timecode()
            if timecode > timecodes[0] and timecode < timecodes[1]:
                content_val = stats_manager.get_metrics(
                    frame_key, metric_keys)[0]
                if start_content_val < content_val:
                    start_content_val = content_val
                    start_timecode = timecode
            if timecode > timecodes[2] and timecode < timecodes[3]:
                content_val = stats_manager.get_metrics(
                    frame_key, metric_keys)[0]
                if end_content_val < content_val:
                    end_content_val = content_val
                    end_timecode = timecode
        threshold = min(start_content_val, end_content_val)

        print(f"Start Time: {start_timecode}, End Time: {end_timecode}")

    finally:
        video_manager.release()

    video_manager = VideoManager([video_path])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    scene_manager.add_detector(ContentDetector(threshold=threshold))
    base_timecode = video_manager.get_base_timecode()

    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.

        start_video_num = 0
        end_video_num = 0
        for i, scene in enumerate(scene_list):
            if scene[0].get_timecode(
            ) >= start_timecode and start_video_num == 0:
                start_video_num = i
                print(f"start video: {start_video_num}")
            if scene[1].get_timecode() >= end_timecode and end_video_num == 0:
                end_video_num = i
                print(f"end video: {end_video_num}")

    finally:
        video_manager.release()

    video_dir = os.path.join(save_dir, video_name)
    if not os.path.exists(video_dir):
        os.makedirs(video_dir)

    split_video_ffmpeg([video_path], scene_list,
                       os.path.join(video_dir,
                                    "${VIDEO_NAME}-${SCENE_NUMBER}.mp4"),
                       video_name)

    return start_video_num, end_video_num, len(scene_list)
Пример #20
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')
Пример #21
0
def find_scenes(video_path):

    try:
        start_time = perf_counter()
        print(f"find_scenes({video_path})")
        # type: (str) -> List[Tuple[FrameTimecode, FrameTimecode]]

        file_name = video_path[video_path.rfind('/') + 1:video_path.find('.')]
        dir = os.path.join(DATA_DIR, file_name)
        if not os.path.exists(dir):
            os.mkdir(dir)

        #cap = cv2.VideoCapture(video_path)
        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=2, min_scene_len=100))
        #scene_manager.add_detector(ThresholdDetector(threshold=4))
        base_timecode = video_manager.get_base_timecode()

        # We save our stats file to {VIDEO_PATH}.stats.csv.
        stats_file_path = f'{video_path}.stats.csv'
        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)

            # 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()

        print(f"find_scenes({video_path}) - phase 2, Extract jpg")

        cap = cv2.VideoCapture(video_path)

        verbose = False
        if verbose:
            print('List of scenes obtained:')
        # Each scene is a tuple of (start, end) FrameTimecodes.

        scenes = []

        for i, scene in enumerate(scene_list):
            if verbose:
                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(),
                ))
            cap.set(
                cv2.CAP_PROP_POS_FRAMES, scene[0].get_frames() +
                (scene[1].get_frames() - scene[0].get_frames()) // 2)
            frame_no = scene[0].get_frames()
            if verbose:
                print('Frame no.', frame_no)
            res, frame = cap.read()
            img_file = os.path.join(DATA_DIR, file_name, "%d.jpg" % i)
            cv2.imwrite(img_file, frame)
            scenes.append({
                "start": scene[0].get_timecode(),
                "img_file": img_file,
                "end": scene[1].get_timecode()
            })

        end_time = perf_counter()
        print(
            f"findScene() Complete. Returning {len(scenes)} scene(s). Duration {int(end_time - start_time)} seconds"
        )

        return json.dumps(scenes)
    except Exception as e:
        print("findScene() throwing Exception:" + str(e))
        raise e
Пример #22
0
def getScenes(video_path,
              threshold=30.0,
              minSceneDur=500,
              windowSize=50,
              fadeThreshold=3.0):
    global progress
    global fileCount

    basename = os.path.basename(video_path)
    doStats = CHECK_FOR_FADE or PLOT or SAVE_STATS

    # 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)

    base_timecode = video_manager.get_base_timecode()
    framerate = video_manager.get_framerate()

    # Add ContentDetector algorithm (each detector's constructor
    # takes detector options, e.g. threshold).
    min_scene_len = roundInt(minSceneDur / 1000.0 * framerate)
    scene_manager.add_detector(
        ContentDetector(threshold=threshold, min_scene_len=min_scene_len))

    # We save our stats file to {VIDEO_PATH}.stats.csv.
    stats_file_path = OUTPUT_FILE.replace(".csv", "%s.csv")
    stats_file_path = stats_file_path % ("_" + basename + "_stats")

    scene_list = []

    print("Looking for scenes in %s" % video_path)
    try:
        # If stats file exists, load it.
        if doStats and 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.
        scenes = scene_manager.get_scene_list(base_timecode)
        # Each scene is a tuple of (start, end) FrameTimecodes.

        for i, scene in enumerate(scenes):
            start = roundInt(scene[0].get_seconds() * 1000)
            end = roundInt(scene[1].get_seconds() * 1000)
            scene_list.append({
                "filename": basename,
                "index": i,
                "start": start,
                "end": end,
                "dur": end - start,
                "frameStart": scene[0].get_frames(),
                "frameEnd": scene[1].get_frames()
            })

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

        # Retrieve raw data for plotting and additional analysis
        fieldNames, sceneData = readCsv(stats_file_path, skipLines=1)
        dlen = len(sceneData)

        # Add smoothed data
        windowLeft = int(windowSize / 2)
        windowRight = windowSize - windowLeft
        for i, d in enumerate(sceneData):
            i0 = max(i - windowLeft, 0)
            i1 = min(i + windowRight, dlen - 1)
            sceneData[i]["smoothed"] = np.mean(
                [d["content_val"] for d in sceneData[i0:i1]])
            sceneData[i]["ms"] = timecodeToMs(d["Timecode"])

        # Add crossfade cuts
        if CHECK_FOR_FADE:
            for i, d in enumerate(sceneData):
                ms = d["ms"]
                value = d["smoothed"]
                frame = d["Frame Number"]
                neighboringCuts = [
                    s for s in scene_list
                    if abs(frame - s["frameStart"]) <= windowSize
                    or abs(frame - s["frameEnd"]) <= windowSize
                ]

                # if there's no nearby cuts and we've reached the fade threshold
                if len(neighboringCuts) <= 0 and value >= fadeThreshold:
                    # retrieve the scene right before this one
                    sortedList = sorted(scene_list,
                                        key=lambda k: k['frameStart'])
                    prev = [s for s in sortedList if s["frameStart"] < frame]
                    if len(prev) > 0:
                        prev = prev[-1]
                    else:
                        prev = sortedList[0]

                    # Find local minimums to determine fade start/end
                    leftWindow = sorted([
                        d for d in sceneData
                        if frame - windowSize < d["Frame Number"] < frame
                    ],
                                        key=lambda k: k['smoothed'])
                    rightWindow = sorted([
                        d for d in sceneData
                        if frame < d["Frame Number"] < frame + windowSize
                    ],
                                         key=lambda k: k['smoothed'])
                    fadeStart = leftWindow[0]
                    fadeEnd = rightWindow[0]

                    # Add new cut if we're not too close to the edges
                    if fadeStart["ms"] - prev["start"] >= minSceneDur and prev[
                            "end"] - fadeEnd["ms"] >= minSceneDur:
                        # Add the new scene
                        scene_list.append({
                            "filename": basename,
                            "index": prev["index"] + 1,
                            "frameStart": fadeEnd["Frame Number"],
                            "frameEnd": prev["frameEnd"],
                            "start": fadeEnd["ms"],
                            "end": prev["end"],
                            "dur": prev["end"] - fadeEnd["ms"]
                        })

                        # Update the previous scene
                        scene_list[prev["index"]]["end"] = fadeStart["ms"]
                        scene_list[prev["index"]][
                            "dur"] = fadeStart["ms"] - prev["start"]
                        scene_list[prev["index"]]["frameEnd"] = fadeStart[
                            "Frame Number"]

                        # Sort and update indices
                        scene_list = sorted(scene_list,
                                            key=lambda k: k['frameStart'])
                        for j, s in enumerate(scene_list):
                            scene_list[j]["index"] = j

        if PLOT:
            f0, f1 = PLOT
            # add raw data
            xs = [
                d["Frame Number"] - 1 for d in sceneData
                if f0 <= d["Frame Number"] <= f1
            ]
            ys = [
                d["content_val"] for d in sceneData
                if f0 <= d["Frame Number"] <= f1
            ]
            plt.plot(xs, ys)

            # add smoothed data
            ys = [
                d["smoothed"] for d in sceneData
                if f0 <= d["Frame Number"] <= f1
            ]
            plt.plot(xs, ys, "c")

            # add horizontal line for threshold
            plt.plot([xs[0], xs[-1]], [threshold, threshold], "g--")

            # add scenes as plot data
            xs = [
                d["frameEnd"] - 1 for d in scene_list
                if f0 <= d["frameEnd"] <= f1
            ]
            ys = [
                sceneData[d["frameEnd"] - 1]["content_val"] for d in scene_list
                if f0 <= d["frameEnd"] <= f1
            ]
            plt.scatter(xs, ys, c="red")
            plt.show()

        if os.path.exists(stats_file_path) and not SAVE_STATS:
            os.remove(stats_file_path)

    finally:
        video_manager.release()

    progress += 1
    sys.stdout.write('\r')
    sys.stdout.write("%s%%" % round(1.0 * progress / fileCount * 100, 1))
    sys.stdout.flush()

    return scene_list