Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

create a thumbnail from video python

Blog • Tutorials
Create a Thumbnail For Your Video with Python and FFMPEG

April 14, 2021 - Erikka Innes in Videos - Upload a Thumbnail, Python

Video thumbnails have the power to make a video more watchable. Thumbnails give people an initial impression of what your video is about. If you have time, the best thumbnail for a video is going to be one where you choose something representative of the video's content, and then add it. But sometimes, you might not have the time to manually choose the perfect thumbnail for every video you create. Sometimes, you might just want to quickly add a thumbnail that represents a frame from the video, and move on. You can easily accomplish this with FFMPEG for Python.
Pre-requisites

    Mac that's OSX10.7 or higher (as always, you can tweak the instructions for Linux or PC if you want to try that but it's a Mac focused tutorial)
    Homebrew - if you don't have it we'll include a little about installing it
    Xcode - you can add this as part of a homebrew installation
    api.video account - you can get one for FREE right here
    Python
    requests module for Python
    ffmpeg-python
    Homebrew - installation of FFMPEG is much easier with homebrew

Install FFMPEG

Sometimes an FFMPEG install can get complicated. You'll see a lot of people online asking why their installation doesn't work. If you have a Mac, there is an easy answer to this problem...hopefully. Let's go over one simple way you can install FFMPEG so it starts working at the command line for you right away. We're going to use the homebrew method, because it makes installation simple if you already have homebrew installed.
For Users Who Already Have Homebrew

If you've got homebrew installed, all you need to do is this:

brew install ffmpeg

That's it. It should install without issue. (Famous last words right?) Test that it works with this:

ffmpeg -version

If it's installed, you will get back a bunch of information about your configuration. If not you will get an error of some type. Troubleshooting installation is beyond the scope of this tutorial, but you can post questions in the community forum and we'll help you figure it out.
For Users Who Don't Have Homebrew

If you fall into this category, you may be wondering what homebrew is. It's a free, open-source software package manager. It simplifies installation of software on macOS and Linux.

To install homebrew, you'll need to run this command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

If you don't have Xcode, or the appropriate Xcode components installed, you'll have to install those. Fortunately, you'll be told what's missing so you can install it. Follow the instructions to install and if you run into problems, there is a lot of documentation online to help you through. We can't be more specific for Xcode, because there's a lot of ways your installation can differ from another one. However, if you need help, feel free to contact us in the community forum.
Thumbnail Creation Method - Using FFMPEG

You can use ffmpeg-python to create a thumbnail and upload it for use with your video. If you don't want to get into learning FFMPEG, you can also use api.video's endpoint that allows you to pick a time from the video, and have it set as the video thumbnail for you automatically.

NOTE: The thumbnail must be .jpg or .jpeg and 8MB or smaller.

If you're interested in trying it with ffmpeg-python, then here's the code:

import requests 
import ffmpeg
import sys

in_filename = "sample-mov-file.mov"
out_filename = "THUMBNAIL.jpg"

def generate_thumbnail(in_filename, out_filename):
    probe = ffmpeg.probe(in_filename)
    time = float(probe['streams'][0]['duration']) // 2
    width = probe['streams'][0]['width']
    try:
        (
            ffmpeg
            .input(in_filename, ss=time)
            .filter('scale', width, -1)
            .output(out_filename, vframes=1)
            .overwrite_output()
            .run(capture_stdout=True, capture_stderr=True)
        )
    except ffmpeg.Error as e:
        print(e.stderr.decode(), file=sys.stderr)
        sys.exit(1)

generate_thumbnail(in_filename, out_filename)
Comment

PREVIOUS NEXT
Code Example
Python :: dataframe concatenate 
Python :: how to make a histogram with plotly for a single variable 
Python :: how to make a window with tkinter 
Python :: rsa decryption 
Python :: how to make bak files with python 
Python :: get value of bit in integer python 
Python :: how to record youtube cc in python 
Python :: inverse box-cox transformation python 
Python :: split custom pytorch dataset 
Python :: pandas assign value to row based on condition 
Python :: python float to int 
Python :: iterate rows and columns dataframe 
Python :: python text recognition 
Python :: python custom class indexing 
Python :: python get image RGB data from URL 
Python :: how to split a dataframe into train and test 
Python :: msg91 python 
Python :: python comment header 
Python :: print f python 
Python :: python n range num list 
Python :: python global lists 
Python :: compare string python 
Python :: delete list using slicing 
Python :: permutation and combination in python 
Python :: rotate existing labels python 
Python :: python bot ban script 
Python :: How to take multiple inputs in one line in python using list comprehension 
Python :: python np array get dimantion 
Python :: bag of word scikit learn 
Python :: python how to add a new key to a dictionary 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =