音视频处理-常用FFmpeg命令集结

845 阅读9分钟

FFmpeg命令摘选

  1. 剪切视频: 参数:起始时间 要截取的时长
ffmpeg -ss 0:1:30 -t 0:0:20 -i input.avi -vcodec copy -acodec copy output.avi
ffmpeg -i input.avi  -ss 00:01:30 -t 00:00:20 -codec copy output.avi
ffmpeg -ss 90 -t 20  -i input.avi -vcodec copy -acodec copy output.avi

#上面的3种方式等价,时间格式支持 HH:MM:SS或者秒数。 -ss 开始时间, -t 持续时间, 
# -vcodec copy 保持原视频编码方式, -acodec copy 保持原音频编码方式。

#注意:-vcodec copy与-c:v以及-codec:v 这三种写法的功能是一样的,具体可参考:
#https://lists.ffmpeg.org/pipermail/ffmpeg-user/2017-February/035335.html
  1. 逆时针旋转90度: 顺时针取负数
ffmpeg -i VID.mp4 -metadata:s:v rotate="90" -codec copy VID2.mp4
  1. 截取gif:
#  色彩格式rgb24 帧率29
ffmpeg -i VID2.mp4 -ss 00:00:04 -t 10 -s 360*640 -pix_fmt rgb24 -r 29 jidu1.gif

# -s 设置分辨率
ffmpeg -ss 00:00:07 -t 00:00:20 -i input.mp4 -s 544*960 -r 15 res.gif
ffmpeg -ss 00:00:07 -t 10 -i input.mp4 -s 544*960 -r 15 res.gif

优化获取gif:

ffmpeg -ss 00:03:54 -t 00:00:20 -i fanren.mp4 -vf "fps=10,scale=960:-1:flags=lanczos,split[split1][split2];[split1]palettegen[pal];[split2][pal]paletteuse=dither=none" fanren.gif

脚本优化视频截取gif:

# https://wizyoung.dogcraft.xyz/video2gif-with-high-quality
set -e

# global filter
fps=15
scale=480:-1
interpolation=lanczos

if [ $5 != 0 ]
then 
	fps=$5
fi

if [ $6 != 0 ]
then 
	scale=$6:-1
fi
printf "the thired param :->-> %d \n" $fps

# for palettegen
max_colors=256  # up to 256
reserve_transparent=on
stats_mode=diff  # chosen from [full, diff, single]

# for paletteuse
dither=bayer  # chosen from [bayer, heckbert, floyd_steinberg, sierra2, sierra2_4a, none]
bayer_scale=3  # [0, 5]. only works when dither=bayer. higher means more color banding but less crosshatch pattern and smaller file size
diff_mode=rectangle  # chosen from [rectangle, none]
new=on  # when stats_mode=single and new=on, each frame uses different palette

ffmpeg -ss $3 -t $4 -i $1 -vf "fps=$fps,scale=$scale:flags=$interpolation,split[split1][split2];[split1]palettegen=max_colors=$max_colors:reserve_transparent=$reserve_transparent:stats_mode=$stats_mode[pal];[split2][pal]paletteuse=dither=$dither:bayer_scale=$bayer_scale:diff_mode=$diff_mode:new=$new" -y $2
# use example: 输入|输出|起始点|时长|fps|width
# > ./gifDuce.sh input.mp4 output.gif 00:06:44 00:00:14  10  960
# ./gifDuce.sh "input.mp4" "out-1.gif" 00:00:00 00:00:05  15  360
  1. 格式转换
ffmpeg -ss 00:00:55 -t 00:01:10 -i input.mp4 -vcodec copy -acodec copy output.avi
  1. 去除水印:
# x,y|w,h    含义:  logo左上角坐标|logo宽高
ffmpeg -i "input.mp4" -vf "delogo=x=9:y=293:w=136:h=94" -c:a copy "output.mp4"
ffmpeg -i jx0156.mp4 -metadata:s:v rotate="90" -codec copy jx0156.mp4
  1. 转换成H264码流
