[英]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
. 要从视频中获取200x100
缩略图,我会执行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. 但如果源视频的宽高比与200x100
,则缩略图会变形(拉伸或压扁,水平或垂直)并且看起来很糟糕。
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
? 有没有一种方法, ffmpeg
可以弄清楚例如500x200
视频是100px
太宽,从右边删除50px
和从左边50px
,使视频400x200
? And because 400x200
is the same aspect ratio as 200x100
, the thumbnail would have no distortion. 而且因为400x200
的宽高比与200x100
相同,所以缩略图没有失真。
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. 我知道有其他工具可以对ffmpeg
生成的缩略图执行此操作,但我更喜欢在ffmpeg
执行此操作而不必再次处理缩略图。
If your thumbnail size is 200x100 fixed, then run 如果缩略图大小为200x100,则运行
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. 缩放滤镜检查源和缩放的宽高比,以便一个尺寸适合200x100画布,另一个尺寸适合超调,除非它是完美匹配。 Then the crop filter crops it to 200x100 from the center thus taking care of the out of bounds region. 然后,裁剪过滤器从中心将其裁剪为200x100,从而处理出界区域。
您可以在比例过滤器中使用force_original_aspect_ratio
选项。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.