ffmpeg命令学习

1、组成

程序:ffmpeg、ffplay、ffprobe、ffserver
ffmpeg:转码程序
ffplay:播放程序
ffserver:服务器程序

库:libavcodec、libavdevice、libavfilter、libavformat、libavresample、libavutil、libpostproc、libswresample、libswscale
libavcodec:编解码器库,提供所有支持的编解码器
libavformat:格式库,提供所有支持的文件、流格式
libavfilter:过滤器
libswscale:图像处理库
libavdevice:输入设备库。包括视频、音频的捕获设备接口。例如:windows下的vfw、directshow;linux下video4linux

2、使用

ffmpeg格式
ffmpeg [[options][`-i' input_file]]... {[options] output_file}...
如果没有输入文件,那么视音频捕捉(只在Linux下有效,因为Linux下把音视频设备当作文件句柄来处理)就会起作用。作为通用的规则,选项一般用于下一个特定的文件。如果你给 –b 64选项,改选会设置下一个视频速率。对于原始输入文件,格式选项可能是需要的。缺省情况下,ffmpeg试图尽可能的无损转换,采用与输入同样的音频视频参数来输出。

流复制:复制文件的流,只更改容器(文件)格式。
./ffmpeg –v debug –i inputfile.mpg –c copy -f asf outfile.asf
-f:设置混合器
-c:设置编码器。当为 copy:指复制编码流
-i:输入文件
-v:调试信息级别(quiet、panic、fatal、error、warning、info、verbose、debug)

指定流:
一个容器里可以包含许多流,包括任何的音视频流。那么在命令行怎样区分流呢?
答:由“:”进行分隔
./ffmpeg –i inputfile.mpg –c:v h263 –c:a g722 –f asf outfile.asf
./ffmpeg –i inputfile.mpg –c:0 h263 –c:1 g722 –f asf outfile.asf
上面命令表示把输入文件 inputfile文件的视频流转换成h263格式,把音频流转换成g722格式。
v:视频
a:音频
数字:表示第几路,从0开始

3、通用选项

-L license
-h 帮助
-fromats显示可用的格式,编解码的,协议的
-codecs 显示可用的编解码器
-bsfs 显示bit流过滤器
-protocols显示可用的协议
-pix_fmts显示可用的位图格式
-sample_fmts显示可用的音频采样格式
-loglevel loglevel set libav* logging level
-v loglevel set libav* logging level
-debug flags set debug flags
-fdebug flags set debug flags
-report generate a report

-f fmt强迫采用格式fmt
-i filename输入文件
-y 覆盖输出文件
-c编解码器名字
-t duration 设置纪录时间 hh:mm:ss[.xxx]格式的记录时间也支持
-ss position 搜索到指定的时间 [-]hh:mm:ss[.xxx]的格式也支持
-timestamp time 设置记录时间戳(‘now’指定当前时间)
-title string 设置标题
-author string 设置作者
-copyright string 设置版权
-comment string 设置评论

4、视频选项

-vframes number 设置记录的视频帧数
-r rate 设置帧率 (Hz value, fraction or abbreviation)
-s size 设置帧的大小 (WxH or abbreviation)
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number set the number of bits per raw sample
-croptop size Removed, use the crop filter instead
-cropbottom size Removed, use the crop filter instead
-cropleft size Removed, use the crop filter instead
-cropright size Removed, use the crop filter instead
-padtop size Removed, use the pad filter instead
-padbottom size Removed, use the pad filter instead
-padleft size Removed, use the pad filter instead
-padright size Removed, use the pad filter instead
-padcolor color Removed, use the pad filter instead
-vn 禁用视频
-vcodec codec force video codec ('copy' to copy stream)
-sameq use same quantizer as source (implies VBR)
-same_quant use same quantizer as source (implies VBR)
-timecode hh:mm:ss[:;.]ff set initial TimeCode value.
-pass n select the pass number (1 or 2)
-passlogfile prefix select two pass log file name prefix
-vf filter list video filters
-b bitrate video bitrate (please use -b:v)
-dn disable data

5、高级视频选项

-pix_fmt format 设置位图格式
-intra deprecated use -g 1
-vdt n discard threshold
-rc_override override rate control override for specific intervals
-deinterlace this option is deprecated, use the yadif filter instead
-psnr calculate PSNR of compressed frames
-vstats dump video coding statistics to file
-vstats_file file dump video coding statistics to file
-intra_matrix matrix specify intra matrix coeffs
-inter_matrix matrix specify inter matrix coeffs
-top top=1/bottom=0/auto=-1 field first
-dc precision intra_dc_precision
-vtag fourcc/tag force video tag/fourcc
-qphist show QP histogram
-force_fps force the selected framerate, disable the best supported framerate selection
-force_key_frames timestamps force key frames at specified timestamps
-vbsf video bitstream_filters deprecated
-vpre preset set the video options to the indicated preset

6、音频选项

-aframes number set the number of audio frames to record
-aq quality set audio quality (codec-specific)
-ar rate set audio sampling rate (in Hz)
-ac channels set number of audio channels
-an disable audio
-acodec codec force audio codec ('copy' to copy stream)
-vol volume change audio volume (256=normal)
-af filter list audio filters

7、音频/视频捕获选项

-vc channel deprecated, use -channel
-tvstd standard deprecated, use -standard
-isync sync read on input
-vd device 设置视频捕获设备。比如/dev/video0
-dv1394 设置DV1394捕获
-av device 设置音频设备比如/dev/dsp

8、高级选项

-cpuflags flags force specific cpu flags
-map [-]input_file_id[:stream_specifier][,sync_file_id[:stream_s set input stream mapping
-map_channel file.stream.channel[:syncfile.syncstream] map an audio channel from one stream to another
-map_chapters input_file_index set chapters mapping
-benchmark add timings for benchmarking
-benchmark_all add timings for each task
-progress url write program-readable progress information
-dump dump 每个输入包
-hex 当 dumping包时,也dump其荷载
-re read input at native frame rate
-vsync video sync method
-async audio sync method
-adrift_threshold threshold audio drift threshold
-copyts copy timestamps
-copytb mode copy input stream time base when stream copying
-shortest finish encoding within shortest input
-dts_delta_threshold threshold timestamp discontinuity delta threshold
-dts_error_threshold threshold timestamp error delta threshold
-copyinkf copy initial non-keyframes
-q q use fixed quality scale (VBR)
-qscale q use fixed quality scale (VBR)
-profile profile set profile
-filter_complex graph_description create a complex filtergraph
-debug_ts 打印调试信息时间戳
-streamid streamIndex:value set the value of an outfile streamid
-muxdelay seconds set the maximum demux-decode delay
-muxpreload seconds set the initial demux-decode delay
-fpre filename set options from indicated preset file

9、例程

1.1.1 截取一张352x240尺寸大小的,格式为jpg的图片:
ffmpeg -i test.asf -y -f image2 -t 0.001 -s 352x240 a.jpg

1.1.2 把视频的前30帧转换成一个Animated Gif
ffmpeg -i test.asf -vframes 30 -y -f gif a.gif

1.1.3 截取指定时间的缩微图
ffmpeg -i test.avi -y -f image2 -ss 8 -t 0.001 -s 350x240 test.jpg
-ss后跟的时间单位为秒

1.1.4 转换文件为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

1.1.5 使用ffmpeg录像屏幕
ffmpeg -vcodec mpeg4 -b 1000 -r 10 -g 300 -vd x11:0,0 -s 1024x768 ~/test.avi
:其中,-vd x11:0,0指录制所使用的偏移为 x=0和 y=0,-s 1024×768指录制视频的大小为 1024×768。录制的视频文件为 test.avi,将保存到用户主目录中
如果你只想录制一个应用程序窗口或者桌面上的一个固定区域,那么可以指定偏移位置和区域大小。使用xwininfo -frame命令可以完成查找上述参数。
重新调整视频尺寸大小
ffmpeg -vcodec mpeg4 -b 1000 -r 10 -g 300 -i ~/test.avi -s 800×600 ~/test-800-600.avi
注:上面的ffmpeg的屏幕录制功能只能在Linux环境下有效。在windows下是否可行,本人还不知道如何操作,如果你知道,请告诉我J

1.1.6 把摄像头的实时视频录制下来,存储为文件
在windows有两种视频捕获方式,使用VFW或DSHOW,你需要在编译ffmpeg时,把相应的组件编译进libavdevice中,下面使用vfw方式捕获:
./ffmpeg -t 10 -f vfwcap -i 0 -r 25 -f asf cap.asf
我们采集10秒,采集设备为vfwcap类型设备,第0个vfwcap采集设备(如果系统有多个vfw的视频采集设备,可以通过-i num来选择),每秒25帧,输出方式为文件,格式为asf
在Linux平台上,ffmpeg对V4L2的视频设备提高了很好的支持,如:
./ffmpeg -t 10 -f video4linux2 -s 176*144 -r 8 -i /dev/video0 -vcodec h263 -f rtp rtp://192.168.1.105:5060 > /tmp/ffmpeg.sdp
以上命令表示:采集10秒钟视频,对video4linux2视频设备进行采集,采集QCIF(176*144)的视频,每秒8帧,视频设备为/dev/video0,视频编码为h263,输出格式为RTP,后面定义了IP地址及端口,将该码流所对应的SDP文件重定向到/tmp/ffmpeg.sdp中,将此SDP文件上传到流媒体服务器就可以实现直播了。
./ffmpeg -t 10 -f video4linux2 -s 176*144 -r 10 -vpre libx264-hq.ffpreset -i /dev/video0 -vcodec libx264 -f rtp rtp://192.168.1.105:6060 > /tmp/x264.sdp
这条命令与上面的类似,但是视频编码为h264,由于ffmpeg是用外部库x264支持h264编码,因此h264的视频采集需要更多参数。主要是需要指定-vpre libx264-hq.ffpreset 才可以。

下面命令则是把捕获到的视频保存成文件:
ffmpeg -f video4linux -s 320*240 -r 10 -i /dev/video0 test.asf

1.1.1 接收组播直播流,并保存成文件:
./ffmpeg -i udp://@ip:port test.ts

1.1.2 从文件生成直播流:
ffmpeg -i a.flv -f flv rtmp://IP/livepkgr/livestream?adbe-live-event=liveevent
IP是你的fms服务器的IP地址

1.1.3 从组播地址接收组播,并把它转成rtmp协议发给 fms服务器
ffmpeg –i udp://@:port –f flv rtmp://IP/livepkgr/livestream?adbe-live-event=liveevent

1.1.4 对流进行分片(支持hls协议)
ffmpeg -I a.asf -codec copy -map 0 -f segment-segment_list out.m3u8 out%03d.tss

 

posted @ 2014-07-04 09:35  笨笨D幸福  阅读(1691)  评论(0编辑  收藏  举报
CSDN博客:http://blog.csdn.net/bbdxf CNBlogs: http://www.cnblogs.com/bbdxf