【Linux】关于ffmpeg的一些常见用法

 一、FFmpeg简介

FFmpeg是一款非常快速的视频和音频转换器, 是开源项目 FFmpeg (Fast Forward moving pictures expert group) 的命令行程序。 它可以在任意采样率之间转换,并通过高质量的多相滤波器实时调整视频大小。

FFmpeg程序的转码流程,如下所示

_______              ______________
|       |            |              |
| input |  demuxer   | encoded data |   decoder
| file  | ---------> | packets      | -----+
|_______|            |______________|      |
                                           v
                                       _________
                                      |         |
                                      | decoded |
                                      | frames  |
                                      |_________|
 ________             ______________       |
|        |           |              |      |
| output | <-------- | encoded data | <----+
| file   |   muxer   | packets      |   encoder
|________|           |______________|


其中, demuxer 为解复用器, muxer 为复用器; decoder 为解码器, encoder 为编码器

二、关于FFmpeg的参数

1、通用选项

-L license
-h 帮助
-fromats 显示可用的格式,编解码的,协议的。。。
-f fmt 强迫采用格式fmt
-I filename 输入文件
-y 覆盖输出文件
-t duration 设置纪录时间 hh:mm:ss[.xxx]格式的记录时间也支持
-ss position 搜索到指定的时间 [-]hh:mm:ss[.xxx]的格式也支持
-title string 设置标题
-author string 设置作者
-copyright string 设置版权
-comment string 设置评论
-target type 设置目标文件类型(vcd,svcd,dvd) 所有的格式选项(比特率,编解码以及缓冲区大小)自动设置 ,只需要输入如下的就可以了:
ffmpeg -i myfile.avi -target vcd /tmp/vcd.mpg
-hq 激活高质量设置
-itsoffset offset 设置以秒为基准的时间偏移,该选项影响所有后面的输入文件。该偏移被加到输入文件的时戳,
    定义一个正偏移意味着相应的流被延迟了 offset秒。 [-]hh:mm:ss[.xxx]的格式也支持

2、视频选项

-b bitrate 设置比特率,缺省200kb/s
-r fps 设置帧频 缺省25
-s size 设置帧大小 格式为WXH 缺省160X128.下面的简写也可以直接使用:
Sqcif 128X96 qcif 176X144 cif 252X288 4cif 704X576
-aspect aspect 设置横纵比 4:3 16:9 或 1.3333 1.7777
-croptop size 设置顶部切除带大小 像素单位
-cropbottom size –cropleft size –cropright size
-padtop size 设置顶部补齐的大小 像素单位
-padbottom size –padleft size –padright size –padcolor color 设置补齐条颜色(hex,6个16进制的数,红:绿:兰排列,比如 000000代表黑色)
-vn 不做视频记录
-bt tolerance 设置视频码率容忍度kbit/s
-maxrate bitrate设置最大视频码率容忍度
-minrate bitreate 设置最小视频码率容忍度
-bufsize size 设置码率控制缓冲区大小
-vcodec codec 强制使用codec编解码方式。 如果用copy表示原始编解码数据必须被拷贝。
-sameq 使用同样视频质量作为源(VBR)
-pass n 选择处理遍数(1或者2)。两遍编码非常有用。第一遍生成统计信息,第二遍生成精确的请求的码率
-passlogfile file 选择两遍的纪录文件名为file

3、高级视频选项

-b bitrate 设置比特率,缺省200kb/s
-r fps 设置帧频 缺省25
-s size 设置帧大小 格式为WXH 缺省160X128.下面的简写也可以直接使用:
Sqcif 128X96 qcif 176X144 cif 252X288 4cif 704X576
-aspect aspect 设置横纵比 4:3 16:9 或 1.3333 1.7777
-croptop size 设置顶部切除带大小 像素单位
-cropbottom size –cropleft size –cropright size
-padtop size 设置顶部补齐的大小 像素单位
-padbottom size –padleft size –padright size –padcolor color 设置补齐条颜色(hex,6个16进制的数,红:绿:兰排列,比如 000000代表黑色)
-vn 不做视频记录
-bt tolerance 设置视频码率容忍度kbit/s
-maxrate bitrate设置最大视频码率容忍度
-minrate bitreate 设置最小视频码率容忍度
-bufsize size 设置码率控制缓冲区大小
-vcodec codec 强制使用codec编解码方式。 如果用copy表示原始编解码数据必须被拷贝。
-sameq 使用同样视频质量作为源(VBR)
-pass n 选择处理遍数(1或者2)。两遍编码非常有用。第一遍生成统计信息,第二遍生成精确的请求的码率
-passlogfile file 选择两遍的纪录文件名为file

4、音频选项

-ab bitrate 设置音频码率
-ar freq 设置音频采样率
-ac channels 设置通道 缺省为1
-an 不使能音频纪录
-acodec codec 使用codec编解码

5、音频/视频捕获选项

-vd device 设置视频捕获设备。比如/dev/video0
-vc channel 设置视频捕获通道 DV1394专用
-tvstd standard 设置电视标准 NTSC PAL(SECAM)
-dv1394 设置DV1394捕获
-av device 设置音频设备 比如/dev/dsp

6、高级选项

-map file:stream 设置输入流映射
-debug 打印特定调试信息
-benchmark 为基准测试加入时间
-hex 倾倒每一个输入包
-bitexact 仅使用位精确算法 用于编解码测试
-ps size 设置包大小,以bits为单位
-re 以本地帧频读数据,主要用于模拟捕获设备
-loop 循环输入流。只工作于图像流,用于ffserver测试

三、实战操作

