自己常用的ffmpeg命令

整理了工作中常用的ffmpeg命令,持续更新中

  • ffmpeg-4.1.1安装
    http://www.linuxfromscratch.org/blfs/view/8.4-systemd/multimedia/ffmpeg.html
  • ffmpeg configure
./configure --prefix=/usr/local --enable-shared --enable-gray --enable-version3 --enable-nonfree --enable-gpl --enable-avresample --enable-libfreetype --enable-libmp3lame   --enable-libx264   --enable-libtensorflow  --enable-libx265 --enable-libvmaf --enable-version3 
  • 关闭插帧抽帧
-vsync 0
  • 图片转视频
    framerate 设定放在-i前面
ffmpeg -r 24000/1001 -i pngs/out%4d.png -vcodec libx265 -pix_fmt yuv422p -crf 10 test.mp4
  • 覆盖同名文件
-y
  • 截图命令
    截取一段时间的视频
ffmpeg  -ss 00:00:00 -t 00:01:00 -i CJM_2_480p.mp4 -vcodec copy CM_2_480p.mp4  

时间也可以用秒表示 如 15.5 表示15.5秒
在某个时间点截取60帧

ffmpeg -i building.mp4 -ss 0:0 -vframes 60 -f image2 ./img/%03d.png

每隔1秒截一帧

ffmpeg -i test.mp4 -y -f image2 -vf fps=1 ./test_WZ/%03d.bmp
  • 改变视频大小
ffmpeg -i input.avi -vf scale="720:480" output.avi
  • 改变yuv大小
ffmpeg -s:v 1280x720 -r 24  -i big_buck_bunny_720p24.yuv -vf scale=640x360 -c:v rawvideo -pix_fmt yuv420p  big_buck_bunny_360p24.yuv
  • 出现错误:Too many packets buffered for output stream 0:1.
    解决:添加命令 -max_muxing_queue_size 1024
  • 执行ffmpeg脚本时出现"at least 3 arguments were expected, only 1 given in string"错误
    解决方法: https://unix.stackexchange.com/questions/36310/strange-errors-when-using-ffmpeg-in-a-loop
    在ffmpeg之前添加
  • yuv转mp4
ffmpeg -s 1280x720 -pix_fmt nv12 -f rawvideo -i SYUV.yuv -vcodec libx264 syuv.mp4
  • mp4转yuv
ffmpeg -i sample.mp4 -c:v rawvideo -pix_fmt yuv420p out.yuv
  • 拼合视频
ffmpeg -i 360x640-crf23-4.mp4 -i sharp4.mp4 -filter_complex "nullsrc=size=720x640[base];[0:v]copy[left];[1:v]copy[right];[base][left]overlay=shortest=1[tmp1];[tmp1][right]overlay=shortest=1:x=360" -vcodec libx265 -x265-params preset=veryslow:crf=18 result2.mp4
ffmpeg -i ori.mp4 -i sharp1.mp4  -filter_complex "[1:v] crop=iw/2:ih:0:0 [out1]; [0:v][out1] overlay=W/2:0" -vcodec h264 -crf 10 360result1.mp4
ffmpeg -i out1.mp4 -i out2.mp4 -i out3.mp4 -i out4.mp4 -filter_complex "[0:v]pad=iw*2:ih*2[a];[a][1:v]overlay=w[b];[b][2:v]overlay=0:h[c];[c][3:v]overlay=w:h" out.mp4

[0:v]copy[left] 中1表示的是操作对象的编号,copy表示操作(也可以用crop等操作),left表示操作输出的结果命名为left,[base][left]表示两个视频叠加,base在下,left在上

  • ffplay播放yuv
ffplay -f rawvideo -video_size 360x640 -loop 0 ori4.yuv
  • 旋转视频
ffmpeg -i outdoor480.mp4 -metadata:s:v rotate="90" -codec copy outdoortest480.mp4
  • 压缩单张图片
ffmpeg -f image2 -i test.png -vcodec libx264 -crf 30  1.mp4
ffmpeg -i 1.mp4 -f image2 1.png
  • 模糊滤镜
    ffmpeg -i input.mpg -vf boxblur=1.5:1 output.mp4
    ffmpeg -i input.jpg -vf smartblur=5:0.8:0 output.png
  • ffmpeg 剪切画面
    自己常用的ffmpeg命令_第1张图片
    例:
ffmpeg -i input -vf crop=iw/3:ih:0:0 output
ffmpeg -i input -vf crop=iw/3:ih:iw/3:0 output
ffmpeg -i input -vf crop=iw/3:ih:iw/3*2:0 output
  • 视频片段拼接
    FFmpeg has three concatenation methods.
  1. concat video filter
  -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" \
  -map "[v]" -map "[a]" output.mkv

Note that this method performs a re-encode.
2. concat demuxer

