本文主要是介绍音视频剪辑|Windows|抽帧和合帧,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
什么是抽帧?
FFmpeg 抽帧(Extracting frames)的作用是从视频文件中按需提取单张或多张静止图像(帧),并将它们保存为图片文件(如 JPEG、PNG 等格式)。这一功能在以下场合十分有用:
-
制作GIF或截图
- 从视频中提取某一帧或几帧,可以生成有趣的动态GIF,或者静态截图用于文章、报告、演示文稿等。
-
视频剪辑预览
- 在视频剪辑的过程中,可以通过抽帧快速浏览视频中的关键帧,帮助剪辑师决定哪些镜头适合使用,或者标记重要的剪辑点。
-
机器学习与计算机视觉
- 在机器学习和计算机视觉领域,经常需要从视频中提取帧,用于训练深度学习模型。例如,进行物体检测、人脸识别、动作识别等任务时,大量的图像样本至关重要。
-
帧分析与修复
- 从视频中抽出帧,可以针对每一帧进行单独的分析和修复,比如去噪、色彩校正、锐化等操作。
-
视频制作特效
- 在动画制作、影视后期制作中,通过抽取视频中的关键帧,可以进行逐帧的手动编辑和绘制,制作出独特的视觉效果。
-
质量检查
- 对视频质量进行检查时,抽帧可以帮助技术人员精确地观察每一帧的画面质量,如清晰度、颜色准确度、是否存在瑕疵等。
-
帧序列输出
- 将视频转换为一系列连续的图片,可用于打印、拼接等多种用途,或者用于渲染成动画或视频游戏等场景。
FFmpeg 抽帧的命令一般格式如下:
Sh
ffmpeg -i C:\Users\Administrator\Desktop\111.mp4 -framerate 5 -v 5 -vfrwamers 20 -f image2 d:\222\image_%3d.jpeg
其中,frame_number
表示要抽取的帧编号,output_frame_%04d.png
表示输出图片的命名格式(每一张图片编号递增)。通过调整 select
过滤器的表达式,可以实现灵活的抽帧需求。
一,
抽帧实例
先查看原视频1的帧率信息这些:
C:\Users\Administrator>ffprobe -i C:\Users\Administrator\Desktop\111.mp4 -hide_banner
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\Administrator\Desktop\111.mp4':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf58.20.100Duration: 00:00:14.60, start: 0.000000, bitrate: 1983 kb/sStream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt470bg/smpte170m, progressive), 720x1280, 1927 kb/s, 30 fps, 30 tbr, 90k tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 47 kb/s (default)Metadata:handler_name : SoundHandlervendor_id : [0][0][0][0]
可以看到该视频在桌面,名称为111.mp4,一个视频流,时长是14.6秒,帧率是30fps,格式为h264,视频大小是720*1280;音频流为44100Hz,aac编码格式的
那么,很简单,可以计算出此视频大概会抽帧出来的图片为30*14.6=438张的图片
原视频2的音视频信息:
C:\Users\Administrator>ffprobe -i D:\tejing.mp4 -hide_banner
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\tejing.mp4':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Duration: 00:00:10.00, start: 0.000000, bitrate: 1250 kb/sStream #0:0[0x1](chi): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1112 kb/s, 25 fps, 25 tbr, 12800 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 libx264Stream #0:1[0x2](chi): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s (default)Metadata:handler_name : SoundHandlervendor_id : [0][0][0][0]
这个原视频2是我从电影里抽取的,视频长度是10秒,帧率是25fps,格式为h264,视频大小是1280*720;音频流为44100Hz,aac编码格式的
那么,很简单,可以计算出此视频大概会抽帧出来的图片为25*10=250张的图片
下面开始抽帧,全部帧都抽取出来:
ffmpeg -i D:\tejing.mp4 -framerate 10 d:\222\image_%3d.jpeg
那么,这个framerate 在这里其实好像没有什么意义,但可以看到确实全部抽帧,确实是250张图片,每个图片的清晰度也是比较好的,图片分辨率和视频是一致的都是1280*720
抽帧的同时图片分辨率修改为720*520
C:\Users\Administrator>ffmpeg -i D:\tejing.mp4 -framerate 10 -s 720*520 d:\222\image_%3d.jpeg -hide_banner
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\tejing.mp4':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Duration: 00:00:10.00, start: 0.000000, bitrate: 1250 kb/sStream #0:0[0x1](chi): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1112 kb/s, 25 fps, 25 tbr, 12800 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 libx264Stream #0:1[0x2](chi): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s (default)Metadata:handler_name : SoundHandlervendor_id : [0][0][0][0]
Stream mapping:Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 0000016583b3ce80] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'd:\222\image_%3d.jpeg':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Stream #0:0(chi): Video: mjpeg, yuvj420p(pc, progressive), 720x520 [SAR 104:81 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 mjpegSide data:cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
[out#0/image2 @ 00000165ffb1abc0] video:2715kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
frame= 250 fps=0.0 q=24.8 Lsize=N/A time=00:00:09.96 bitrate=N/A speed=40.9x
只抽取2秒的视频帧,2秒就是50张图片,可以看到确实只抽了50张图片,关键参数-vframes 50 :
C:\Users\Administrator>ffmpeg -i D:\tejing.mp4 -framerate 10 -vframes 50 -vf "select=not(mod(n\,15))" d:\222\image_%3d.jpeg -hide_banner
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\tejing.mp4':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Duration: 00:00:10.00, start: 0.000000, bitrate: 1250 kb/sStream #0:0[0x1](chi): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1112 kb/s, 25 fps, 25 tbr, 12800 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 libx264Stream #0:1[0x2](chi): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s (default)Metadata:handler_name : SoundHandlervendor_id : [0][0][0][0]
Stream mapping:Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 0000014c2483bf00] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'd:\222\image_%3d.jpeg':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Stream #0:0(chi): Video: mjpeg, yuvj420p(pc, progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 mjpegSide data:cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
[out#0/image2 @ 0000014c20a68300] video:1233kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
frame= 50 fps=0.0 q=24.8 Lsize=N/A time=00:00:01.96 bitrate=N/A dup=56 drop=0 speed=21.9x
每25帧抽一帧,均匀抽帧
因为帧率fps是25,因此抽取10个图片,均匀抽取,关键参数-vsync vfr:
C:\Users\Administrator>ffmpeg -i D:\tejing.mp4 -framerate 10 -vf "select=not(mod(n\,25))" -vsync vfr d:\222\image_%3d.jpeg -hide_banner
-vsync is deprecated. Use -fps_mode
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\tejing.mp4':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Duration: 00:00:10.00, start: 0.000000, bitrate: 1250 kb/sStream #0:0[0x1](chi): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1112 kb/s, 25 fps, 25 tbr, 12800 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 libx264Stream #0:1[0x2](chi): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s (default)Metadata:handler_name : SoundHandlervendor_id : [0][0][0][0]
Stream mapping:Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 000001c54248b500] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'd:\222\image_%3d.jpeg':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Stream #0:0(chi): Video: mjpeg, yuvj420p(pc, progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 mjpegSide data:cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
[out#0/image2 @ 000001c53e369500] video:342kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
frame= 10 fps=0.0 q=19.4 Lsize=N/A time=00:00:09.00 bitrate=N/A speed= 127x
每隔5帧抽一帧:
###这里提示-vsync参数已经过期,以后可能要废弃,要改为-fps_mode 这个参数
C:\Users\Administrator>ffmpeg -i D:\tejing.mp4 -framerate 10 -vf "select=not(mod(n\,5))" -vsync vfr d:\222\image_%3d.jpeg -hide_banner
-vsync is deprecated. Use -fps_mode
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\tejing.mp4':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Duration: 00:00:10.00, start: 0.000000, bitrate: 1250 kb/sStream #0:0[0x1](chi): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1112 kb/s, 25 fps, 25 tbr, 12800 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 libx264Stream #0:1[0x2](chi): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s (default)Metadata:handler_name : SoundHandlervendor_id : [0][0][0][0]
Stream mapping:Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 000001e0c80e8480] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'd:\222\image_%3d.jpeg':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Stream #0:0(chi): Video: mjpeg, yuvj420p(pc, progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 mjpegSide data:cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
[out#0/image2 @ 000001e0c3f690c0] video:1174kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
frame= 50 fps=0.0 q=24.8 Lsize=N/A time=00:00:09.80 bitrate=N/A speed=90.3x
因此,命令可以修改为如下:
ffmpeg -i D:\tejing.mp4 -framerate 10 -vf "select=not(mod(n\,5))" -fps_mode vfr d:\222\image_%3d.jpeg -hide_banner
二、
合帧实例
以上面的最后一个抽帧取出的图片为例,将这50张图片合成为一个新的视频:
说明:vcodec 是指定视频的编码格式,一般常用的视频格式是libx264,因为是50张图片合成,因此,指定帧率是25,这样视频的长度就是2秒了
C:\Users\Administrator>ffmpeg -f image2 -r 25 -i d:\222\image_%3d.jpeg -vcodec libx264 test1.mp4 -y -hide_banner
Input #0, image2, from 'd:\222\image_%3d.jpeg':Duration: 00:00:01.67, start: 0.000000, bitrate: N/AStream #0:0: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 1280x720 [SAR 1:1 DAR 16:9], 30 fps, 30 tbr, 30 tbn
Stream mapping:Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 00000176db102a80] using SAR=1/1
[libx264 @ 00000176db102a80] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 00000176db102a80] profile High, level 3.1, 4:2:0, 8-bit
[libx264 @ 00000176db102a80] 264 - core 164 r3172 c1c9931 - H.264/MPEG-4 AVC codec - Copyleft 2003-2023 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=22 lookahead_threads=3 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'test1.mp4':Metadata:encoder : Lavf60.16.100Stream #0:0: Video: h264 (avc1 / 0x31637661), yuvj420p(pc, bt470bg/unknown/unknown, progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 30 fps, 15360 tbnMetadata:encoder : Lavc60.31.102 libx264Side data:cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
[out#0/mp4 @ 00000176db17c380] video:628kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.228960%
frame= 50 fps=0.0 q=-1.0 Lsize= 630kB time=00:00:01.56 bitrate=3292.7kbits/s speed=5.83x
[libx264 @ 00000176db102a80] frame I:1 Avg QP:24.53 size: 24782
[libx264 @ 00000176db102a80] frame P:13 Avg QP:22.08 size: 17738
[libx264 @ 00000176db102a80] frame B:36 Avg QP:24.48 size: 10758
[libx264 @ 00000176db102a80] consecutive B-frames: 4.0% 0.0% 0.0% 96.0%
[libx264 @ 00000176db102a80] mb I I16..4: 26.2% 69.1% 4.8%
[libx264 @ 00000176db102a80] mb P I16..4: 19.6% 42.9% 1.4% P16..4: 7.3% 3.9% 2.3% 0.0% 0.0% skip:22.7%
[libx264 @ 00000176db102a80] mb B I16..4: 7.9% 21.8% 0.1% B16..8: 16.0% 5.9% 1.9% direct: 5.8% skip:40.6% L0:49.5% L1:44.2% BI: 6.3%
[libx264 @ 00000176db102a80] 8x8 transform intra:70.4% inter:88.9%
[libx264 @ 00000176db102a80] coded y,uvDC,uvAC intra: 47.9% 68.7% 3.6% inter: 12.4% 19.5% 0.2%
[libx264 @ 00000176db102a80] i16 v,h,dc,p: 52% 30% 16% 3%
[libx264 @ 00000176db102a80] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 17% 42% 4% 1% 1% 1% 2% 5%
[libx264 @ 00000176db102a80] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 43% 29% 12% 2% 3% 3% 3% 3% 2%
[libx264 @ 00000176db102a80] i8c dc,h,v,p: 39% 25% 33% 3%
[libx264 @ 00000176db102a80] Weighted P-Frames: Y:7.7% UV:0.0%
[libx264 @ 00000176db102a80] ref P L0: 50.0% 14.5% 17.6% 15.7% 2.2%
[libx264 @ 00000176db102a80] ref B L0: 72.8% 19.4% 7.8%
[libx264 @ 00000176db102a80] ref B L1: 86.9% 13.1%
[libx264 @ 00000176db102a80] kb/s:3084.74
查看合成的视频长度,确实是2秒了:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\Administrator\test1.mp4':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Duration: 00:00:02.00, start: 0.000000, bitrate: 2716 kb/sStream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, bt470bg/unknown/unknown, progressive), 1280x720 [SAR 1:1 DAR 16:9], 2710 kb/s, 25 fps, 25 tbr, 12800 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 libx264
上面的合帧命令-r 修改为5,那么,50/5 就是10秒的视频了,查看新的合成视频,可以看到确实是10秒了,也可以正常的打开播放:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\Administrator\test1.mp4':Metadata:major_brand : isomminor_version : 512compatible_brands: isomiso2avc1mp41encoder : Lavf60.16.100Duration: 00:00:10.00, start: 0.000000, bitrate: 790 kb/sStream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, bt470bg/unknown/unknown, progressive), 1280x720 [SAR 1:1 DAR 16:9], 789 kb/s, 5 fps, 5 tbr, 10240 tbn (default)Metadata:handler_name : VideoHandlervendor_id : [0][0][0][0]encoder : Lavc60.31.102 libx264
这篇关于音视频剪辑|Windows|抽帧和合帧的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!