Exemple #1
0
    def detect_scenes(self):
        filenames = os.listdir(self.rgb_folder)
        filenames.sort(key=lambda x: int(re.sub('\D', '', x)))
        detector = ContentDetector(threshold=30.0, min_scene_len=7)
        self.cutting_list = []
        self.frames = []

        frame_num = 0
        for filename in filenames:
            with open(self.rgb_folder + filename, 'rb') as f:
                bytes = bytearray(f.read())
            bytes = np.array(bytes)
            frame_img = np.dstack([
                bytes[0:320 * 180], bytes[320 * 180:320 * 180 * 2],
                bytes[320 * 180 * 2:]
            ])
            frame_img = frame_img.reshape(180, 320, 3)
            self.frames.append(frame_img)
            cuts = detector.process_frame(frame_num, frame_img)
            self.cutting_list += cuts
            frame_num += 1
            if frame_num % 1000 == 0 and self.signals is not None:
                self.signals.report_progress.emit((
                    'Detecting and segmenting shots... {frame_num}/16200 frames evaluated.'
                    .format(frame_num=frame_num), frame_num / 16200))
        self.cutting_list += detector.post_process(frame_num)
        self.frames = self.frames
def capture_video(video_path, capture_path, default_path):
    print("\n[전환장면 캡처 시작] 영상 내 전환 시점을 기준으로 이미지 추출을 시작합니다")

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

    # 가장 예민하게 잡아내도록 1~100 중 1로 설정
    scene_manager.add_detector(ContentDetector(threshold=1))

    video_manager.set_downscale_factor()

    video_manager.start()
    scene_manager.detect_scenes(frame_source=video_manager)

    scene_list = scene_manager.get_scene_list()
    print(">>>", f'{len(scene_list)} scenes detected!')  # 전환 인식이 된 장면의 수

    save_images(scene_list,
                video_manager,
                num_images=1,
                image_name_template='$SCENE_NUMBER',
                output_dir=capture_path)

    write_scene_list_html(default_path + 'SceneDetectResult.html', scene_list)

    captured_timeline_list = []  # 전환된 시점을 저장할 리스트 함수
    for scene in scene_list:
        start, end = scene

        # 전환 시점 저장
        captured_timeline_list.append(start.get_seconds())

    print("[전환장면 캡처 종료] 영상 내 전환 시점을 기준으로 이미지 추출을 종료합니다\n")
    return captured_timeline_list
Exemple #3
0
def get_shots(video_path, downscale_factor=None, threshold=30):
    """
    

    Parameters
    ----------
    video_path : scenedetect VideoManager object.
    downscale_factor : Factor by which to downscale video to improve speed.
    threshold : Cut detection threshold.

    Returns
    -------
    cut_tuples : A list of tuples where each tuple contains the in- and out-frame of each shot.

    """
    #print('Detecting cuts...')
    video = VideoManager([video_path])
    video.set_downscale_factor(downscale_factor)
    scene_manager = SceneManager()
    scene_manager.add_detector(ContentDetector(threshold=threshold))
    video.start()
    scene_manager.detect_scenes(frame_source=video)
    shot_list = scene_manager.get_scene_list()
    cut_tuples = []
    for shot in shot_list:
        cut_tuples.append((shot[0].get_frames(), shot[1].get_frames()))
    video.release()
    return cut_tuples
 def _create_detector(self):
     if self.detector_type == 'content':
         return ContentDetector(threshold=self.threshold,
                                min_scene_len=self.min_scene_len)
     else:
         return ThresholdDetector(threshold=self.threshold,
                                  min_scene_len=self.min_scene_len)
Exemple #5
0
def scene_detect(opt):

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

    video_manager.set_downscale_factor()

    video_manager.start()

    scene_manager.detect_scenes(frame_source=video_manager)

    scene_list = scene_manager.get_scene_list(base_timecode)

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

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

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

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

    return scene_list
