user779159
user779159

Reputation: 9602

Use ffmpeg to generate video thumbnail without any distortion

To get a 200x100 thumbnail from a video, I do ffmpeg -ss 100 -i /tmp/video.mp4 -frames:v 1 -s 200x100 image.jpg. But if the source video isn't in the same aspect ratio as 200x100, the thumbnail gets distorted (either stretched or squished, horizontally or vertically) and it looks bad.

Is there a way that ffmpeg can figure out for example that a 500x200 video is 100px too wide, and remove 50px from the right and 50px from the left, making the video 400x200? And because 400x200 is the same aspect ratio as 200x100, the thumbnail would have no distortion.

I know there are other tools that can do this to the thumbnails generated by ffmpeg, but I'd prefer doing it within ffmpeg and not having to process the thumbnails again.

Upvotes: 5

Views: 9394

Answers (2)

llogan
llogan

Reputation: 133743

You can use the force_original_aspect_ratio option in the scale filter.

ffmpeg -ss 100 -i /tmp/video.mp4 -frames:v 1 -q:v 2 -vf "scale=200:100:force_original_aspect_ratio=increase,crop=200:100" image.jpg

Upvotes: 6

Gyan
Gyan

Reputation: 93018

If your thumbnail size is 200x100 fixed, then run

ffmpeg -ss 100 -i /tmp/video.mp4 -vf "scale='if(gt(dar,200/100),100*dar,200)':'if(gt(dar,200/100),100,200/dar)',setsar=1,crop=200:100"  -frames:v 1 image.jpg

The scale filter checks the aspect ratio of the source and scale so that one dimension fits the 200x100 canvas and the other overshoots, unless it's a perfect match. Then the crop filter crops it to 200x100 from the center thus taking care of the out of bounds region.

Upvotes: 4

Related Questions