![如何使用 ffmpeg 从 .flv 视频创建缩略图?](https://linux22.com/image/1336079/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20ffmpeg%20%E4%BB%8E%20.flv%20%E8%A7%86%E9%A2%91%E5%88%9B%E5%BB%BA%E7%BC%A9%E7%95%A5%E5%9B%BE%EF%BC%9F.png)
我正在使用以下命令从 flv 视频创建缩略图,图像已创建但出现错误(图像未打开)
ffmpeg -ss 00:00:10 -i output.mp4 -f image2 -s 150x150 -vframes 1 -c:v libx264 assetPathNew.jpg
ffmpeg version 0.9.2, Copyright (c) 2000-2012 the FFmpeg developers
built on Oct 22 2012 11:15:53 with gcc 4.4.4 20100630 (Red Hat 4.4.4-10)
configuration: --enable-libx264 --enable-gpl
libavutil 51. 32. 0 / 51. 32. 0
libavcodec 53. 42. 4 / 53. 42. 4
libavformat 53. 24. 2 / 53. 24. 2
libavdevice 53. 4. 0 / 53. 4. 0
libavfilter 2. 53. 0 / 2. 53. 0
libswscale 2. 1. 0 / 2. 1. 0
libpostproc 51. 2. 0 / 51. 2. 0
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 1970-01-01 00:00:00
encoder : Lavf52.64.2
Duration: 00:00:16.96, start: 0.000000, bitrate: 873 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 320x240, 825 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc
Metadata:
creation_time : 1970-01-01 00:00:00
handler_name : VideoHandler
Stream #0:1(und): Audio: mp3 (mp4a / 0x6134706D), 22050 Hz, stereo, s16, 40 kb/s
Metadata:
creation_time : 1970-01-01 00:00:00
handler_name :
[buffer @ 0xa1c7620] w:320 h:240 pixfmt:yuv420p tb:1/1000000 sar:0/1 sws_param:
[scale @ 0xa1befa0] w:320 h:240 fmt:yuv420p -> w:150 h:150 fmt:yuv420p flags:0x4
[libx264 @ 0xa1bc540] using cpu capabilities: none!
[libx264 @ 0xa1bc540] profile High, level 1.1
Output #0, image2, to 'assetPathNew.jpg':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 1970-01-01 00:00:00
encoder : Lavf53.24.2
Stream #0:0(und): Video: h264, yuv420p, 150x150, q=-1--1, 90k tbn, 25 tbc
Metadata:
creation_time : 1970-01-01 00:00:00
handler_name : VideoHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 -> libx264)
Press [q] to stop, [?] for help
frame= 1 fps= 0 q=28.0 Lsize= 0kB time=00:00:00.-4 bitrate= -0.0kbits/s dup=0 drop=41
video:5kB audio:0kB global headers:0kB muxing overhead -100.000000%
[libx264 @ 0xa1bc540] frame I:1 Avg QP:32.24 size: 4707
[libx264 @ 0xa1bc540] mb I I16..4: 0.0% 43.0% 57.0%
[libx264 @ 0xa1bc540] 8x8 transform intra:43.0%
[libx264 @ 0xa1bc540] coded y,uvDC,uvAC intra: 94.8% 97.0% 74.0%
[libx264 @ 0xa1bc540] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 35% 10% 9% 12% 8% 7% 3% 10% 5%
[libx264 @ 0xa1bc540] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 22% 17% 13% 10% 7% 8% 6% 10% 8%
[libx264 @ 0xa1bc540] i8c dc,h,v,p: 49% 19% 19% 13%
[libx264 @ 0xa1bc540] kb/s:941.40
答案1
当你传递-c:v libx264
after时-i
,你告诉 FFmpeg 将输入视频流编码为 x264 视频。但是,你告诉它将视频写入image2
JPEG 文件中的格式。这当然行不通。你实际上可以在流映射中看到这一点:
Stream mapping:
Stream #0:0 -> #0:0 (h264 -> libx264)
因此,让我们让 FFmpeg 写入 JPEG 图像。使用一个简单的命令:
ffmpeg -ss 00:00:10 -i output.mp4 -s 150x150 -vframes 1 assetPathNew.jpg
这次,我们得到了正确的流映射:
Stream mapping:
Stream #0:0 -> #0:0 (h264 -> mjpeg)
请注意,FFmpeg 会自动选择正确的容器。因此,您不需要-f image2
。
使用150x150
可能会创建拉伸图像,并且不会保留输入的原始纵横比。您可以使用scale
过滤器自动调整大小,同时保持纵横比:
ffmpeg -ss 00:00:10 -i output.mp4 -filter:v scale=150:-1 -vframes 1 assetPathNew.jpg
这很可能会给你一个 150x113 的输出。如果你必须有 150x150 的尺寸,那么你可以添加pad
过滤器来填充额外的内容:
ffmpeg -ss 00:00:10 -i output.mp4 -filter:v "scale=150:-1,pad=iw:150:0:(ow-ih)/2" -vframes 1 assetPathNew.jpg