Exemple #6
0
def extract_shots_with_pyscenedetect(src_video,
                                     threshold=0,
                                     min_scene_length=15):
    video_manager = VideoManager([src_video])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    scene_manager.add_detector(ContentDetector(threshold, min_scene_length))
    base_timecode = video_manager.get_base_timecode()
    scene_list = []

    try:
        start_time = base_timecode
        video_manager.set_duration(start_time=start_time)
        video_manager.set_downscale_factor(downscale_factor=1)
        video_manager.start()
        scene_manager.detect_scenes(frame_source=video_manager, frame_skip=0)
        scene_list = scene_manager.get_scene_list(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(),))
        write_csv(src_video, scene_list)
    except:
        print("error")
    finally:
        video_manager.release()
    return scene_list
def get_scene_startFrames(filepath):
    # Create a video_manager point to video file testvideo.mp4. Note that multiple
    # videos can be appended by simply specifying more file paths in the list
    # passed to the VideoManager constructor. Note that appending multiple videos
    # requires that they all have the same frame size, and optionally, framerate.
    video_manager = VideoManager([filepath])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

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

        # Start video_manager.
        video_manager.start()

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

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

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

    finally:
        video_manager.release()

    return scene_startFrames
Exemple #8
0
def frameExtraction(video, method, treshold):

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

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

    base_timecode = video_manager.get_base_timecode()

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

    # Start video_manager.
    video_manager.start()

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

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

    cap = cv2.VideoCapture(video)

    for i, scene in enumerate(scene_list):
        i = i + 1
        cut_frame = scene[0].get_frames()
        cap.set(1, cut_frame)
        ret, frame = cap.read()
        frame_name = "shot " + str(i) + ".jpg"
        cv2.imwrite(frame_name, frame)
Exemple #9
0
def find_scenes(video_path, threshold=30.0):
    # Create our video & scene managers, then add the detector.
    video_manager = VideoManager([video_path])
    scene_manager = SceneManager()
    scene_manager.add_detector(ContentDetector(threshold=threshold))

    # Improve processing speed by downscaling before processing.
    video_manager.set_downscale_factor()

    # Start the video manager and perform the scene detection.
    video_manager.start()
    time = scene_manager.detect_scenes(frame_source=video_manager)

    # Each returned scene is a tuple of the (start, end) timecode.

    times = scene_manager.get_scene_list(time)

    video_splitter.split_video_ffmpeg(
        video_path,
        times,
        '$VIDEO_NAME - Scene $SCENE_NUMBER',
        video_path,
        arg_override='-c:v libx264 -preset fast -crf 21 -c:a aac',
        hide_progress=False,
        suppress_output=False)
Exemple #10
0
def test_content_detector(test_movie_clip):
    """ Test SceneManager with VideoManager and ContentDetector. """
    for threshold, start_frames in TEST_MOVIE_CLIP_GROUND_TRUTH_CONTENT:
        vm = VideoManager([test_movie_clip])
        sm = SceneManager()
        sm.add_detector(ContentDetector(threshold=threshold))

        try:
            video_fps = vm.get_framerate()
            start_time = FrameTimecode('00:00:50', video_fps)
            end_time = FrameTimecode('00:01:19', video_fps)

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

            vm.start()
            sm.detect_scenes(frame_source=vm)
            scene_list = sm.get_scene_list()
            assert len(scene_list) == len(start_frames)
            detected_start_frames = [
                timecode.get_frames() for timecode, _ in scene_list
            ]
            assert all(x == y
                       for (x, y) in zip(start_frames, detected_start_frames))

        finally:
            vm.release()
