ffmpeg参数优化

kevin.Zhu 发布于:2017-10-11 1:36 分类:视频直播  有 15 人浏览,获得评论 0 条  

http://blog.sina.com.cn/s/blog_80ce3a550102w1i3.html


http://www.cnblogs.com/wainiwann/p/4128154.html


http://blog.csdn.net/defonds/article/details/9274479/   #ffmpeg参数详解

http://blog.csdn.net/wutong_login/article/details/42292787   #使用Nginx+FFMPEG搭建HLS直播转码服务器



基本选项:
http://keren.iteye.com/blog/1773536
-formats 输出所有可用格式
-f fmt 指定格式(音频或视频格式)
-i filename 指定输入文件名,在linux下当然也能指定:0.0(屏幕录制)或摄像头
-y 覆盖已有文件
-t duration 记录时长为t
-fs limit_size 设置文件大小上限
-ss time_off 从指定的时间(s)开始, [-]hh:mm:ss[.xxx]的格式也支持
-itsoffset time_off 设置时间偏移(s),该选项影响所有后面的输入文件。该偏移被加到输入文件的时戳,定义一个正偏移意味着相应的流被延迟了 offset秒。 [-]hh:mm:ss[.xxx]的格式也支持
-title string 标题
-timestamp time 时间戳
-author string 作者
-copyright string 版权信息
-comment string 评论
-album string album名
-v verbose 与log相关的
-target type 设置目标文件类型("vcd", "svcd", "dvd", "dv", "dv50", "pal-vcd", "ntsc-svcd", ...)
-dframes number 设置要记录的帧数
视频选项:
-b 指定比特率(bits/s),似乎ffmpeg是自动VBR的,指定了就大概是平均比特率,指定码率注意单位是bit/s,所以我们一般要加k,比如 -b 1000k 就是1000kb/s
-bitexact 使用标准比特率,也支持M,比如4M
-vb 指定视频比特率(bits/s)
-vframes number 设置转换多少桢(frame)的视频
-r rate 帧速率(fps) (可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97)
-s size 指定分辨率 (320x240)
-aspect aspect 设置视频长宽比(4:3, 16:9 or 1.3333, 1.7777)
-croptop size 设置顶部切除尺寸(in pixels)
-cropbottom size 设置底部切除尺寸(in pixels)
-cropleft size 设置左切除尺寸 (in pixels)
-cropright size 设置右切除尺寸 (in pixels)
-padtop size 设置顶部补齐尺寸(in pixels)
-paom size 底补齐(in pixels)
-padleft size 左补齐(in pixels)
-padright size 右补齐(in pixels)
-padcolor color 补齐带颜色(000000-FFFFFF)
-vn 取消视频
-vcodec codec 强制使用codec编解码方式('copy' to copy stream)
-sameq 使用同样视频质量作为源(VBR)
-pass n 选择处理遍数(1或者2)。两遍编码非常有用。第一遍生成统计信息,第二遍生成精确的请求的码率
-passlogfile file 选择两遍的纪录文件名为file
-newvideo 在现在的视频流后面加入新的视频流
 
  • -maxrate 和-minrate 指定允许的最大和最小码率,一般如果要用cbr模式编码的话会用这个:
    ffmpeg -i myfile.avi -b 4000k -minrate 4000k -maxrate 4000k -bufsize 1835k out.m2v
    否则用处不大: http://stackoverflow.com/questions/7125446/encoding-h-264-cbr-videos-with-ffmpeg    Specify -nal-hrd cbr after -bufsize 400000.  CBR is when maxrate == bitrate and bufsize is set. 
  1. -nal-hrd           <<span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">int       E..V.. Signal HRD information (requires vbv-bufsize; cbr not allowed in .mp4) (from -1 to INT_MAX)  
  2.    none                         E..V..  
  3.    vbr                          E..V..  
  4.    cbr                          E..V..  
高级视频选项
-pix_fmt format set pixel format, 'list' as argument shows all the pixel formats supported
-intra 仅适用帧内编码
-qscale q 以<数值>质量为基础的VBR,取值0.01-255,约小质量越好
-loop_input 设置输入流的循环数(目前只对图像有效)
-loop_output 设置输出视频的循环数,比如输出gif时设为0表示无限循环
-g int 设置图像组大小
-cutoff int 设置截止频率
-qmin int 设定最小质量,与-qmax(设定最大质量)共用,比如-qmin 10 -qmax 31
-qmax int 设定最大质量
-qdiff int 量化标度间最大偏差 (VBR)
-bf int 使用frames B 帧,支持mpeg1,mpeg2,mpeg4