ffmpeg -y -i "1.avi" -title "Test" -vcodec xvid -s 368x208 -r 29.97 -b 1500  \
-acodec aac -ac 2 -ar 24000 -ab 128 -vol 200 -f psp -muxvb 768 "output.wmv" 
  • -y 覆盖输出文件,即如果 output.wmv 文件已经存在的话,不经提示就覆盖掉

  • -i "1.avi" 输入文件是和ffmpeg在同一目录下的1.avi文件,可以自己加路径,改名字

  • -title "Test" 在PSP中显示的影片的标题

  • -vcodec xvid 使用XVID编码压缩视频,不能改的

  • -s 368x208 输出的分辨率为368x208,注意片源一定要是16:9的不然会变形

  • -r 29.97 帧数,一般就用这个吧

  • -b 1500 视频数据流量,用-b xxxx的指令则使用固定码率,数字随便改,1500以上没效果;还可以用动态码率如:-qscale 4和-qscale 6,4的质量比6高

  • -acodec aac 音频编码用AAC

  • -ac 2 声道数1或2

  • -ar 24000 声音的采样频率,好像PSP只能支持24000Hz

  • -ab 128 音频数据流量,一般选择32、64、96、128

  • -vol 200 200%的音量,自己改

  • -muxvb 768 好像是给PSP机器识别的码率,一般选择384、512和768,我改成1500,PSP就说文件损坏了

  • -f psp 输出psp专用格式

  • "output.wmv" 输出文件名,也可以加路径改文件名

2、视频裁剪

将test.mp4视频的前3秒,重新生成一个新视频

  • -ss 开始时间,如: 00:00:00,表示从0秒开始,格式也可以00:00:0

  • -t 时长,如: 00:00:03,表示截取3秒长的视频,格式也可以00:00:3

  • -y 如果文件已存在强制替换;

  • -i 输入,后面是空格,紧跟着就是输入视频文件;

  • -vcodec copy 和 -acodec copy表示所要使用的视频和音频的编码格式,这里指定为copy表示原样拷贝;

ffmpeg -i test.mp4 -y -f mjpeg -ss 3 -t 1  test1.jpg    
ffmpeg -i test.mp4 -y -f image2 -ss 3 -vframes 1 test1.jpg    

在第三秒的时候,截图

# ffmpeg -i test.mp4 -ab 56 -ar 22050 -qmin 2 -qmax 16 -b 320k -r 15 -s 320x240 outputfile.flv    #mp4 转 flv  
      
# ffmpeg -i outputfile.flv -copyts -strict -2 test.mp4       #flv 转 mp4  
ffmpeg -y -i test.mp4 -acodec copy -vf "movie=uwsgi.jpg [logo]; [in][logo] overlay=10:10:1 [out]" test2.mp4   

overlay=10:10:1,后三个数据表示是距离左边的距离,距离上边的距离,是否透明,1表示透明。上例我用的是jpg,当然不可能透明。

ffmpeg -y -i test.mp4 -acodec copy -vf "movie=uwsgi.jpg [logo]; [in][logo] overlay=enable='lte(t,1)' [out]" test2.mp4     
ffmpeg -i test.asf -vframes 30 -y -f gif a.gif    #把视频的前30帧转换成一个Gif  
ffmpeg -i test2.mp4 -y -f image2 -ss 08 -t 0.001 -s 352x240 b.jpg     #在秒处接取一个352X240的图片  
ffmpeg -i "rtmp://192.168.10.103:1935/live/111 live=1" -acodec copy -vcodec copy -f flv -y test.flv

 将rtmp流,以文件的形势保存到本地

ffmpeg -i test.mp3  -i test1.mp3 -filter_complex amix=inputs=2 -f mp3 c.mp3    #合并二个音频
ffmpeg -i test.mp3 -i test1.mp3 -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f mp3 a.mp3

合并二个音频,以第一个音频的时长为新音频时长

a.txt 格式如下

file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'

视频合成

ffmpeg -f concat -i a.txt -c copy a.flv 
ffmpeg -i "concat:1.flv|2.flv|3.flv" -c copy 4.flv  
ffmpeg -f concat -i <(for i in {1..4}; do printf "file '%s'\n" input.mp4; done) -c copy output.mp4

Also see:


下面是一些其他选项

loop filter

Example using the loop filter to loop 4 times, each loop is 75 frames, each loop skips the first 25 frames of the input:

ffmpeg -i input -filter_complex loop=loop=3:size=75:start=25 output
  • Or use the shorthand: loop=3:75:25
  • Filtering requires re-encoding.
  • This filter places all frames into memory.
  • Using loop=3 will loop 4 times.
  • To loop infinitely use -1.
  • You must list the number of frames to loop (shown as 75 in the example above). Max value is 32767.
  • Also see ffmpeg -h filter=loop.

-loop option

The -loop option is specific to the image file demuxer and gif muxer, so it can't be used for typical video files, but it can be used to infinitely loop a series of input images. This example will loop over and over but the -t 30 will limit the output duration to 30 seconds:

ffmpeg -loop 1 -i %03d.png -t 30 output.mkv

Or to loop a GIF output:

ffmpeg -i input -loop 3 output.

10、视频推流

nohup ffmpeg -stream_loop -1 -re   -i  7189189.mp4  -vcodec copy -acodec copy  \
strict -1 -f flv rtmp://push.test.com/db/gw2 > b.txt 2>&1  &

  

参考文章:

https://video.stackexchange.com/questions/12905/repeat-loop-input-video-with-ffmpeg

https://stackoverflow.com/questions/30846719/infinite-stream-from-a-video-file-in-a-loop

https://www.cnblogs.com/jhj117/p/9130073.html

http://www.mikewootc.com/wiki/sw_develop/multimedia/ffmpeg_app_param.html 

 

猜你喜欢

转载自www.cnblogs.com/chenpingzhao/p/10758820.html