def test_scene_list(test_video_file):
    """ Test SceneManager get_scene_list method with VideoManager/ContentDetector. """
    vm = VideoManager([test_video_file])
    sm = SceneManager()
    sm.add_detector(ContentDetector())

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

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

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

        assert num_frames == end_time.get_frames() + 1

        scene_list = sm.get_scene_list(base_timecode)

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

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

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

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

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

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

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

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

            # 获取检测到的场景列表。
            scene_list = scene_manager.get_scene_list(base_timecode)
            # 与FrameTimecodes一样,如果是,则可以对scene_list中的每个场景进行排序
            # 场景列表变为未排序。
            # print('List of scenes obtained:')
            # print(scene_list)
            # 如果scene_list不为空,整理结果列表,否则,视频为单场景
            re_scene_list = []
            if scene_list:
                for i, scene in enumerate(scene_list):
                    # print('%d , %d' % (scene[0].get_frames(), scene[1].get_frames()))
                    re_scene = (scene[0].get_frames(),
                                scene[1].get_frames() - 1)
                    re_scene_list.append(re_scene)
            else:
                re_scene = (0, frames_num - 1)
                re_scene_list.append(re_scene)
            # 输出切分场景的列表结果
            print("\n")
            print(re_scene_list)
            print("\n")
            # for item in re_scene_list:
            #     assert item[1] - item[0] + 1 >= 3
            result_list.append(re_scene_list)
        finally:
            video_manager.release()
    with open(os.path.join(dirpath, 'scene.pickle'), 'wb') as f:
        pickle.dump(result_list, f)
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()
Exemple #16
0
def getSceneList(name="testvideo.mp4", start_frame=0):

    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([name])
    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:

        start_time = base_timecode + start_frame  # + 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)

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

        # 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(),
            ))
        #import code
        #code.interact(local=locals())
        a = [(scene_start.get_frames(), scene_end.get_frames())
             for scene_start, scene_end in scene_list]
        a[-1] = (a[-1][0], a[-1][-1] + start_frame)
        return a
    finally:
        video_manager.release()
Exemple #17
0
async def hello(request):
    entry_time = time.time()
    # file_path = request.app['file_path']
    file_key, cache_conn = app['cache_params']
    video_file = tempfile.NamedTemporaryFile(delete=False)
    try:
        file_byte = cache_conn.get(file_key)
        if file_byte is None:
            file_path = app['file_path']
            with open(file_path, 'rb') as f:
                file_byte = f.read()
            cache_conn.set(file_key, file_byte)
        video_file.write(file_byte)
        # print(video_file.name)
        video_file.close()
        video_file_name = video_file.name
        video_manager = VideoManager([video_file_name])
        print("load time: {}".format(time.time()-entry_time))
        stats_manager = StatsManager()
        scene_manager = SceneManager(stats_manager)
        scene_manager.add_detector(ContentDetector())
        base_timecode = video_manager.get_base_timecode()
        
        try:
            start_time = base_timecode # 00:00:00.667
            # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
            end_time = start_time + 60
            video_manager.set_duration(start_time=start_time, end_time=end_time)

            # Set downscale factor to improve processing speed (no args means default).
            video_manager.set_downscale_factor()
    
            # Start video_manager.
            video_manager.start()
    
            # Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager)
    
            # Obtain list of detected scenes.
            scene_list = scene_manager.get_scene_list(base_timecode)
            # Like FrameTimecodes, each scene in the scene_list can be sorted if the
            # list of scenes becomes unsorted.
    
            output_string = 'List of scenes obtained:\n'
            for i, scene in enumerate(scene_list):
                output_string += ('     Scene %2d: Start %s / Frame %d, End %s / Frame %d\n' % (
                    i+1,
                    scene[0].get_timecode(), scene[0].get_frames(),
                    scene[1].get_timecode(), scene[1].get_frames(),))
        finally:
            video_manager.release()        
    finally:
        os.unlink(video_file.name)
        video_file.close()
    print("overall latency:{}".format(time.time()-entry_time)) 
    return web.Response(text="%s" % output_string)