音频选项:
-ab 设置比特率(单位:bit/s,也许老版是kb/s)前面-ac设为立体声时要以一半比特率来设置,比如192kbps的就设成96,转换 默认比特率都较小,要听到较高品质声音的话建议设到160kbps(80)以上。
-aframes number 设置转换多少桢(frame)的音频
-aq quality 设置音频质量 (指定编码)
-ar rate 设置音频采样率 (单位:Hz),PSP只认24000
-ac channels 设置声道数,1就是单声道,2就是立体声,转换单声道的TVrip可以用1(节省一半容量),高品质的DVDrip就可以用2
-an 取消音频
-acodec codec 指定音频编码('copy' to copy stream)
-vol volume 设置录制音量大小(默认为256) <百分比> ,某些DVDrip的AC3轨音量极小,转换时可以用这个提高音量,比如200就是原来的2倍
-newaudio 在现在的音频流后面加入新的音频流
字幕选项:
-sn 取消字幕
-scodec codec 设置字幕编码('copy' to copy stream)
-newsubtitle 在当前字幕后新增
-slang code 设置字幕所用的ISO 639编码(3个字母)
Audio/Video 抓取选项:
-vc channel 设置视频捕获通道(只对DV1394)
-tvstd standard 设置电视标准 NTSC PAL(SECAM)


        要得到一个高画质音质低容量的MP4的话,首先画面最好不要用固定比特率,而用VBR参数让程序自己去判断,而音质参数可以在原来的基础上提升一点,听起来要舒服很多,也不会太大(看情况调整 ) 




        转换为flv: 
    ffmpeg -i test.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 test.flv 
    ffmpeg -i test.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 test.flv 

        转换文件格式的同时抓缩微图: 
    ffmpeg -i "test.avi" -y -f image2 -ss 8 -t 0.001 -s 350x240 'test.jpg' 

        对已有flv抓图: 
    ffmpeg -i "test.flv" -y -f image2 -ss 8 -t 0.001 -s 350x240 'test.jpg' 

        转换为3gp:
    ffmpeg -y -i test.mpeg -bitexact -vcodec h263 -b 128 -r 15 -s 176x144 -acodec aac -ac 2 -ar 22500 -ab 24 -f 3gp test.3gp 
    ffmpeg -y -i test.mpeg -ac 1 -acodec amr_nb -ar 8000 -s 176x144 -b 128 -r 15 test.3gp 




        例:ffmpeg -y -i "test.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 "test.***" 
    # 参数解释:
 
    -y(覆盖输出文件,即如果1.***文件已经存在的话,不经提示就覆盖掉了) 
    -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%的音量,自己改) 
    -f psp(输出psp专用格式) 
    -muxvb 768(好像是给PSP机器识别的码率,一般选择384、512和768,我改成1500,PSP就说文件损坏了)
    "test.***"(输出文件名,也可以加路径改文件名)

                                                            CBR 和 VBR 设置
http://ffmpeg-users.933282.n4.nabble.com/Does-constant-bitrate-exist-in-libx264-td2255554.html
http://stackoverflow.com/questions/7125446/encoding-h-264-cbr-videos-with-ffmpeg
https://trac.ffmpeg.org/wiki/Encode/H.264#CBRConstantBitRate
crf参数: 固定质量  http://slhck.info/articles/crf
http://trac.ffmpeg.org/wiki/Limiting the output bitrate
https://mailman.videolan.org/pipermail/x264-devel/2006-February/001447.html
http://forum.doom9.org/archive/index.php/t-151486.html

ffmpeg中CBR(固定码率控制)的设置:
                c->bit_rate = br;
                c->rc_min_rate =br;
                c->rc_max_rate = br;  
                c->bit_rate_tolerance = br;
                c->rc_buffer_size=br;
                c->rc_initial_buffer_occupancy = c->rc_buffer_size*3/4;
                c->rc_buffer_aggressivity= (float)1.0;
                c->rc_initial_cplx= 0.5; 


The params (FPS is 25): 
 params->rc.b_cbr = TRUE; 
 params->rc.i_bitrate = 1500; 
 params->rc.i_vbv_max_bitrate = 1500; 
 params->rc.i_vbv_buffer_size = 1500; 
 params->rc.f_rate_tolerance = 0.05;


ffmpeg中VBR(可变率控制)的设置:
                c->flags |= CODEC_FLAG_QSCALE;
                c->rc_min_rate =min;
                c->rc_max_rate = max; 
                c->bit_rate = br;


