Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With FFMPEG, create thumbnails proportional to the video's ratio [closed]

Tags:

I'm using the following command to create thumbnails with FFMPEG:

ffmpeg -itsoffset -1 -i video.avi -vcodec mjpeg -vframes 1 -an -f rawvideo -s 240x180 image.png 

And it works fine. However, when the video isn't 4:3 ratio, it will still create a 240x180 image, and the extra space will be painted black. Is there some variation of the command that will prevent this and give me an image proportional to the video's ratio? In other words, I want 240x180 to be the maximum size of the thumbnail, but not the minimum.

Extra points if the command creates a smaller image when the video is smaller than 240x180.

like image 212
Sophivorus Avatar asked Jan 27 '13 19:01

Sophivorus


People also ask

How to take thumbnails and screenshots using FFmpeg?

Thumbnails & Screenshots using FFmpeg – 3 Efficient Techniques 1 Periodic Screenshot/Thumbnail with Resizing. Here is another common use case that FFmpeg can solve easily – how do you take screenshots/thumbnails at regular intervals, and store them to JPG files ... 2 Screenshot/Thumbnail every 10 seconds. ... 3 Conclusion. ...

What can FFmpeg do for You?

Here is another common use case that FFmpeg can solve easily – how do you take screenshots/thumbnails at regular intervals, and store them to JPG files after resizing them? Here is a simple one-liner that can take care of creating a thumbnail and resizing it for you.

What is the best image format for a thumbnail image?

The other answers are fine... but for most "video" content, JPEG is a more space-efficient choice for a thumbnail image. This answer discusses JPEG quality settings.

What aspect ratio should I pad the video to?

The above answers are great, but most of them assume specific video dimensions and don't operate on a generic aspect ratio. You can pad the video to fit any aspect ratio, regardless of specific dimensions, using this: I use the ratio 16/9 in my example.


1 Answers

Use the scale filter:

ffmpeg -itsoffset -1 -i video.avi -vframes 1 -filter:v scale="280:-1"  image.png 

This will preserve the aspect ratio to achieve a width of 280 pixels. To increase the width to a maximum of 280 pixels but preserve the ratio, use:

scale='min(280\, iw):-1' 

Here, we'll just assume that your video is in landscape format, so we can set the maximum width to 280 and forget about the height, which should be 180 for a 3:2 video and 158 for 16:9 content.

Notes:

  • -vcodec mjpeg doesn't make sense when you're writing to a PNG file
  • -f rawvideo doesn't do anything here either
  • -an is not needed because FFmpeg can't write audio to a PNG file
like image 174
slhck Avatar answered Sep 18 '22 14:09

slhck