Exemple #18
0
def keyframe_detection_with_scenedetect(parameters):
	video = VideoManager([parameters.input_file])
	scene = SceneManager()
	scene.add_detector(ContentDetector(threshold = 30.0))

	video.set_downscale_factor()
	video.start()
	scene.detect_scenes(frame_source = video)

	return scene.get_scene_list()
    def find_scenes(self, video_folder, threshold=50.0):
        video_paths = os.listdir(video_folder)
        # self.data.set_index("id", inplace=True)
        ids = self.data["id"]
        sep_times = {}
        for i, vp in enumerate(video_paths):
            if i % 100 == 0:
                self.logger.critical("Extracting scene frames, at %d/%d" %
                                     (i, len(video_paths)))
            id = vp.split('.')[0]
            # row = self.data.loc[int(id)]
            sep_times[id] = []
            video_path = os.path.join(video_folder, vp)

            # Create our videos & scene managers, then add the detector.
            video_manager = VideoManager([video_path], logger=self.logger)
            scene_manager = SceneManager()
            scene_manager.add_detector(ContentDetector(threshold=threshold))

            # Improve processing speed by downscaling before processing.
            # video_manager.set_downscale_factor(2)

            # Start the videos manager and perform the scene detection.
            video_manager.start()
            scene_manager.detect_scenes(frame_source=video_manager)

            scene_list = scene_manager.get_scene_list()
            # Each returned scene is a tuple of the (start, end) timecode.
            save_images(scene_list,
                        video_manager,
                        image_name_template='%s-Scene-$SCENE_NUMBER' % id,
                        num_images=1,
                        output_dir=os.path.join(config.data_folder,
                                                'scene_cover'))

            for scene in scene_list:
                sta_sec = scene[0].get_seconds()
                end_sec = scene[1].get_seconds()
                sep_times[id].append((sta_sec, end_sec))

            # print(id)
            # for i, scene in enumerate(scene_list):
            #     print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
            #         i + 1,
            #         1000 * scene[0].get_seconds(), scene[0].get_frames(),
            #         1000 * scene[1].get_seconds(), scene[1].get_frames()))
            # separate_texts = json.loads(row["separated_text"])
            # separate_timestamps = json.loads(row["separate_timestamps"])
            # for text, timestamp in zip(separate_texts, separate_timestamps):
            #     print(text, timestamp)
            # print('-' * 30)
            video_manager.release()
        sep_times_list = [json.dumps(sep_times[str(id)]) for id in ids]
        self.data["scene_times"] = sep_times_list
    def __init__(self):
        '''
        Init the detection manager.
        '''

        print("Init the shot detector")
        print("PySceneDetect version being used: %s" %
              str(scenedetect.__version__))
        self.stats_manager = StatsManager()
        self.scene_manager = SceneManager(self.stats_manager)
        self.scene_manager.add_detector(ContentDetector())
 def get_plans(video_path, threshold):
     video_manager = VideoManager([video_path])
     base_timecode = video_manager.get_base_timecode()
     video_manager.set_downscale_factor()
     video_manager.start()
     stats_manager = StatsManager()
     plan_manager = SceneManager(stats_manager)
     plan_manager.add_detector(ContentDetector(threshold=threshold))
     plan_manager.detect_scenes(frame_source=video_manager)
     plan_list = plan_manager.get_scene_list(base_timecode)
     return plan_list
Exemple #22
0
    def scenedetect(self, video: Path):
        # Skip scene detection if the user choosed to
        if self.skip_scenes:
            return ''

        try:

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

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

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

            # Work on whole video
            video_manager.set_duration()

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

            # Start video_manager.
            video_manager.start()

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

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

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

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

        except Exception:

            print('Error in PySceneDetect')
            sys.exit()
Exemple #23
0
def scenes(video):
    """
    convert video file into list of scenes based on frame range and fps
    :param: video: video filename
    """
    video_manager = VideoManager([video])
    scene_manager = SceneManager()
    scene_manager.add_detector(ContentDetector())
    video_manager.set_downscale_factor()
    video_manager.start()
    scene_manager.detect_scenes(frame_source=video_manager)
    scene_list = scene_manager.get_scene_list(video_manager.get_base_timecode())
    return scene_list
