Summary of FFmpeg common commands

1. Obtain video information, including network url

ffmpeg -i input.mp4
ffmpeg -i http://xxx.com/videofiles/xxxx.flv

 2. Separate audio and video streams

//视频流按原来的编码格式,禁用音频流。
ffmpeg -i input_file -vcodec copy -an output_file_video

// 音频流按原来的编码格式,禁用视频流。
ffmpeg -i input_file -acodec copy -vn output_file_audio 

If you want to change the part of the code stream that is separated, then -codec must be accompanied by an alias of the encoding format supported by ffmpeg.

besides! The suffix of the specific output package file needs to be consistent with the audio encoding. For example,
the flv package format supports h.264, flashvideo + aac/mp3/ac3, and then it needs to be understood that mp3 is an audio encoding format: mpeg1--audio(layer1 ,2,3)
The mp4 encapsulation format supports almost all encoding formats of audio and video streams.

3. Extract the h264 code stream with spspps

//提取 264码流,只能用ffplay -i 播放
ffmpeg -i input.mp4 -vcodec copy -an -bsf: h264_mp4toannexb -f h264 output.264  

//提取 264码流,只能用ffplay -i 播放
ffmpeg -i input.mp4 -vcodec copy -an -bsf: h264_mp4toannexb -f h264 output.264  


// 注意   -bsf后面一定要带一个空格!
// h264格式是分为: VCL(avc1), NALU(annexb)
// VCL(avc1):MP4 文件,一个文件只有一个头部(sps, pps)
// NALU(annexb):一个nalu附带一个spspps头

4. Convert mp4 to ts 

  //MP4转换ts
ffmpeg -i ..\XXFile\XX.mp4 -c copy -f mpegts ..\XXFile\XX.ts     

 //h264视频转ts视频流
ffmpeg -i ..\XXFile\XX.h264 -vcodec copy -f mpegts output.ts   
 
 //h264视频转mp4 
ffmpeg -i ..\test\test.h264 -vcodec copy -f mp4 output.mp4       

5. Change video bit rate, frame rate, gop, width and height; number of audio channels, sampling rate

ffmpeg -i test.mp4 -b:v 640k output.flv       

ffmpeg -i test.mp4 -acodec copy -vcodec copy -f flv output.flv    
 
ffmpeg431 -i test.mp4    -s 176x144      -vcodec libx264      -r 25 -g 100      -acodec aac      -b 200k     -ac 1   -ab 32   -ar 8000   -f avi  output.avi

-s:指定宽高(widthxheight: 必须也必然是偶数,不是偶数也会变成偶数)
-r :帧率 25,30,50,60
-g:gop 图像组,多少帧有一个关键帧
-b:码率,bps(bits per second)
-ac:声道数
-ar:采样率,每秒采样多少次(44.1KHz=44100,48KHz=48000,80KHz)
-ab:采样位数

6. Faststart conversion of MP4 format

ffmpeg -i http://xxxx/154202543368.m3u8 -c copy -movflags +faststart  output.mp4    //m3u8转mp4

/// The encapsulation format of mp4, the header information is at the end of the file by default, so when parsing MP4, the entire file must be read first to parse normally, which seriously affects the efficiency.
/// -movflags +faststart, put the header information of the mp4 file at the head of the file, so that the efficiency will not be affected, and it can also transfer the network stream to the local MP4 format in normal use 

7. Generate video thumbnails

ffmpeg -i input_ test.mp4 -r 1 -f image2 output_ image-%03d.jpeg
ffmpeg -i ..\test\guide.mp4 -ss 8 -t 4  -s 320*240 ..\test\t1_image.gif

-r:1秒1帧图,-f:输出格式为image2,output_ image-%03d.jpeg格式化输出图片

-ss:从8s开始时间,-t:持续4s(把-t的时间值控制在每帧显示的时间内,就是静态图,如果是大于每帧的显示时间,就是动图图),-s:输出大小320*240

8. Merge thumbnails into video

ffmpeg -f image2 -t 30 -r 1   -i ..\test\output_image-%03d.jpeg ..\test\out_compjpeg.mp4

-r: 1秒1帧图,-t:30张图,共持续30s

9. Filter watermark, static layer overlay

Overlay technology is also called video overlay technology. Overlay video technology is widely used. Common examples include the TV station logo displayed in the upper right corner of the TV screen and the picture-in-picture function. Picture-in-picture means that there is a small playback window in a large video playback window, and different video contents of the two windows are played simultaneously. Two windows are involved in the overlay technology. Usually, the larger window is called the background window, and the smaller window is called the foreground window. Videos or pictures can be played in either the background window or the foreground window. Using the overlay filter in FFmpeg can achieve video overlay effect.

/// -vf,video filter:简单过滤器
/// 注意1:overlay的第三个参数,默认是0,如果1强制颜色空间为rgb,但是容易出问题,最好省略第三个参数
/// 注意2: -vf,codec不能使用copy,需要重新指定编码格式
//右上角
ffmpeg -i guide.mp4 -vf "movie=logo.png[logo]; [in][logo] overlay=W-w:10:0 [out]" output.mp4
//中间
ffmpeg -i guide.mp4 -vf "movie=logo.png[logo]; [in][logo]overlay=W/2-w/2:H/2-h/2[out]" -vcodec libx264 -acodec aac output.mp4
//右下角
ffmpeg -i guide.mp4 -vf "movie=logo.png[logo]; [in][logo]overlay=W-w:H-h[out]" -vcodec libx264 -acodec aac output.mp4   -y

