Skip to content

How to extract keyframes using multithreading? #970

@jaycecd

Description

@jaycecd

Hello everyone, I want to save the keyframes of the video efficiently, I have read the cookbook for this:
https://pyav.org/docs/stable/cookbook/basics.html#saving-keyframes
I see that the speed can be improved by adding code:

thread_type = "AUTO"

in Threading.
But when I add the code, the speed and cpu usage(still ~10%) did not change.
I want to know how to extract keyframes using multithreading, here is my code:

def extract_video_keyframes(path_to_video, output_dir):
    '''
    :param path_to_video:
    :param output_dir:
    :return:
    '''
    try:
        image_list = []
        os.makedirs(output_dir, exist_ok=True)

        with av.open(path_to_video) as container:
            # container.streams.video[0].thread_type = "AUTO"

            stream = container.streams.video[0]
            stream.thread_type = "AUTO"
            stream.codec_context.skip_frame = 'NONKEY'
            for frame in container.decode(stream):
                image_list.append(frame.to_ndarray(format='rgb24'))
                print(frame)
                frame.to_image().save(os.path.join(output_dir, 'temporary-image-{:04d}.png'.format(frame.pts)))
            return image_list

    except Exception as e:
        print('Program error occurred:{}'.format(repr(e)))


if __name__ == "__main__":
    import time

    start = time.time()

    path_to_video = r'E:\Test sample\long video.mp4'
    output_dir = r'E:\Test sample\pyav'
    images = extract_video_keyframes(path_to_video, output_dir)
    print("The predict total time is {}".format(time.time() - start))

Software Environment:

  • Python 3.6.3
  • Pyav 8.0.3
  • Windows 10

Can anyone help me with this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions