Using FFmpeg to Create Video Thumbnails in Python

Using FFmpeg to Create Video Thumbnails in Python

What is FFmpeg

FFmpeg is A complete, cross-platform solution to record, convert and stream audio and video. You can download it from here and install it by following this link for your platform. Detailed documentation of FFmpeg can be seen here. In this post, I will discuss how to user FFmpeg with Python.

Installation

  • On Ubuntu, you can install by running following command

sudo apt-get install ffmpeg

  • On Windows and MacOS, you can follow instructions provided on this page.

Creating Thumbnail

Python have a wrapper for FFmpeg called FFmpy. You can see its documentation here. You can install it by running following command.

pip install ffmpy

The command to use FFmpeg to create thumbnail of a video is below

ffmpeg -i input.mp4 -ss 00:00:04 -vframes 1 output.png

  • input.mp4 is input video
  • 00:00:04 is the position of thumbnail in video
  • -vframes 1 shows number of frames you want to get
  • output.png is the output image at provided position

This command can be converted into python by following code

from ffmpy import FFmpeg

ff = FFmpeg(inputs={'input.mp4': None}, outputs={"output.png": ['-ss', '00:00:4', '-vframes', '1']})

print ff.cmd

# Print result
# ffmpeg -i input.mp4 -ss 00:00:10 -vframes 1 output.png

ff.run()

In the above command, one frame was created. If you want to get multiple frames, you can use following command.

ffmpeg -i input.mp4 -vf fps=1 out%d.png

  • -vf fps=1 mean create frame after one minute

This will be done in python like this

from ffmpy import FFmpeg

ff = FFmpeg(inputs={'input.mp4': None}, outputs={"out%d.png": ['-vf', 'fps=1']})

print ff.cmd

# Print result
#ffmpeg -i input.mp4 -vf fps=1 out%d.png

ff.run()

Conclusion:

With above mentioned way of extracting video thumbnail using FFmpeg with python wrapper, we can run many more commands. We will come up with other commands in next post.

Advertisements

2 thoughts on “Using FFmpeg to Create Video Thumbnails in Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s