Additional Information & Tips

ABR (Average Bit Rate)

ffmpeg -i input -c:v libx264 -b:v 1000k output.mp4

This provides something of a "running average" target, with the end goal that the final file match this number "overall on average" (so basically, if it gets a lot of black frames, which cost very little, it will encode them with less than the requested bitrate, but then the next few seconds of (non-black) frames it will encode at very high quality, to bring the average back in line). Using 2-pass can help this method to be more effective. You can also use this in combination with a "max bit rate" setting in order to prevent some of the swings.

CBR (Constant Bit Rate)

There is no native CBR mode, but you can "simulate" a constant bit rate setting by tuning the parameters of ABR:

ffmpeg -i input -c:v libx264 -b:v 4000k -minrate 4000k -maxrate 4000k -bufsize 1835k out.m2v

In the above example, -bufsize is the "rate control buffer" so it will enforce your requested "average" (4000k in this case) across each 1835k worth of video. So basically it is assumed that the receiver/end player will buffer that much data so it's ok to fluctuate within that much.

Of course, if it's all just empty/black frames then it will still serve less than that many bits/s but it will raise the quality level as much as it can, up to the crf level.




Creating a live stream with limited bit rate

In this case, you want to have a live stream with more/less constant bit rate (using the option -b:v), to be able to control the bandwidth used. We will instruct the libx264 encoder to simulate a stream transmission with a virtual buffer (just like the real buffer at the decoding side). The technique will constrain the bit rate in order not to exceed a certain threshold value which would take more time to transmit and would cause the decoding buffer to underflow waiting for the new data to arrive.

The typical example would be something like this (as shown in the documentation):

ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi

The key option here is -bufsize which tells the encoder how often to calculate the average bit rate and check to see if it conforms to the average bit rate specified on the command line (-b:v 64k). If we don't specify the -bufsize option, ffmpeg will still calculate and correct the average bit rate produced, but more lazy, at some intervals which can be significantly longer than we would want. This would cause the current bit rate to frequently jump a lot over and below the specified average bit rate and would cause an unsteady output bit rate. If we specify a smaller -bufsize, ffmpeg will more frequently check for the output bit rate and constrain it to the specified average bit rate from the command line. Specifying too small -bufsize would cause ffmpeg to degrade the output image quality, because it would have to (frequently) conform to the limitations and would not have enough of a free space to use some optimizations (for example, optimizations based on the frame repetitions and similar), because the buffer would not contain enough frames for the optimizations to be effective.

The suggestion is to play around with the combinations of -bufsize, starting from the same value like the one specified for the -b:v option, and increasing it until your output bit rate starts jumping too much above/below the specified average bit rate. Then you know you've reached the limit and should lower the -bufsize value a little bit in order to get back on the safe side.

There are also -minrate and -maxrate options which control the minimum and maximum bit rate tolerance (in bits/s). With these options you can give ffmpeg a hint of how much of a bit rate tolerance (a difference from the specified average bit rate) you are willing to accept.

Creating the output file of specific size

For this scenario it is by far the best to use a 2-pass encoding, using the -pass option, like it is explained in the example of targeting a specific output file size.


CBR simply means that vbv-maxrate is equal to bitrate and that bufsize is set to the appropriate value for the device.

http://qtoue.com/3Jrwd8m3/encoding-h264-cbr-videos-with-ffmpeg.html
Here is what I have for low-latency CBR video to an MPEG tranport stream:
ffmpeg -i sintel_trailer-720p.mp4 -an -tune zerolatency -x264opts bitrate=4000:vbv-maxrate=4000:vbv-bufsize=166 -vcodec libx264 -f mpegts -muxrate 4000K -y trailer.ts 
According the x264 developer's blog you set:
  • vbv-maxrate = bitrate = B = target bitrate
  • vbv-bufsize = B / fps (in this video's case that's 24 fps)
Finally, set the ffmpeg switch for x264 of -tune zerolatency.
下面是一个在项目中使用的配置:
-s 1920*1080 -pix_fmt yuv420p -i xxx.yuv -vcodec libx264 -r 30 -nal-hrd cbr -bufsize 1835k -b:v 4M -bt 4M  -minrate 4M -maxrate 4M
注意到设置不同的bitrate需要去调整bufsize来满足CBR的要求。实际得到的码率还是有一点波动,但是已经是非常的接近CBR了。

http://blog.yikuyiku.com/?p=2029