ffmpeg -i input.mp4 -vcodec h264 output.h264
# 其中 -i 表示输入文件, -vcodec h264 表示视频编解码方式为 H264。
  1. 转换成H265码流
ffmpeg -i input.mp4 -vcode hevc output.h265  
# 其中 -i 表示输入文件, -vcodec hevc 表示视频编解码方式为 H265,注意ffmpeg 中名称为 hevc,不是H265!
  1. 设置输出视频的分辨率
ffmpeg -i input.mp4 -vcodec h264 -s 1280x720 output.mp4
# 其中 -s 表示分辨率。
  1. 设置输出文件的音视频比特率
ffmpeg -i input_file  -vcodec h264 -b:v 10M -b:a 128K output_file
#其中 -b:v 10M 表示视频码率为10Mbps, -b:a 128K 表示音频码率为 128Kbps,注意FFMPEG对于码率控制,有时候不太准确,跟输入源有一定关系。
  1. 分离音视频流保存为不同文件
ffmpeg -i input_file -vcodec copy -an output_file_video  # 提取视频流 
ffmpeg -i input_file -acodec copy -vn output_file_audio  # 提取音频流
# 其中 -an 表示不处理音频, -vn 表示不处理视频。
  1. 合并多个音视频文件为一个文件
ffmpeg –i video_file –i audio_file –vcodec copy –acodec copy output_file
  1. 提取视频图像保存为图片文件(将视频分解为单幅图片)
ffmpeg -i input_file -r 1 -f image2 image-%3d.jpeg //提取图片
# 其中 -r 表示提取图像的频率,-f 表示输出格式, %3d 表示文件命名方式(也即生成的图片名为 image-001.jpeg, image-002.jpeg, …, image-999.jpeg)。
  1. 转换成YUV原始文件
ffmpeg -i input.mp4 -vcodec rawvideo -an output.yuv
# yuv文件会特别大
  1. YUV序列转出AVI文件
ffmpeg –s w*h –pix_fmt yuv420p –i input.yuv –vcodec mpeg4 output.avi 
  1. 控制关键帧间隔和B帧
ffmpeg -i input_file  -vcodec h264 -bf 0 -g 25 -s 854x480 -an -f m4v output_file
# 其中-bf 控制B帧数目,-g 控制关键帧间隔, -f 控制文件格式(format,注意与codec的区别)。
  1. 多个视频文件拼接
# 首先创建一个需要拼接的文件,例如 concat.txt,内容如下:

# file ‘input_0.avi’
# file ‘input_1.avi’
# file   'input_2.avi'

#然后执行如下命令

ffmpeg  -f concat -i concat.txt  -c copy output_merge.avi 
  1. 查看ffmpeg支持哪些yuv格式的转换
ffmpeg -pix_fmts

18.ffmpeg转换yuv格式命令 将一种YUV格式转换为另一种YUV格式

ffmpeg -pix_fmt yuv420p -s 176x144 -i carphone_qcif.yuv -pix_fmt nv12 carphone_qcif_nv12.yuv

19.ffmpeg命令行yuv缩放命令

ffmpeg -s:v 1920x1080 -r 25 -i input.yuv -vf scale=960:540 -c:v rawvideo -pix_fmt yuv420p out.yuv

ffmpeg解码码流命令

ffmpeg -i 720P.264 -s 1280x720 -pix_fmt yuv422p 720P-out.yuv

简化版:ffmpeg -i 720P.264 720P-out.yuv

  1. ffmpeg yuv转avi
ffmpeg -s wxh -pix_fmt yuv420p -i input.yuv -vcodec mpeg4 output.avi  
ffmpeg -s wxh -pix_fmt yuv420p -i input.yuv -vcodec h264 output.avi
  1. ffmpeg 裸码流转avi
ffmpeg -f h264 -i source.264 -c:v libx264 -an dest.avi