Exemple #24
0
def find_scenes(video_path, threshold=30.0):
    # Create our video & scene managers, then add the detector.
    video_manager = VideoManager([video_path])
    scene_manager = SceneManager()
    scene_manager.add_detector(ContentDetector(threshold=threshold))
    # Base timestamp at frame 0 (required to obtain the scene list).
    base_timecode = video_manager.get_base_timecode()
    # Improve processing speed by downscaling before processing.
    video_manager.set_downscale_factor()
    # Start the video manager and perform the scene detection.
    video_manager.start()
    scene_manager.detect_scenes(frame_source=video_manager)
    # Each returned scene is a tuple of the (start, end) timecode.
    return scene_manager.get_scene_list(base_timecode)
    def scenedetect(self, video):
        # PySceneDetect used split video by scenes and pass it to encoder
        # Optimal threshold settings 15-50
        video_manager = VideoManager([video])
        scene_manager = SceneManager()
        scene_manager.add_detector(ContentDetector(threshold=self.threshold))
        base_timecode = video_manager.get_base_timecode()

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

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

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

            # Start video_manager.
            video_manager.start()

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

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

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

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

            # We only write to the stats file if a save is required:
            if self.scenes:
                with open(join(self.here, self.scenes), 'w') as stats_file:
                    stats_file.write(scenes)
                    return scenes
            else:
                return scenes
        except:
            print('Error in PySceneDetect')
            sys.exit()
    def run(self):
        self.logger.info('run(): started')
        self.logger.info("Running PySceneDetect API test...")
        self.logger.info("PySceneDetect version being used: %s" %
                         str(scenedetect.__version__))

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

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

            # Set video_manager duration
            start_time = base_timecode

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

            # Start video_manager.
            video_manager.start()

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

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

        finally:
            video_manager.release()
            self.logger.info('run(): finished')
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
Exemple #28
0
def detect_scenes(video_pth: Path,
                  scene_detection_threshold: int,
                  min_scene_length: int,
                  frame_skip: Optional[int],
                  show_progress: bool = False):
    """
    Detect scenes in a video
    :param video_pth: Path to video file to process
    :param scene_detection_threshold: Threshold for detection of change in scene
    :param min_scene_length: Minimum allowed length of scene, in frames
    :param frame_skip:  Number of frames to skip, peeding up the detection time at the expense of accuracy
    :param show_progress: If true, will show a tqdm progress bar of scene detection
    :return:
    """
    time_start = time.time()
    logger.info(
        f"Starting detect_scene with args: \n"
        f"{video_pth=}\n{scene_detection_threshold=}\n{min_scene_length=}\n{frame_skip=}"
    )
    video_manager = VideoManager([str(video_pth)])
    scene_manager = SceneManager()
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(
        ContentDetector(threshold=scene_detection_threshold,
                        min_scene_len=min_scene_length))
    base_timecode = video_manager.get_base_timecode()

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

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

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        time_taken = time.time() - time_start
        logger.info(
            f"Detection of scenes complete in {round(time_taken, 1)} seconds")
        logger.info('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            logger.info(
                f'    Scene {i + 1}: Start {scene[0].get_timecode()} / Frame {scene[0].get_frames()}, '
                f'End {scene[1].get_timecode()} / Frame {scene[1].get_frames()}'
            )
        return scene_list
    finally:
        video_manager.release()
Exemple #29
0
def find_scene(video, results):
    video_manager = VideoManager([video])
    scene_manager = SceneManager()
    scene_manager.add_detector(ContentDetector(threshold=THRESHOLD))
    base_timecode = video_manager.get_base_timecode()
    video_manager.set_downscale_factor()
    video_manager.start()
    scene_manager.detect_scenes(frame_source=video_manager)
    scene_list = scene_manager.get_scene_list(base_timecode)
            # Each scene is a tuple of (start, end) FrameTimecodes.
    for i, scene in enumerate(scene_list):
        #scene[0].get_timecode(), scene[0].get_frames(),
        #scene[1].get_timecode(), scene[1].get_frames(),))
        results.append((scene[0].get_timecode()))
    video_manager.release()
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()