35

I need to create thumbnails for a video file once I've uploaded to a webapp running python.

How would I go about this... I need a library that can basically either do this for me, or that can read the image frames out of video files (of several formats) automatically.

Alterlife
  • 6,557
  • 7
  • 36
  • 49
  • 1
    See [this answer][1] for how to do it with Gstreamer and Python. [1]: http://stackoverflow.com/a/16478342/1049318 – David Planella May 11 '13 at 13:19
  • Anyone looking for a Django specific solution take a look at https://stackoverflow.com/a/76044945/9564152 – Eric O. Apr 18 '23 at 13:02

9 Answers9

34

I could not install ffvideo on OSX Sierra so i decided to work with ffmpeg.

OSX:

brew install ffmpeg

Linux:

apt-get install ffmpeg

Python 3 Code:

import subprocess
video_input_path = '/your/video.mp4'
img_output_path = '/your/image.jpg'
subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', '-vframes', '1', img_output_path])
Tobias Ernst
  • 4,214
  • 1
  • 32
  • 30
  • 7
    This is valid answer in 2019 – joe Jul 01 '19 at 08:38
  • I am getting Stream #0:0 -> #0:0 (h264 (native) -> png (native)) Press [q] to stop, [?] for help Cannot determine format of input stream 0:0 after EOF Error marking filters as finished error. Any suggetions ? – R.singh Sep 07 '20 at 09:52
14

You can use ffvideo

from ffvideo import VideoStream
pil_image = VideoStream('0.flv').get_frame_at_sec(5).image()
pil_image.save('frame5sec.jpeg')
zakhar
  • 411
  • 3
  • 6
12

A simple combination of PyMedia and PIL would do the trick for AVI, ASF, or MPEG files. PyMedia lets you extract the frames (using the decoder() routine), while PIL has a simple thumbnail() routine.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Peter Hansen
  • 21,046
  • 5
  • 50
  • 72
  • PyMedia doesn't work on python 3.x do you have another possible solution? – EduardoMaia Sep 22 '17 at 01:48
  • @EduardoMaia These days I would probably start by invoking an external package like ffmpeg, or libav at https://libav.org/ . – Peter Hansen Sep 26 '17 at 04:49
  • I'm using ffmpeg. It's just that, when you are working with other people it's easier if It's a python package. I just put it in the requirements.txt and they just install it with the other stuff... But when its a external package, they must install it, put the enviroment variable inside the activate script of their virtual enviroment, and all that stuff... – EduardoMaia Sep 27 '17 at 15:02
  • @EduardoMaia good point. Unfortunately I haven't had to do anything with video lately so I can't provide a better answer. If you find one, let me know and I can include it in the answer. – Peter Hansen Sep 27 '17 at 15:40
5

Use pyffmpeg

Install

pip install pyffmpeg

Use

from pyffmpeg import FFmpeg

inf = 'vid.mp4'
outf = 'thumb.jpg'

ff = FFmpeg()
ff.convert(inf, outf)
surge10
  • 622
  • 6
  • 18
4
import cv2
vcap = cv2.VideoCapture(filename)
res, im_ar = vcap.read()
while im_ar.mean() < threshold and res:
      res, im_ar = vcap.read()
im_ar = cv2.resize(im_ar, (thumb_width, thumb_height), 0, 0, cv2.INTER_LINEAR)
#to save we have two options
#1) save on a file
cv2.imwrite(save_on_filename, im_ar)
#2)save on a buffer for direct transmission
res, thumb_buf = cv2.imencode('.png', im_ar)
# '.jpeg' etc are permitted
#get the bytes content
bt = thumb_buf.tostring()

"threshold" is an integer. When you get a video frame it can be very black, white etc to get some good thumbnail you can specify the mean value of all the pixel in the frame.

Community
  • 1
  • 1
2

You could use the Youtube API for storage and transcoding and grab the feed thumbnails for free. Honestly, that's the easiest way to handle online video and I'm not just shilling a 3rd party service, I'm a very happy user of that API and the internal video paths I was able to delete thanks to it.

ironfroggy
  • 7,991
  • 7
  • 33
  • 44
  • If the website is a content management system and users can upload their own video, then will it be feasible to do the same? How would you store video in youtube in that case? You need to have a channel in YouTube, but you don't know whether the user has an account in YouTube. Am I correct or am I missing something? – Pran Kumar Sarkar Aug 17 '20 at 19:10
1

Look into PythonMagick, a Python interface to ImageMagick. That should have what you need. (Disclaimer: I haven't used the Python interface before, but I know ImageMagick is good mojo.)

perimosocordiae
  • 17,287
  • 14
  • 60
  • 76
1

You can use the Python script pyvideothumbnailer found on GitHub, which uses PyAV, MediaInfo and PIL/Pillow. It was written by me and is available under the BSD-3-clause license. It should do the complete job for you.

It has meaningful defaults. So you can just start creating your first preview thumbnails image of an individual video file by invoking:

pyvideothumbnailer [VIDEO FILE]

or to create thumbnails of all video files located in the current working directory:

pyvideothumbnailer

or to create thumbnails of all video files located in a directory:

pyvideothumbnailer [DIRECTORY CONTAINING VIDEOS]

or in case that you want to create previews of videos in subdirectories as well:

pyvideothumbnailer --recursive [DIRECTORY CONTAINING VIDEOS]

Its behavior can be controlled by command line options and a user-defined configuration file .pyvideothumbnailer.conf.

Here are two examples how it works creating preview thumbmnails of Big Buck Bunny. For further reference have a look at the GitHub wiki page.

Using defaults:

pyvideothumbnailer bbb_sunflower_1080p_60fps_normal.mp4

bbb_sunflower_1080p_60fps_normal_defaults mp4

White header font on black background, DejaVuSans TrueType font instead of the built-in font, adding a comment at the bottom of the header, custom preview thumbnails image width and 5 x 4 preview thumbnails:

pyvideothumbnailer --background-color black --header-font-color white --header-font DejaVuSans.ttf --timestamp-font DejaVuSans.ttf --comment-text "Created with pyvideothumbnailer" --width 1024 --columns 5 --rows 4 bbb_sunflower_1080p_60fps_normal.mp4

bbb_sunflower_1080p_60fps_normal mp4_example5

Harald Hetzner
  • 171
  • 1
  • 4
1

My Solution for both linux and windows os

  • windows download ffmeg compiled lib frmom https://www.gyan.dev/ffmpeg/builds/ add to environment variables

  • Linux apt-get install ffmpeg

  • Python Code

     img_output_path = Path(img_output_path).absolute()
    
     src_video_path = Path(src_video_path).absolute() 
    
     command = f"ffmpeg -i \"{src_video_path}\" -ss 00:00:00.000 -vframes 1 \"{img_output_path}\""
    
     os.system(_command)
    
Baher El Naggar
  • 181
  • 1
  • 5