Use -c:v copy instead to copy the source stream without re-encoding 参考网址:stackoverflow.com/questions/3…

  1. ffmpeg avi转裸码流
  • 23.1 提取裸码流
ffmpeg -i BQSquare_416x240_37.avi -f rawvideo -vcodec copy xx.264
  • 23.2 提取若干帧数码流
ffmpeg -i BQSquare_416x240_37.avi -f rawvideo -vcodec copy -vframes 100 xx.264

对于提取特定的帧,需要用特殊的方法:假设提取260帧,帧率是26, 则可用-ss 10.0 其中10.0=260/26

  1. YUV裁剪功能 Extract some YUV frames from large yuv File 从第0帧开始截取30帧:
ffmpeg -s widthxheight -i input.yuv -c:v rawvideo -filter:v select="gt(n\, -1)" -vframes 30 out30.yuv

或者:

ffmpeg -s widthxheight -i input.yuv -c:v rawvideo -filter:v select="between(n\, 0\, 29)" out30.yuv

或者:

ffmpeg -r 1 -ss 0 -i input.yuv -vcodec copy -vframes 30 output.yuv

中间截取帧(截取从第30帧到第100帧):

ffmpeg -s widthxheight -i input.yuv -c:v rawvideo -filter:v select="between(n\, 30\, 100)" out.yuv

根据时间截取帧(截取从第10秒到第20秒 ):

ffmpeg -s widthxheight -i input.yuv -c:v rawvideo -filter:v select="between(t\, 10\, 20)" out.yuv

参考自: blog.csdn.net/listener51/…

FFMPEG常用参数说明:

  • 主要参数: -i 设定输入流 -f 设定输出格式 -ss 开始时间 -formats 查看所有支持的容器格式 -codecs 查看所有编解码器 -filters 查看所有可用的filter -pix_fmts 查看所有支持的图片格式 -sample_fmts 查看所有支持的像素格式 -i input_file 查看媒体文件input_file的信息(注意后面不再接其它参数,例如: ffmpeg -i test.mp4,查看 test.mp4视频信息)

  • 视频参数: -b 设定视频流量,默认为200Kbit/s -r 设定帧速率,默认为25 -s 设定画面的宽与高 -aspect 设定画面的比例 -vn 不处理视频 -vcodec 设定视频编解码器,未设定时则使用与输入流相同的编解码器

  • 音频参数: -ar 设定采样率 -ac 设定声音的Channel数 -acodec 设定声音编解码器,未设定时则使用与输入流相同的编解码器 -an 不处理音频


  1. 裁剪:568(画面宽),24(画面高) 880(左起点x坐标) 1055(左起点y坐标)

    ffmpeg -i "fanren test.mp4" -strict -2 -vf crop=880:1055:568:24 "fanren test-1.mp4"
    
    ffmpeg -ss 00:00:34 -t 00:00:14 -i "fanren test.mp4" -s 360*640 -r 20 "fanren test-1.gif"
    
  2. 旋转:

    ffmpeg -i “fanren test.mp4” -metadata:s:v rotate="-90" -codec copy “fanren test-1.mp4”
    
  3. 去水印:

    ffmpeg -i "fanren test-1.mp4" -vf "delogo=x=926:y=1480:w=107:h=385" -c:a copy "fanren test-2.mp4"
    
  4. 多视频合并:

ffmpeg -f concat -safe 0 -i allvediolist.txt -c copy output.mp4
# allvediolist.txt:
# file '/home/test/Videos/40_0060.mp4'
# file '/home/test/Videos/40_0061.mp4'
# file '/home/test/Videos/40_0062.mp4'
# file '/home/test/Videos/40_0063.mp4'
  1. 转换格式
ffmpeg -i test.ts -acodec copy -vcodec copy -f mp4 test.mp4
  1. 为视频添加字幕:
ffmpeg -i input.mkv -vf subtitles=subtitles.srt output.mkv

input.mkv 中的字幕(默认)嵌入到 output.mp4 文件