$ cat mylist.txt
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
$ ffmpeg -f concat -i mylist.txt -c copy output

for Windows:

(echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt
ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4

使用这种方法合mp4遇到Non-monotonous DTS 问题时,先把mp4转ts,再合
3. concat protocol

ffmpeg -i "concat:input1|input2" -codec copy output

This method does not work for many formats, including MP4, due to the nature of these formats and the simplistic concatenation performed by this method.
Which one to use
concat filter: Use if your inputs do not have the same parameters (width, height, etc), or are not the same formats/codecs, or if you want to perform any filtering. (You could re-encode just the inputs that don’t match so they share the same codec and other parameters, then use the concat demuxer to avoid re-encoding the other inputs).
concat demuxer: Use when you want to avoid a re-encode and your format does not support file level concatenation (most files used by general users do not support file level concatenation).
concat protocol: Use with formats that support file level concatenation (MPEG-1, MPEG-2 PS, DV). Do not use with MP4.
If in doubt try the concat demuxer.

  • 高斯模糊
ffmpeg -i video -vf gblur=sigma=0.6:steps=1 outVideo
  • 配置VMAF
    先git clone VMF源码,make&&make install 后 对ffmpeg ./configure --enable-libvmaf --enable-version3 即可
ffmpeg -r 50 -i CJM_5.mp4 -r 50 -i CJM_5_22_GSraisr.mp4 -lavfi libvmaf="psnr=1:ssim=1:enable_transform=1:log_path=vmaf.txt" -f null -
  • 插帧命令
ffmpeg -i GAME2.mp4 -filter_complex "minterpolate='fps=60'" test_game2.mp4
  • 倍(慢)速播放
ffmpeg -i test_nba.mp4 -filter:v "setpts=2*PTS" test.mp4
  • ffplay播放yuv
ffplay -f rawvideo -video_size 1920x1080 a.yuv
  • ffmpeg安装vmaf
    https://github.com/Netflix/vmaf/blob/master/resource/doc/libvmaf.md
./configure --enable-shared --enable-libx264  --enable-libx265 --enable-gpl --enable-libvmaf --enable-version3
ffmpeg -i main.mpg -i ref.mpg -lavfi libvmaf -f null -
  • 在os.system如何添加含有%d %s的ffmpeg命令行
    https://stackoverflow.com/questions/55564929/how-to-write-this-ffmpeg-command-in-python

  • ffprobe获取I帧索引
    https://superuser.com/questions/885452/extracting-the-index-of-key-frames-from-a-video-using-ffmpeg

ffprobe -select_streams v -show_frames -show_entries frame=pict_type -of csv test.mp4 | grep -n I | cut -d ':' -f 1 > frame_indices.txt
  • ffmpeg转换颜色空间
ffmpeg -i input.mp4 -vf "scale=in_color_matrix=bt601:out_color_matrix=bt709" output.mp4
  • ffmpeg/ffprobe 如何统计总帧数
    https://stackoverflow.com/questions/2017843/fetch-frame-count-with-ffmpeg
ffmpeg -i input.mkv -map 0:v:0 -c copy -f null -
  • 测PSNR
ffmpeg -s 960x540 -i movie_960x540.yuv -s 960x540 -i output_1.yuv -lavfi psnr="stats_file=psnr1.log" -f null -
  • 不同分辨率的视频先scale再测PSNR
ffmpeg -i 10547315.mp4 -i 23117385.mp4  -lavfi "[0]scale=3840:2160[a];[a][1]psnr=psnr.log" -f null -
  • ffmpeg超分
ffmpeg -i bunny.bmp -vf sr=dnn_backend=native:model=espcn.model  sr.bmp
  • ffplay 慢速播放
ffplay -loop 0 -s 960x540 -vf "setpts=0.5*PTS" denoise.yuv
  • x265
x265 --input-res 3840x2160 --fps 24000/1001 --input-csp i422 --input ./SR_422/68847251.yuv --preset slow  --crf 10 -o  test.mp4
  • 检测sceencut并输出相应帧号
ffmpeg -r 23.98 -i SDR_540p_test/18523430.mp4  -filter:v "select='gt(scene,0.2)',showinfo" -f null -
  • ffmpeg xcode编译
    https://www.jianshu.com/p/cac086219a58
./configure --prefix=/usr/local  --enable-libx264   --enable-gpl --enable-debug --enable-ffplay  --disable-optimizations 

Q:使用xcode单步调试ffmpeg的时候,添加–enable-shared没有办法进入下面库如libavfilter的断点
A:去掉 --enable-shared 或者手动给Makefile添加-g选项,另外-O0 -O3之类的指令也会影响编译优化
添加–disable-optimizations似乎没有用
在make的时候export MACOSX_DEPLOYMENT_TARGET=10.7,可以设定ffmpeg的deployment target版本

  • pix_fmt rgb48 le和be
    PIX_FMT_RGB48BE, ///< packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big-endian
    PIX_FMT_RGB48LE, ///< packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as little-endian

  • X265默认不支持10bit编码,需要在运行configure脚本时把more bit depth设为true

  • atadenoise
    参数p的作用是选定denoise的plane,
    比如默认值7=111,就是3通道,最大值15=1111,就是4通道,设为0时,不做denoise

  • 关于color range的问题

https://blog.csdn.net/yue_huang/article/details/77164579
设定输出为full range
https://stackoverflow.com/questions/39920815/video-normalization-with-ffmpeg
  • eq filter调整对比度亮度饱和度
ffmpeg -i original.vid -vf eq=gamma=1.5:saturation=1.3:brightness=0.1   -c:a copy  outfile.vid
  • 添加对第三方库的支持
./configure --prefix=/usr/local --enable-libx264 --enable-gpl --enable-debug --enable-ffplay --extra-libs="-lclahe -lmctd -lfftw3f -lfftw3f_threads -lpthread -lm -lstdc++ -ldl `pkg-config --cflags --libs opencv4 `" --extra-cflags=' -I/Users/zhangen/projects/ffilter/zmctd/include/' --extra-ldflags=' -L/Users/zhangen/projects/ffilter/zmctd/lib/mac/' 
  • ffmpeg-4.1版本vf_lut3d.c 387行代码有误,应为strncmp(line, "LUT_3D_SIZE ",11)

  • 264编码命令
    https://trac.ffmpeg.org/wiki/Encode/H.264

  • 使用vf copy截取一段时间的视频时,出现截取帧数和时长不对,即time stamp对不上的情况,在前面加上 -copyts 选项

  • 指定scale类型

-sws_flags bicubic
  • ffmpeg自定义filter 有参数的话 一定要加priv_class
    context中一定要加 const AVClass *class;

  • ffmpeg添加文字水印
    https://www.jianshu.com/p/9d24d81ca199

-vf drawtext=fontcolor=white:fontsize=40:fontfile=msyh.ttf:text='Hello World':x=0:y=100
  • mac编译调试ffmpeg configure
./configure --prefix=/usr/local  --enable-libx264   --enable-gpl --enable-debug --enable-ffplay --enable-libfreetype --enable-version3  --enable-libvmaf --extra-libs="-lclahe -lmctd -lfftw3f -lfftw3f_threads -lpthread -lm -lstdc++ -ldl `pkg-config --cflags --libs opencv4 `" --extra-cflags="$extra_cflags -I/Users/zhangen/projects/ffilter/zmctd/include/" --extra-ldflags="$extra_ldflags -L/Users/zhangen/projects/ffilter/zmctd/lib/mac"  
  • 视频拼接 同时添加水印
ffmpeg -i noisy.mp4 -i denoise.mp4 -filter_complex "nullsrc=size=1280x360[base];
[0:v]scale=640x360,drawtext=fontcolor=white:fontsize=30:fontfile=/Library/Fonts/Arial.ttf:text='Noisy':x=30:y=30[left];
[1:v]scale=640x360,clahe,eq=saturation=1.5: brightness=0.05,unsharp=5:5:0.5,drawtext=fontcolor=white:fontsize=30:fontfile=/Library/Fonts/Arial.ttf:text='MCTD':x=30:y=30[right];
[base][left] overlay=shortest=1[tmp1];
[tmp1][right] overlay=shortest=1:x=640" -sws_flags bicubic  demo0.mp4
ffmpeg -i noisy.mp4 -i mctd.mp4 -filter_complex "nullsrc=size=640x360[base];
[0:v]scale=640x360,drawtext=fontcolor=white:fontsize=30:fontfile=/Library/Fonts/Arial.ttf:text='Noisy':x=30:y=30[left];
[1:v]scale=640x360,drawtext=fontcolor=white:fontsize=30:fontfile=/Library/Fonts/Arial.ttf:text='MCTD':x=30:y=30[right];
[base][left] overlay=shortest=1[tmp1];
[tmp1][right] overlay=shortest=1:x=320" -sws_flags bicubic  demo0.mp4
  • ffmpeg 获取视频mv信息
    https://trac.ffmpeg.org/wiki/Debug/MacroblocksAndMotionVectors
    ffplay -flags2 +export_mvs douyin_ori.mp4 -vf codecview=mv=pf+bf+bb
    ffmpeg -flags2 +export_mvs -i input.mp4 -vf codecview=mv=pf+bf+bb output.mp4

  • debanding

ffmpeg -i face1.png -vf gradfun=4 face1_grad.png
ffmpeg -i face1.png -vf deband face1_deband.png

你可能感兴趣的:(FFmpeg,ffmpeg,计算机视觉)