Advanced Python: how to generate video thumbnails with Python code

On Reddit is flooded with all kinds of robot accounts, the government has also very supportive of this behavior, if not meaningless to speak more than a robot can increase activity to attract real users work together to express their views, such as a week of " annoying Tuesday, "the post, and everybody is Tucao good place to live in all sorts of troubles, thus providing a complete set of developer API, while the SDK is not hard to find, there is an exhaustive list, we can according to their language preferences ad libitum.
Advanced Python: how to generate video thumbnails with Python code

The script is very simple, the real difficulty is that the video upload, Reddit requirements are very special, with the post title and video path is not enough, you also need to provide a thumbnail, SDK documentation also said, if you do not provide, it will automatically upload their Logo as a thumbnail of the video. This time, we need to reposition itself in the ffmpeg.

installation

brew install ffmpeg

Python also provides a layer of ffmpeg package - ffmpy, this layer is to translate the essence of the package the argument passed to the command line, call subprocess to execute. For example, an example of the official website

>>> import ffmpy
>>> ff = ffmpy.FFmpeg(
...     inputs={'input.mp4': None},
...     outputs={'output.avi': None}
... )
>>> ff.run()

Essentially run from the command line

ffmpeg -i input.mp4 output.avi

Generate thumbnails

Simple functions as a write, a given video path, taking the first frame as a thumbnail, save as jpg format


import ffmpy

def get_thumbnail_from_video(video_path):
    thumbnail_path = video_path.replace(".mp4", ".jpg")
    ff = ffmpy.FFmpeg(
        inputs={video_path: None},
        outputs={thumbnail_path: ['-ss', '00:00:00.000', '-vframes', '1']}
    )
    ff.run()
    return thumbnail_path

Here I simply assumed that the video formats are mp4, so the path of simple replacement the next, no more detailed examination. Similarly, in essence, it is the implementation of

ffmpeg -i video_path -ss 00:00:00.000 -vframes 1 thumbnail_path

When this posting videos and pictures at the same time given, the command line to get past the various Hit the jump operation is very convenient.

Guess you like

Origin blog.51cto.com/14646124/2460579