视频转图片
ffmpeg -i cctv1.mp4 -t 1 -s 640x360 -r 15 frame%03d.jpg
- -t 需要转换的视频时长
- -s 设置目标图片的分辨率
- -r 帧率,每秒产生图片的数量
- frame%03d.jpg 生成图片文件的名称
$ ffmpeg -i cctv1.mp4 -t 1 -s 640x360 -r 15 frame%03d.jpg
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cctv1.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:23.72, start: 0.000000, bitrate: 2154 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
[swscaler @ 0x55ff375f3900] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'frame%03d.jpg':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Stream #0:0(und): Video: mjpeg, yuvj420p(pc), 640x360, q=2-31, 200 kb/s, 15 fps, 15 tbn, 15 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc58.54.100 mjpeg
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
frame= 15 fps=0.0 q=24.8 Lsize=N/A time=00:00:01.00 bitrate=N/A dup=0 drop=7 speed=6.88x
$ ls
cctv1.mp4 frame002.jpg frame004.jpg frame006.jpg frame008.jpg frame010.jpg frame012.jpg frame014.jpg
frame001.jpg frame003.jpg frame005.jpg frame007.jpg frame009.jpg frame011.jpg frame013.jpg frame015.jpg
图片转视频
ffmpeg -f image2 -i frame%03d.jpg -r 25 output.mp4
- -f 源图片的格式
- frame%03d.jpg 源图片的名称
- -r 目标视频的帧率
$ ffmpeg -f image2 -i frame%03d.jpg -r 25 output.mp4
Input #0, image2, from 'frame%03d.jpg':
Duration: 00:00:00.60, start: 0.000000, bitrate: N/A
Stream #0:0: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 640x360, 25 fps, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
Output #0, mp4, to 'output.mp4':
Metadata:
encoder : Lavf58.29.100
Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuvj420p(pc), 640x360, q=-1--1, 25 fps, 12800 tbn, 25 tbc
Metadata:
encoder : Lavc58.54.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame= 15 fps=0.0 q=-1.0 Lsize= 114kB time=00:00:00.48 bitrate=1937.6kbits/s speed=3.31x
截图
ffmpeg -i cctv1.mp4 -y -f image2 -ss 00:00:03 -vframes 1 -s 640x360 1.jpg
- -y 强制覆盖已有文件
- -f 指定图片格式
- -ss 开始截图时刻
- -vframes 截图图片数量 1:一张图(40ms)
- -s 指定图片分辨率
$ ffmpeg -i cctv1.mp4 -y -f image2 -ss 00:00:03 -vframes 1 -s 640x360 1.jpg
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cctv1.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:23.72, start: 0.000000, bitrate: 2154 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 0x55b31c96c940] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to '1.jpg':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Stream #0:0(und): Video: mjpeg, yuvj420p(pc), 640x360, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc58.54.100 mjpeg
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
frame= 1 fps=0.0 q=5.6 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.128x
生产gif动图
ffmpeg -i cctv1.mp4 -t 5 -r 3 image1.gif
- -t 5 共截图5s
- -r 3 每秒3fps
- image1.gif 动图名称
$ ffmpeg -i cctv1.mp4 -t 5 -r 3 image1.gif
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cctv1.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:23.72, start: 0.000000, bitrate: 2154 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2088 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 78 kb/s (default)
Metadata:
handler_name : SoundHandler
File 'image1.gif' already exists. Overwrite ? [y/N] y
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> gif (native))
Press [q] to stop, [?] for help
[swscaler @ 0x563cd7bdab80] No accelerated colorspace conversion found from yuv420p to bgr8.
Output #0, gif, to 'image1.gif':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Stream #0:0(und): Video: gif, bgr8, 1920x1080, q=2-31, 200 kb/s, 3 fps, 100 tbn, 3 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc58.54.100 gif
frame= 15 fps= 14 q=-0.0 Lsize= 8867kB time=00:00:04.68 bitrate=15520.2kbits/s dup=0 drop=104 speed=4.31x
video:8867kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000220%
$ ffprobe image1.gif
Input #0, gif, from 'image1.gif':
Duration: 00:00:05.01, start: 0.000000, bitrate: 14497 kb/s
Stream #0:0: Video: gif, bgra, 1920x1080, 3 fps, 3 tbr, 100 tbn, 100 tbc
gif动图转视频
ffmpeg -f gif -i image1.gif image2.mp4
- -f gif 指定输入文件格式
- image2.mp4 指定输出文件名称
$ ffmpeg -f gif -i image1.gif image2.mp4
Input #0, gif, from 'image1.gif':
Duration: 00:00:05.01, start: 0.000000, bitrate: 14497 kb/s
Stream #0:0: Video: gif, bgra, 1920x1080, 3 fps, 3 tbr, 100 tbn, 100 tbc
Stream mapping:
Stream #0:0 -> #0:0 (gif (native) -> h264 (libx264))
Output #0, mp4, to 'image2.mp4':
Metadata:
encoder : Lavf58.29.100
Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv444p, 1920x1080, q=-1--1, 3 fps, 12288 tbn, 3 tbc
Metadata:
encoder : Lavc58.54.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame= 15 fps=5.2 q=-1.0 Lsize= 10315kB time=00:00:04.00 bitrate=21125.2kbits/s speed=1.38x
video:10314kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.010548%
$ ffprobe image2.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'image2.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:00:05.00, start: 0.000000, bitrate: 16900 kb/s
Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p, 1920x1080, 16898 kb/s, 3 fps, 3 tbr, 12288 tbn, 6 tbc (default)
Metadata:
handler_name : VideoHandler