ffmpeg

1.1         源码下载

用GIT从下面地址下载:git://source.ffmpeg.org/ffmpeg.git

例如:在linux下:

git  clone  git://source.ffmpeg.org/ffmpeg.git  ffmpeg

1.2         依赖

如果要编译ffplay,它依赖sdl库。安装SDL库:http://www.libsdl.org/

ffmpeg 0.11,需要make3.82版本。由于现在ffmpeg处理活动期,所以最好是在最新的linux操作系统上编译。这样autoconf、automake、make这些工具可以是最新的,就不用升级。

 

如果是windows下编译,还需要cygwin或者minwin

cygwin下可以会因为回车符产生以下错误:

 

./makes: line 2: $'\r': command not found

解决方法以:

1、配置cygwin系统,让cygwin忽略dos回车符

以下是具体的修改方法!

# ignore return

set -o igncr

export SHELLOPTS

 

 2、用dos2unix命令转换有回车符的文件:
dos2unix 文件

 3、用VI替换文件中的回车符

%s/\n/^M/g

  其中^M这个需要先Ctrl+v再按回车得到

 

1.3         编译、安装

1.3.1    在linux下

./configure --disable-yasm

make

make install

 

默认安装在 /usr/local下。

设置pkg-config环境变量:

PKG_CONFIG_PATH=”$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig”

export PKG_CONFIG_PATH

 

或者用 libdir 指定依赖库的位置:

 ./configure --enable-libx264 --enable-gpl  --libdir=/usr/local/lib

1.3.2    cygwin下编译与linux下编译一样。

如果在windows环境下使用,则需要把C:\cygwin\bin加到环境变量PATH中。因为ffmpeg程序依赖的cygwin的库都存在这里。

发行时,就需要把相关依赖的库放到同一目录中。

用ldd ffmpeg 查看依赖哪些库。

1.3.3    mingw下编译与linux下编译一样。

1.3.4    在cygwin下用mingw来交叉编译:

./configure --cross_prefix=i686-pc-mingw32- --target-os=mingw32 --arch=i686 --prefix=/usr/i686-pc-mingw32/sys-root/mingw --enable-swscale --enable-postproc --enable-gpl

会提示说i686-pc-mingw32-pkg-config没找到,不过这没有关系,可以忽略,如果要安装,可以从下面地址下载glib和pkg-config包,pkg-config依赖glib:

然后把它们复制到:
/usr/i686-pc-mingw32/sys-root/mingw 目录下

 

如果在windows环境下使用,则需要把C:\cygwin\usr\i686-pc-mingw32\sys-root\mingw\bin加到环境变量PATH中。因为mingw程序依赖的cygwin的库都存在这里。

发行时,就需要把相关依赖的库放到同一目录中。

 

1.1         ffmpeg格式

ffmpeg [[options][`-i' input_file]]... {[options] output_file}...

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

1.2         流复制:复制文件的流,只更改容器(文件)格式。

./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)

 

1.3         指定流:

一个容器里可以包含许多流,包括任何的音视频流。那么在命令行怎样区分流呢?

答:由“:”进行分隔

./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开始

 

1.1         Ffmpeg选项详解

你可以使用:ffmpeg –help来获得选项内容

 

1.1.1    通用选项

-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 设置评论

 

1.1.2    视频选项

-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

1.1.3    高级视频选项

-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

1.1.4    音频选项

-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

1.1.5    音频/视频捕获选项

-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

 

1.1.6    高级选项

-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

 

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

 

用in.avi作为输入视频,out.avi作为输出视频,pp.png表示要添加的水印图片,是png格式的,调整水印大小(scale = 50:50)和位置(overlay=10:10)
ffmpeg –i in.avi -i /data/move/long.png -filter_complex '[1:v]scale=50:50[s];[0:v][s]overlay=10:10' -b:v 500000 -c:v h264  -s cif -f mpegts out.avi

使用overlay添加透明水印时,位置参数的位置在 -vfilters 里面的 overlay= 的后面的前两个参数,参数之间是冒号分隔的。第一个参数是横向的间距,第二个是纵向的间距。可以结合与视频和水印相关的四个值来设置,这四个值分别是:mainW表示主视频宽度,mainH表示主视频高度,overlayW表示水印宽度,overlayH表示水印高度。这些值加在overlay参数中,ffmpeg将自动识别。

 

也就是说要让水印

显示在视频的左上角overlay参数为 overlay=0:0

显示在右上角为 overlay= main_w-overlay_w:0

显示在右下角为 overlay= main_w-overlay_w:main_h-overlay_h

显示在左下角为 overlay=0: main_h-overlay_h

上面的0可以改为5,或10像素,以便多留出一些空白。

 

用复合过滤器添加水印:

ffmpeg -i input -i logo -filter_complex 'overlay=10:main_h-overlay_h-10' output

input:输入流

logo:水印文件,也可以是一个流。注意:需要编译时把相应的解码器编译。例如PNG图片。需要编译PNG解码器。Ffmpeg才能够识别图片文件,把图片做为一种流。注意:PNG图片必须含有alpha通道。Overlay过滤器是根据alpha通道来进行复盖的。所以,你想要透明效果时,须先制做一张透明的PNG图片。

output:输出流

也可以用下面命令:

ffmpeg -i input  -vf 'movie=long.png[logo];[in][logo]overlay=10:10[out]' output

movie过滤器用来把两个流组合成一个流。它有一个输出PAD。

 

 

 

转载于:https://www.cnblogs.com/hujihon/p/4979467.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值