ffmpeg -i input.mkv -vf subtitles=input.mkv output.mp4
  • 30.1 添加双语字幕

    需要准备视频文件和字幕文件:

    • test.mp4为视频文件
    • test_zh.srt为该视频的中文srt字幕文件
    • test_en.srt为该视频的英文srt字幕文件

    现在打算将中文字幕设为第一字幕,英文设为第二字幕:

    1. 添加中文字幕:

      ffmpeg -i test.mp4 -strict -2 -vf subtitles=test_zh.srt:force_style='Fontsize=20\,Fontname=FZYBKSJW--GB1-0\,MarginV=30\,Bold=-1\,BorderStyle=1' -qscale:v 3 test_zh.m
      p4
      # MarginV,设置字幕到视频底部的垂直方位,由于中文为第一字幕,需要添加到视频的稍上位置,这里将MarginV参数值设为30
      
    2. 添加英文字幕:

      英文字幕,作为第二字幕,必在第一字幕靠下方位置,可以直接使用ffmpeg默认字幕安放配置,无需添加MarginV参数设置

      ffmpeg -i test_zh.mp4 -strict -2 -vf  subtitles=test_en.srt:force_style
      ='Fontsize=15\,Fontname=FZYBKSJW--GB1-0\,Bold=-1\,BorderStyle=1' -  qscale:v 3 test_en_zh.mp4
      

命令行参数说明:

  • 01.Name 风格(Style)的名称. 区分大小写. 不能包含逗号.
  • 02.Fontname 使用的字体名称, 区分大小写.
  • 03.Fontsize 字体的字号
  • 04.PrimaryColour 设置主要颜色, 为蓝-绿-红三色的十六进制代码相排列, BBGGRR. 为字幕填充颜色
  • 05.SecondaryColour 设置次要颜色, 为蓝-绿-红三色的十六进制代码相排列, BBGGRR. 在卡拉OK效果中由次要颜色变为主要颜色.
  • 06.OutlineColour 设置轮廓颜色, 为蓝-绿-红三色的十六进制代码相排列, BBGGRR.
  • 07.BackColour 设置阴影颜色, 为蓝-绿-红三色的十六进制代码相排列, BBGGRR. ASS的这些字段还包含了alpha通道信息. (AABBGGRR), 注ASS的颜色代码要在前面加上&H
  • 08.Bold -1为粗体, 0为常规
  • 09.Italic -1为斜体, 0为常规
  • 10.Underline [-1 或者 0] 下划线
  • 11.Strikeout [-1 或者 0] 中划线/删除线
  • 12.ScaleX 修改文字的宽度. 为百分数
  • 13.ScaleY 修改文字的高度. 为百分数
  • 14.Spacing 文字间的额外间隙. 为像素数
  • 15.Angle 按Z轴进行旋转的度数, 原点由alignment进行了定义. 可以为小数
  • 16.BorderStyle 1=边框+阴影, 3=纯色背景. 当值为3时, 文字下方为轮廓颜色的背景, 最下方为阴影颜色背景.
  • 17.Outline 当BorderStyle为1时, 该值定义文字轮廓宽度, 为像素数, 常见有0, 1, 2, 3, 4.
  • 18.Shadow 当BorderStyle为1时, 该值定义阴影的深度, 为像素数, 常见有0, 1, 2, 3, 4.
  • 19.Alignment 定义字幕的位置. 字幕在下方时, 1=左对齐, 2=居中, 3=右对齐. 1, 2, 3加上4后字幕出现在屏幕上方. 1, 2, 3加上8后字幕出现在屏幕中间. 例: 11=屏幕中间右对齐. Alignment对于ASS字幕而言, 字幕的位置与小键盘数字对应的位置相同.
  • 20.MarginL 字幕可出现区域与左边缘的距离, 为像素数
  • 21.MarginR 字幕可出现区域与右边缘的距离, 为像素数
  • 22.MarginV 垂直距离