// [logo]是输入logo.png的标签名,[in]是输入guide.mp4的标签名。

语法: overlay[=x:[[:rgb={0, 1}]]
参数x和y是可选的,默认为0。
参数rgb 参数也是可选的,其值为0(YUV)或1(RGB),默认为原输入的颜色格式。
参数说明:
X                 从左上角的水平坐标,默认值为0
y                 从左.上角的垂直坐标,默认值为0
rgb              值为0表示输入颜色空间不改变,默认为0;值为1表示将输入的颜色空间设置为RGB
变量说明:如下变量可用在x和y的表达式中
main_w或W           主输入(背景窗口)宽度
main_h或H            主输入(背景窗口)高度
overlay_ w或w       overlay输入(前景窗口)宽度
overlay_ h或h        overlay输入(前景窗口)高度


///也可以使用另外一种方式  -filter_complex overlay=参数
ffmpeg -i guide.mp4 -i logo.png -filter_complex overlay=W/2-w/2:H/2-h/2 test1.mp4

10. Filter watermark, input text/current time

ffmpeg -i guide.mp4 -vf "drawtext=fontfile=simhei.ttf:text='xsfdsx':x=100:y=10:fontsize=24:fontcolor=yellow:shadowy=2"  drawtext.mp4

//添加文字水印命令   drawtext=key=value:key=value:key=value

● line_h, Ih
the height of each text line  文本行的高度
● main_h, h, H
the input height  输入视频的高度:像素
● main_w, w, W
the input width   输入视频的宽度:像素
● n
the number of input frame, starting from 0  第几帧开始
● rand(min, max)
return a random number included between min and max 随机数
● mod(a, b)
求余,a%b,5%2== 1
● sar
The input sample aspect ratio.    输入采样的宽高比,yuv像素的宽高比.
● t
timestamp expressed in seconds, NAN if the input timestamp is unknown    时间戳,单位:秒
● text_h, th
the height of the rendered text   文本的高度:像素
● text_w, tw
the width of the rendered text    文本的宽度:像素
● fontfile       字体样式
● fontsize     字体大小
● fontcolor    字体颜色,支持#336699

/ 在右上角显示当前时间,为了辨析,参数的冒号用了中文字符显示

ffmpeg guide.mp4 -vf drawtext="fontfile=D\\:/test/font/font.ttf:x=W-tw:y=10:fontcolor=black:fontsize=30:text='%{localtime\:%H\\\:%M\\\:%S}" -y output.mp4

// 在命令行当中,drawtext输入的k:v的v里面如果有“:”需要转义“\:”
// 如果fontfile使用了绝对路径D:\test\font\font.ttf,需要转义成fontfile=D\\:/test/font/font.ttf,路径的斜杠 \ 需要变成反斜杠 /
// 在写代码的时候就必须注意双斜杠\\的转义操作。
// localtime的输出格式,斜杠一个都不能少!!!
// java代码,注意转义
String strCmdLine =    "  ....ext='%{localtimne\\:%H\\\\\\:%M\\\\\\:%S}'...   ";
// cpp,注意转义
char strCmdLine[1024]= {0};
strcpy(strCmdLine, " ....xt='%{localtime\\:%H\\\\\\:%M\\\\\\:%S}'... " );

11. Video rotation

旋转有种做法,第一种是在配置元数据上添加属性,视频原始内容不产生任何的改变,转换效率高,但是解码渲染效率有所降低。


// -90 顺时针
ffmpeg -i ..\test\guide.mp4 -metadata:s:v rotate="-90" -codec copy ..\test\meta_rotate_-90.mp4

// 90 逆时针
ffmpeg -i ..\test\guide.mp4 -metadata:s:v rotate="90" -codec copy ..\test\meta_rotate_90.mp4

 第二种,直接改变输入视频的显示内容,转换过程较慢;转换后的解码渲染效率没有任何变化。

// 0: 逆时针旋转90度并垂直翻转
ffmpeg -i ..\test\guide.mp4 -vf "transpose=0"    ..\test\transpose_0.mp4

// 1: 顺时针旋转90度
ffmpeg -i ..\test\guide.mp4 -vf "transpose=1"    ..\test\transpose_1.mp4

// 2: 逆时针旋转90度
ffmpeg -i ..\test\guide.mp4 -vf "transpose=2"    ..\test\transpose_2.mp4

// 3: 顺时针旋转90度后并垂直翻转
ffmpeg -i ..\test\guide.mp4 -vf "transpose=3"    ..\test\transpose_3.mp4

// 4: 水平翻转视频画面
ffmpeg -i ..\test\guide.mp4 -vf hflip guide_hflip.mp4

// 5: 垂直翻转视频画面
ffmpeg -i ..\test\guide.mp4 -vf vflip guide_vflip.mp4

12.视频提取图片注意事项

ffmpeg -i test.avi -r 1 -f image2 image- %3d.jpeg
// -r 1         每秒一帧,提取视频图像。
ffmpeg -ss 0:1:30 -t 0:0:20 -i input.avi -vcodec copy -acodec copy output.avi
// -ss 开始时间,-t 持续时间
// 从10s 处开始剪切,持续15秒(此方法有时候会遇到视频第一帧黑屏, 就是未播放时候的封面是黑色的,原因是未定位到关键帧1帧)
ffmpeg -i test.mp4 -ss 10 -t 15 -codec copy cut.mp4
// 从10s处开始剪切,持续15秒(此方法为优化后的方法,此剪切视频的方法可以解决第一帧黑屏问题,但是可能切割的时间落点稍稍的有一丢丢的不准确,但是应该可以落在误差范围之内)
ffmpeg -ss 10 -t 15 -i test.mp4 -codec copy cut.mp4
// 注意事项:如果 视频文件很大,2GB, duration: 02:00:00
前: -i xxx.mp4 -ss 01:10:05 -t 00:15:20,大视频会很慢,因为需要解码整个视频,然后定位时间点
后: -ss 01:10:05 -t 00:15:20 -i xxx.mp4,较快,因为是seekto

13.音视频倒放,加速减速播放

// 视频倒放 音频不变
ffmpeg -i ..\test\guide.mp4 -vf reverse ..\test\guide_v_reverse.mp4

// 音频倒放 视频不变
ffmpeg -i ..\test\guide.mp4 -c:v copy -af areverse ..\test\guide_a_reverse.mp4

// 音视频同时倒放    -preset superfast 快速编码,视频质量有所下降
ffmpeg -i ..\test\guide.mp4 -vf reverse -af areverse -preset superfast ..\test\guide_av_reverse.mp4

// 减速播放,pts增加为原来的2倍,声音为原来的0.5倍速
ffmpeg -i ..\test\guide.mp4 -vf setpts=PTS*2 -af atempo=0.5 ..\test\guide_speed0.5.mp4

// 加速播放,pts缩短为原来的1/2,声音为原来的2倍速
ffmpeg -i ..\test\guide.mp4 -vf setpts=PTS*0.5 -af atempo=2 ..\test\guide_speed2.mp4

14.视频拼接

以ts流为媒介,因为ts流是可以concate的,先把mp4封装成ts,然后再用concate指令合并ts流,最后再把ts流拼接为mp4。
// 内容复习,-vbsf h264_mp4toannexb,在I帧前都带上sps和pps

ffmpeg -i 10s.mp4 -vcodec libx264 -acodec aac -vbsf h264_mp4toannexb 10s.ts

ffmpeg -i guide.mp4 -vcodec libx264 -acodec aac -vbsf h264_mp4toannexb guide.ts

ffmpeg -i "concat:..\test\10s.ts|..\test\guide.ts" -acodec copy -vcodec copy -absf aac_adtstoasc ..\test\concat_10s_guide.mp4

将AAC码流转换成一些其他格式时,需要特殊配置码流滤波器(-absf aac_adtstoasc)

将AAC编码器编码后的原始码流(ADTS头 + ES流)封装为MP4或者FLV或者MOV等格式时,需要先将ADTS头转换为MPEG-4 AudioSpecficConfig (将音频相关编解码参数提取出来),并将原始码流中的ADTS头去掉(只剩下ES流)

15.视音频合并 

两个视频的合并(注意参数hstack 代表是横向合并,也就是左右合并,横向合并的形象类比为“川”; 如果是竖向合并,也就是上下合并,用参数vstack,竖向或者纵向合并的形象类比为“三”)

注意:经过此命令处理后的视频output.mp4只会保留第一个视频input1.mp4的音频 

ffmpeg -i input1.mp4 -i input2.mp4 -lavfi hstack output.mp4

ffmpeg -i input1.mp4 -i input2.mp4 -lavfi vstack output.mp4

把音频文件mp3 合并到视频文件mp4中去(就是为原视频加背景音乐)

ffmpeg -i video.mp4 -i bg_music.mp3 -c:v copy -c:a aac  -strict experimental    merge.mp4

15、视频区域裁剪

ffmpeg -i ..\test\guide.mp4 -vf crop=400:400:0:0 -y  ..\test\guide_crop.mp4

指令crop的参数格式为w:h:x:y;w、h为输出视频的宽和高,x、y标记输入视频中的某点,将该点作为基准点,向右下进行裁剪得到输出视频。如果xy不写的话,默认居中剪切。

16、视频缩放 

//  将视频宽高度放大一倍

ffmpeg -i ..\test\guide.mp4 -vf scale=iw*2:ih*2     ..\test\guide_scale2.mp4  

//  将视频宽高缩小一半

ffmpeg -i ..\test\guide.mp4 -vf scale=iw*0.5:ih*0.5     ..\test\guide_scale0.5.mp4  

     ~~to : Lydia                                    

おすすめ

転載: blog.csdn.net/Jason_HD/article/details/130123355