标签归档:ffmpeg

ffmpeg .avi 或 .MKV 转换to .MP3

ffmpeg -i "Sample.avi" -vn -ar 44100 -ac 2 -ab 128k -f mp3 "Sample.mp3"
目录查找批量修改方法:
The following command will find all mkv files that are in the current directory and in all sub-folders and extract the audio to mp3 format.

find . -type f -name "*.mkv" -exec bash -c 'FILE="$1"; ffmpeg -i "${FILE}" -vn -c:a libmp3lame -y "${FILE%.mkv}.mp3";' _ '{}' \;
The filename of the audio file will be the same as the mkv video with the correct extension. The mkv extension will be removed and replaced by the mp3 extension e.g hi.mkv will create a new file named hi.mp3

如何使用FFmpeg连接两个MP4文件

我正在尝试用ffmpeg连接3个MP4文件。我需要这是一个自动的过程,因此我选择了ffmpeg。我正在将这3个文件转换成.ts文件,然后将它们连接起来,然后尝试对连接的.ts文件进行编码。这些文件是H 264和AAC编码的,我希望尽可能保持质量不变或接近原版。

FFmpeg有三种级联方法。

1.级联视频滤波器

ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \ -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" \ -map "[v]" -map "[a]" output.mkv

请注意,此方法执行重新编码。

2.凹式破碎机

$ cat mylist.txt
file '/path/to/file1' 
file '/path/to/file2' 
file '/path/to/file3' 
$ ffmpeg -f concat -i mylist.txt -c copy output

Windows操作系统:

(echo file ../../../../1.mp4 & echo file ../../../../2.mp4 )>%tmp%/list.txt
ffmpeg -safe 0 -f concat -i %tmp%/list.txt -c copy c:/output.mp4

3.COAT协议

ffmpeg -i "concat:input1|input2" -codec copy output

该方法不适用于许多格式,包括MP4,因为这些格式的性质以及该方法执行的简单级联。

该用哪一个

  • 级联滤波器:如果输入没有相同的参数(宽度、高度等),或者格式/编解码不相同,或者要执行任何筛选,则使用。
  • 凹式破碎机:当想避免重新编码并且你的格式不支持文件级连接时使用(一般用户使用的大多数文件不支持文件级连接)。
  • COAT协议:使用支持文件级连接的格式(MPEG-1,MPEG-2 PS,DV).不要与MP4一起使用。

如果有疑问,可以试试减刑演示程序。

一个JAVASCRIPT的MPEG1视频和MP2音频解码器JSMpeg

一个JAVASCRIPT的MPEG1视频和MP2音频解码器

JSMpeg是用JavaScript编写的视频播放器。它由MPEG-TS分配器,MPEG1视频和MP2音频解码器,WebGL和Canvas2D渲染器以及WebAudio声音输出组成。JSMpeg可以通过Ajax加载静态文件,并通过WebSocktes实现低延迟流(〜50ms)。

JSMpeg可以在iPhone 5S上以30fps解码720p视频,适用于任何现代浏览器(Chrome,Firefox,Safari和Edge),并以20kb的格式进行压缩。

使用它可以这样简单:

< script  src =  jsmpeg.min.js  > </ script > < div  class =  jsmpeg   data-url =  video.ts  > </ div >

下载

低延迟的视频和音频流,无处不在 – 是的,即使在IPHONE上

JSMpeg可以连接到发送二进制MPEG-TS数据的WebSocket服务器。这些数据来自哪里,取决于你。

延迟时间仅取决于您的来源,网络条件以及您的浏览器,GPU和显示器可以吐出帧的速度。对于本地网络上的屏幕截图,它可以低至50ms。

最简单的方法之一就是使用 ffmpeg一个小型的WebSocket服务器。有关 详细信息,请参阅github上的 文档

使用JSMPEG和进一步阅读的项目


JSMpeg – JavaScript中的MPEG1视频和MP2音频解码器

JSMpeg是用JavaScript编写的视频播放器。它由MPEG-TS分路器,MPEG1视频和MP2音频解码器,WebGL和Canvas2D渲染器以及WebAudio声音输出组成。JSMpeg可以通过Ajax加载静态视频,并通过WebSockets实现低延迟流(〜50ms)。

JSMpeg可以在iPhone 5S上以30fps的速度解码720p视频,适用于任何现代浏览器(Chrome,Firefox,Safari,Edge),并且只需要20KB的压缩文件。

使用它可以这样简单:

< script  src =  jsmpeg.min.js  > </ script > < div  class =  jsmpeg   data-url =  video.ts  > </ div >

一些更多的信息和演示:jsmpeg.com

用法

JSMpeg视频播放器可以使用jsmpeg容器的CSS类在HTML中创建

< div  class =  jsmpeg   data-url =  <url>  > </ div >

或者直接JSMpeg.Player()在JavaScript中调用构造函数:

var player =  new  JSMpeg.Player(url [,options]);

请注意,使用HTML元素(内部JSMpeg.VideoElement)提供了一些功能JSMpeg.Player即一个SVG暂停/播放按钮,并能够在iOS设备上“解锁”音频。

url参数接受一个URL到MPEG的.ts文件或网页套接字服务器(WS:// …)。

options参数支持以下属性:

  • canvas – 用于视频渲染的HTML Canvas元素。如果没有提供,渲染器将创建自己的画布元素。
  • loop – 是否循环播放视频(仅限静态文件)。默认true
  • autoplay – 是否立即开始播放(仅限静态文件)。默认false
  • audio – 是否解码音频。默认true
  • video – 是否解码视频。默认true
  • poster – 在视频播放前用作海报的图像的URL。
  • pauseWhenHidden – 选项卡处于非活动状态时是否暂停播放。默认true请注意,浏览器通常在非活动选项卡上调节JS。
  • disableGl – 是否禁用WebGL并始终使用Canvas2D渲染器。默认false
  • preserveDrawingBuffer– WebGL上下文是否创建preserveDrawingBuffer– 通过“屏幕截图”需要canvas.toDataURL()默认false
  • progressive – 是否以块(仅限静态文件)加载数据。启用时,可以在整个源完全加载之前开始播放。默认true
  • throttled– 使用时progressive,是否推迟加载块,当他们不需要播放呢。默认true
  • chunkSize– 使用时progressive,以字节为单位的块大小一次加载。默认1024*1024(1mb)。
  • decodeFirstFrame – 是否解码并显示视频的第一帧。用于设置画布大小,并使用该框架作为“海报”图像。这在使用autoplay或流媒体源时不起作用默认true
  • maxAudioLag – 流式传输时,以秒为单位的最大入队音频长度。
  • videoBufferSize – 流传输时,视频解码缓冲区的大小(以字节为单位)。默认512 * 1024(512kb)。您可能需要增加非常高的比特率。
  • audioBufferSize – 流式传输时,音频解码缓冲区的大小(以字节为单位)。默认128 * 1024(128kb)。您可能需要增加非常高的比特率。

除了from之外的所有选项canvas也可以通过data-属性与HTML Element一起使用例如,要在JavaScript中指定循环和自动播放:

var player =  new  JSMpeg.Player' video.ts ' {loop  true,autoplay  true });

或HTML

< div  class =  jsmpeg   data-url =  video.ts   data-loop =  true   data-autoplay =  true  > </ div >

请注意,camelCased用作数据属性时选项必须使用连字符。例如decodeFirstFrame: true成为data-decode-first-frame="true"HTML元素。

JSMpeg.Player API

一个JSMpeg.Player实例支持以下方法和属性:

  • .play() – 开始播放
  • .pause() – 暂停播放
  • .stop() – 停止播放并寻求开始
  • .destroy() – 停止播放,断开源代码并清理WebGL和WebAudio状态。玩家以后不能使用。
  • .volume – 获取或设置音量(0-1)
  • .currentTime – 以秒为单位获取或设置当前播放位置

为JSMpeg编码视频/音频

JSMpeg只支持MPEG1视频编解码器和MP2音频编解码器的MPEG-TS容器播放。视频解码器不能正确处理B帧(尽管现在的编码器似乎没有默认使用这些),视频的宽度必须是2的倍数。

你可以使用ffmpeg编码一个合适的视频,如下所示:

ffmpeg -i in.mp4 -f mpegts -codec:v mpeg1video -codec:a mp2 -b 0 out.ts

您还可以控制视频大小(-s),帧率(-r),视频比特率(-b:v),音频比特率(-b:a),音频通道数量(-ac),采样率(-ar)等等。有关详细信息,请参阅ffmpeg文档。

综合例子:

ffmpeg -i in.mp4 -f mpegts \ -codec:v mpeg1video -s 960x540 -b:v 1500k -r 30 -bf 0 \ -codec:a mp2 -ar 44100 -ac 1 -b:a 128k \ out.ts

性能考虑

尽管JSMpeg甚至可以在iPhone 5S上以30fps处理720p视频,但请记住,MPEG1并不像现代编解码器那样高效。MPEG1需要相当多的高清视频带宽。720p在2 Mbits / s(250kb / s)时开始显示正常。而且,比特率越高,JavaScript解码的工作就越多。

这不应该是一个静态文件的问题,或者如果你只在你的本地WiFi流。如果您不需要支持移动设备,那么10mbit / s的1080p就可以正常工作(如果您的编码器能够继续使用)。对于其他一切,我建议您使用最大2Mbit / s的540p(960×540)。

通过WebSockets流媒体

JSMpeg可以连接到发送二进制MPEG-TS数据的WebSocket服务器。在流式传输时,JSMpeg会尽可能降低延迟 – 它会立即解码所有内容,忽略视频和音频时间戳。为了使所有内容保持同步(以及延迟较低),音频数据应该非常频繁地在视频帧之间交错(-muxdelay在ffmpeg中)。

一个单独的,缓冲的流模式,其中JSMpeg预加载数秒的数据,并提供准确的时间和音频/视频同步的一切是可以想象的,但目前尚未实现。

视频和音频的内部缓冲区相当小(分别为512kb和128kb),JSMpeg将丢弃旧的(甚至未播放的)数据,为新到达的数据腾出空间,而不会有太多模糊。这可能在网络拥塞时引入解码伪像,但确保延迟保持在最低限度。如有必要,您可以增加videoBufferSizeaudioBufferSize通过选项。

JSMpeg附带一个用Node.js编写的小型WebSocket“relay”。该服务器通过HTTP接受MPEG-TS源,并通过WebSocket将其提供给所有连接的浏览器。传入的HTTP流可以使用ffmpeggstreamer或其他方式生成。

源和WebSocket中继之间的拆分是必要的,因为ffmpeg不会说WebSocket协议。但是,这种拆分还允许您在公共服务器上安装WebSocket中继,并在Internet上共享您的流(通常,路由器中的NAT会阻止公共Internet连接本地网络)。

总之,它的工作原理是这样的:

  1. 运行websocket-relay.js
  2. 运行ffmpeg,将输出发送到中继的HTTP端口
  3. 将浏览器中的JSMpeg连接到中继的Websocket端口

流的示例设置:Raspberry Pi Live网络摄像头

对于这个例子,ffmpeg和WebSocket中继在同一个系统上运行。这使您可以在本地网络中查看流,但不能在公共网络上查看。

这个例子假定你的网络摄像机与Video4Linux2兼容,并且出现/dev/video0在文件系统中。大多数USB摄像头支持UVC标准,应该工作得很好。通过加载内核模块,板载树莓相机可以作为V4L2设备使用:sudo modprobe bcm2835-v4l2

  1. 安装ffmpeg(请参阅如何在Debian / Raspbian上安装ffmpeg)。使用ffmpeg,我们可以捕捉网络摄像头的视频和音频,并将其编码成MPEG1 / MP2。

  2. 安装Node.js和npm(请参阅在基于Debian和Ubuntu的Linux发行上安装Node.js以获得更新的版本)。Websocket中继是用Node.js编写的

  3. 安装http-server。我们将使用它来提供静态文件(view-stream.html,jsmpeg.min.js),以便我们可以在浏览器中用视频查看网站。任何其他网络服务器也可以工作(nginx,apache等): sudo npm -g install http-server

  4. 安装git并克隆这个仓库(或者直接将它下载为ZIP并解压)

sudo apt-get install git git clone https://github.com/phoboslab/jsmpeg.git 
  1. 转到jsmpeg /目录 cd jsmpeg/

  2. 安装Node.js Websocket库: npm install ws

  3. 启动Websocket中继。为输入的HTTP视频流和我们可以在浏览器中连接的Websocket端口提供密码和端口: node websocket-relay.js supersecret 8081 8082

  4. 在一个新的终端窗口(仍然在jsmpeg/目录中,启动,http-server所以我们可以服务view-stream.html到浏览器: http-server

  5. 在浏览器中打开流式传输网站。http-server会告诉你的IP(通常192.168.[...])和端口(通常8080)在哪里运行:http://192.168.[...]:8080/view-stream.html

  6. 在第三个终端窗口中,启动ffmpeg来捕捉摄像头视频并将其发送到WebSocket中继。在目标网址中提供密码和端口(从第7步开始):

ffmpeg \ -f v4l2 \ -framerate 25 -video_size 640x480 -i /dev/video0 \ -f mpegts \ -codec:v mpeg1video -s 640x480 -b:v 1000k -bf 0 \ http://localhost:8081/supersecret 

您现在应该在您的浏览器中看到一个活的摄像头图像。

如果ffmpeg无法打开输入视频,则可能是因为您的摄像头不支持给定的分辨率,格式或帧速率。要获得兼容模式的列表,请运行:

ffmpeg -f v4l2 -list_formats all -i /dev/video0

要添加摄像头音频,只需使用两个独立的输入调用ffmpeg即可。

ffmpeg \ -f v4l2 \ -framerate 25 -video_size 640x480 -i /dev/video0 \ -f alsa \ -ar 44100 -c 2 -i hw:0 \ -f mpegts \ -codec:v mpeg1video -s 640x480 -b:v 1000k -bf 0 \ -codec:a mp2 -b:a 128k \ -muxdelay 0.001 \ http://localhost:8081/supersecret 

注意这个muxdelay论点。这应该会减少延迟,但在流式传输视频和音频时并不总是有效 – 请参阅下面的注释。

关于fmpeg muxing和延迟的一些评论

将音频流添加到MPEG-TS有时会引起相当大的延迟。我特别发现这是使用ALSA和V4L2的Linux上的一个问题(在macOS上使用AVFoundation工作得很好)。但是,有一个简单的解决方法:只运行ffmpeg的两个实例。一个是音频,一个是视频。发送两个输出到相同的Websocket中继。由于MPEG-TS格式的简单性,两个流的适当的“混合”在继电器中自动发生。

ffmpeg \ -f v4l2 \ -framerate 25 -video_size 640x480 -i /dev/video0 \ -f mpegts \ -codec:v mpeg1video -s 640x480 -b:v 1000k -bf 0 \ -muxdelay 0.001 \ http://localhost:8081/supersecret  # In a second terminal ffmpeg \ -f alsa \ -ar 44100 -c 2 -i hw:0 \ -f mpegts \ -codec:a mp2 -b:a 128k \ -muxdelay 0.001 \ http://localhost:8081/supersecret 

在我的测试中,USB摄像头引入了大约180ms的延迟,似乎我们无能为力。树莓派然而有一个摄像头模块,提供较低的延迟视频采集。

要使用ffmpeg捕获Windows或MacOS上的网络摄像头输入,请参阅ffmpeg Capture / Webcam Wiki

JSMpeg架构和内部

这个库建立在一个相当模块化的方式,同时保持在最低限度的开销。应该可以实现新的分配器,解码器,输出(渲染器,音频设备)或信号源,而无需更改任何其他部分。但是,您仍然需要继承JSMpeg.Player以使用任何新的模块。

查看一下jsmpeg.js源代码,了解这些模块如何互连以及应该提供哪些API。我还写了一篇关于JSMpeg内部部分的博文:解码就像是1999年

使用库的一部分而不创建一个完整的播放器也应该相当简单。例如,您可以创建一个独立的JSMpeg.Decoder.MPEG1Video实例.connect()一个渲染器,.write()一些数据和.decode()一个框架,而不需要触及JSMpeg的其他部分。

以前的版本

目前生活在这个回购的JSMpeg版本是完全重写原始的jsmpeg库,只能够解码原始mpeg1video。如果您正在查找旧版本,请参阅v0.2标签

ffmpeg 文件推流 rtsp和rtmp

// rtsp推流  /opt/srs/srs2

./objs/ffmpeg/bin/ffmpeg -re -i /root/mp4/1.mp4 -vcodec copy -codec copy -f rtsp rtsp://192.168.2.161/live/rtsp_test

// rtmp 推流

./objs/ffmpeg/bin/ffmpeg -re -i /root/mp4/1.flv -vcodec copy -codec copy -f flv rtmp://192.168.2.161/live/rtsp_test

// rtsp推流  /opt/srs/srs2

./objs/ffmpeg/bin/ffmpeg -re -i /root/mp4/1.mp4 -vcodec copy -codec copy -f rtsp rtsp://192.168.2.161/live/rtsp_test

// rtmp 推流./objs/ffmpeg/bin/ffmpeg -re -i /root/mp4/1.flv -vcodec copy -codec copy -f flv rtmp://192.168.2.161/live/rtsp_test

使用ffmpeg合并视频文件的三种方法

ffmpeg合并视频的方法有三种。国内大多数仅介绍了其中之一。于是觉得有必要翻译一下。其实在ffmpeg的 FAQ文档中有比较详细的说明。

  1. 使用concat协议进行视频文件的合并

    这种方式的适用场景是:视频容器是MPEG-1, MPEG-2 PS或DV等可以直接进行合并的。换句话说,其实可以直接用cat或者copy之类的命令来对视频直接进行合并。很多文章介绍了这种方法,但适用性却没有提及。这并不是一个通用的方法。典型的命令示例如下:

    ffmpeg -i concat:"intermediate1.mpg|intermediate2.mpg" -c copy intermediate_all.mpg
  2. 使用concat demuxer进行视频文件的合并

    这种合并方式的适用场景是:当容器格式不支持文件层次的合并,而又不想(不需要)进行再编码的操作的时候。这种方式对源视频同样有同格式同性质的要求。其详细语法参见 这里 。典型的命令示例如下:

    ffmpeg -f concat -i Cam01.txt -c copy Cam01.mp4

    其中,Cam01.txt 为包含了输入文件的描述文件。

  3. 使用concat滤镜(filter)进行视频文件的合并:

    当需要进行任意程度的重新编解码时,官方推荐使用的方法即是用concat滤镜来进行视频文件的合并处理。详细说明参见 这里 。典型命令示例如下:

    ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv -filter_complex \ '[0:0] [0:1] [0:2] [1:0] [1:1] [1:2] [2:0] [2:1] [2:2]
       concat=n=3:v=1:a=2 [v] [a1] [a2]' \
      -map '[v]' -map '[a1]' -map '[a2]' output.mkv

    这段命令目的是将三段双语格式的视频合并至最终的一段视频(output.mkv)。参数n=3说明待合成的视频有三段,v=1说明视频流为一,a=2说明音频流为二。 -map参数的详细说明可以从Filtergraph文档中找到。

众所周知,从某些视频网站下载的视频是分段的。比如新浪视频每隔6分钟分段,俗称“6分钟诅咒”。
现在的任务是将这些视频片段合并起来,并且尽量无损。

方法一:FFmpeg concat 协议

对于 MPEG 格式的视频,可以直接连接:

ffmpeg -i "concat:input1.mpg|input2.mpg|input3.mpg" -c copy output.mpg

对于非 MPEG 格式容器,但是是 MPEG 编码器(H.264、DivX、XviD、MPEG4、MPEG2、AAC、MP2、MP3 等),可以包装进 TS 格式的容器再合并。在新浪视频,有很多视频使用 H.264 编码器,可以采用这个方法
ffmpeg -i input1.flv -c copy -bsf:v h264_mp4toannexb -f mpegts input1.ts
ffmpeg -i input2.flv -c copy -bsf:v h264_mp4toannexb -f mpegts input2.ts
ffmpeg -i input3.flv -c copy -bsf:v h264_mp4toannexb -f mpegts input3.ts
ffmpeg -i "concat:input1.ts|input2.ts|input3.ts" -c copy -bsf:a aac_adtstoasc -movflags +faststart output.mp4
保存 QuickTime/MP4 格式容器的时候,建议加上 -movflags +faststart。这样分享文件给别人的时候可以边下边看。

方法二:FFmpeg concat 分离器

这种方法成功率很高,也是最好的,但是需要 FFmpeg 1.1 以上版本。先创建一个文本文件filelist.txt
file 'input1.mkv'
file 'input2.mkv'
file 'input3.mkv'
然后:

ffmpeg -f concat -i filelist.txt -c copy output.mkv

注意:使用 FFmpeg concat 分离器时,如果文件名有奇怪的字符,要在 filelist.txt 中转义。

方法三:Mencoder 连接文件并重建索引

这种方法只对很少的视频格式生效。幸运的是,新浪视频使用的 FLV 格式是可以这样连接的。对于没有使用 MPEG 编码器的视频(如 FLV1 编码器),可以尝试这种方法,或许能够成功。

mencoder -forceidx -of lavf -oac copy -ovc copy -o output.flv input1.flv input2.flv input3.flv

方法四:使用 FFmpeg concat 过滤器重新编码(有损)

语法有点复杂,但是其实不难。这个方法可以合并不同编码器的视频片段,也可以作为其他方法失效的后备措施。

ffmpeg -i input1.mp4 -i input2.webm -i input3.avi -filter_complex '[0:0] [0:1] [1:0] [1:1] [2:0] [2:1] concat=n=3:v=1:a=1 [v] [a]' -map '[v]' -map '[a]' <编码器选项> output.mkv

如你所见,上面的命令合并了三种不同格式的文件,FFmpeg concat 过滤器会重新编码它们。注意这是有损压缩。
[0:0] [0:1] [1:0] [1:1] [2:0] [2:1] 分别表示第一个输入文件的视频、音频、第二个输入文件的视频、音频、第三个输入文件的视频、音频。concat=n=3:v=1:a=1 表示有三个输入文件,输出一条视频流和一条音频流。[v] [a] 就是得到的视频流和音频流的名字,注意在 bash 等 shell 中需要用引号,防止通配符扩展。

提示

  1. 以上三种方法,在可能的情况下,最好使用第二种。第一种次之,第三种更次。第四种是后备方案,尽量避免。
  2. 规格不同的视频合并后可能会有无法预测的结果。
  3. 有些媒体需要先分离视频和音频,合并完成后再封装回去。
  4. 对于 Packed B-Frames 的视频,如果封装成 MKV 格式的时候提示 Can't write packet with unknown timestamp,尝试在 FFmpeg 命令的 ffmpeg 后面加上 -fflags +genpts

ffprobe使用大全

目录

1. 语法

2. 描述

3. 选项

3.1 流指示符

3.2 通用选项

3.3 音视频选项

3.4 主选项

4. 写入器

4.1 默认值

4.2 compact, csv

4.3 flat

4.4 ini

4.5 json

4.6 xml

5. Timecode



1. 语法

  ffprobe [options] [‘input_file’]



2. 描述

ffprobe收集多媒体文件或流的信息,并以人和机器可读的方式输出。

例如,它可以用来检查多媒体流的容器格式,以用每个流的类型和格式;

如果指定的以文件作为输入,ffprobe会打开文件并解析文件内容,如果文件打开失败或文件不是媒体文件,则返回负值。



ffprobe可以作为一个命令行程序独立使用,也可以于一个文本过滤器组合使用,从而实现更复杂的处理,

如统计处理或绘图。

选项用来列出ffprobe支持的格式,指定要显示的信息,和设置如何显示。



ffprobe的输出设计成可以方便地被文本过滤器解析,由一个或多个章节组成,由“print_format”选项指定。

章节是递归的,都有一个唯一的标识名。



容器或流中的Metadata标签识别成”FORMAT”,”STREAM”或”PROGRAM_STREAM”章节



3. 选项

如果没有特别的声明,所有的数字选项都可带一个表示数字的字符串,如’K’,’M’,’G’.



3.1 流指示符

很多选项可应用于每个流,如码率或codec.

流指示符用于明确指示给定的选项属于哪个流。



流指示符是跟在选项名后的字符串,由冒号分隔。

例如:

   -codec:a:1 ac3

它包含了 a:1 这个流指示符,用于匹配第二个音频流,因此,整个意思是选择AC3 codec来处理第二个音频流。



一个流指示符可以匹配多个流,因此选项也可以同时作用于它们。

例如,

   -b:a 128k     匹配所有的音频流;



空流指示符匹配所有流。

例如:

   -codec copy 或 -codec:copy  指示所有的流都不进行再编码;



流指示符的可能形式:

‘stream_index’ 使用索引号来匹配流;

例如:

   -threads:1 4 

将设置第二个流的线程数为4;



‘stream_type[:stream_index]’

stream_type可以是下列之一: 

‘v’  为视频;

‘a’  为音频;

‘s’  为子目录;

‘d’  为数据;

‘t’  为附录。

如果指定了stream_index,那么这些类型只能stream_index指定的流有效,否则,对所有的流起作用; 



‘p:program_id[:stream_index]’

如果指定了stream_index, 那么它只匹配id号为program_id的由stream_index指定的流,否则,匹配节目中的所有流。



‘#stream_id or i:stream_id’

匹配stream_id指定的流(如, MPEG-TS容器中的PID)



3.2 通用选项

这些选项可用于所有ff* 工具。

-L : Show license.

-h, -?, -help, –help [arg]

       显示帮助. 

       arg 的值可以是:

         ‘long’ , Print advanced tool options in addition to the basic tool options.

         ‘full’ , Print complete list of options, including shared and private options for encoders, 

                     decoders, demuxers, muxers, filters, etc.

decoder=decoder_name

       Print detailed information about the decoder named decoder_name. 

       Use the ‘-decoders’ option to get a list of all decoders.

encoder=encoder_name

       Print detailed information about the encoder named encoder_name. 

       Use the ‘-encoders’ option to get a list of all encoders.

demuxer=demuxer_name

       Print detailed information about the demuxer named demuxer_name. 

       Use the ‘-formats’ option to get a list of all demuxers and muxers.

muxer=muxer_name

       Print detailed information about the muxer named muxer_name. 

       Use the ‘-formats’ option to get a list of all muxers and demuxers.

filter=filter_name

       Print detailed information about the filter name filter_name. 

       Use the ‘-filters’ option to get a list of all filters.

-version :Show version.

-formats :Show available formats.

-codecs  :Show all codecs known to libavcodec.

       Note that the term ’codec’ is used throughout this documentation as a shortcut 

       for what is more correctly called a media bitstream format.

-decoders:Show available decoders.

-encoders:Show all available encoders.

-bsfs    :显示有效的流过滤器

-protocols:Show available protocols.

-filters :Show available libavfilter filters.

-pix_fmts:Show available pixel formats.

-sample_fmts:Show available sample formats.

-layouts:显示通道名和标准的通道布局

-colors :Show recognized color names.

-loglevel [repeat+]loglevel | -v [repeat+]loglevel

       设置库中的日志级别。

       “repeat+”表示不将重复的日志输出都放在第一行,并去掉”Last message repeated n times”行。

       如果“repeat”单独使用并没有更高的loglevel设置,则使用默认的级别;

       如果指定了多个loglevel的参数,使用’repeat’将不会改变loglevel。

       loglevel是一个数字或字符串,可以使用的值如下:

        ‘quiet’  Show nothing at all; be silent.

        ‘panic’  Only show fatal errors which could lead the process to crash,

                   such as and assert failure. This is not currently used for anything.

        ‘fatal’  Only show fatal errors. These are errors after which the process absolutely cannot continue after.

        ‘error’  Show all errors, including ones which can be recovered from.

        ‘warning’Show all warnings and errors. 

                   Any message related to possibly incorrect or unexpected events will be shown.

        ‘info’   Show informative messages during processing. This is in addition to warnings and errors. 

                   This is the default value.

        ‘verbose’Same as info, except more verbose.

        ‘debug’  Show everything, including debugging information.



默认的程序日志是输出到stderr, 如果终端支持颜色,则error和warning会标识成不同的颜色。



-report :导出所有命令行和控制台的输出到文件,文件位于当前路径,命名为program-YYYYMMDD-HHMMSS.log.

          这个文件可以用作bug报告,也样也使用-loglevel选项设置。

          设置环境变量FFREPORT成任意值有同样的效果。

        

Setting the environment variable FFREPORT to any value has the same effect. 

If the value is a ’:’-separated key=value sequence, these options will affect the report;

options values must be escaped if they contain special characters or the options delimiter ’:’ 

(see the “Quoting and escaping” section in the ffmpeg-utils manual). The following option is recognized:

file    :set the file name to use for the report; 

          %p is expanded to the name of the program, 

          %t is expanded to a timestamp, %% is expanded to a plain %

          Errors in parsing the environment variable are not fatal, and will not appear in the report.



-hide_banner :关闭版本声明输出.

          All FFmpeg tools will normally show a copyright notice, build options and library versions.

          This option can be used to suppress printing this information.



-cpuflags flags (global)

          Allows setting and clearing cpu flags. This option is intended for testing. 

          Do not use it unless you know what you’re doing.

          ffmpeg -cpuflags -sse+mmx …

          ffmpeg -cpuflags mmx …

          ffmpeg -cpuflags 0 …

          Possible flags for this option are:

          ‘x86’

          ‘mmx’

          ‘mmxext’

          ‘sse’

          ‘sse2’

          ‘sse2slow’

          ‘sse3’

          ‘sse3slow’

          ‘ssse3’

          ‘atom’

          ‘sse4.1’

          ‘sse4.2’

          ‘avx’

          ‘xop’

          ‘fma4’

          ‘3dnow’

          ‘3dnowext’

          ‘cmov’

          ‘ARM’

          ‘armv5te’

          ‘armv6’

          ‘armv6t2’

          ‘vfp’

          ‘vfpv3’

          ‘neon’

          ‘PowerPC’

          ‘altivec’

          ‘Specific Processors’

          ‘pentium2’

          ‘pentium3’

          ‘pentium4’

          ‘k6’

          ‘k62’

          ‘athlon’

          ‘athlonxp’

          ‘k8’

-opencl_bench: 基准测试所有openCL设备并显示结果. 

          This option is only available when FFmpeg has been compiled with –enable-opencl.



-opencl_options options (global):

          Set OpenCL environment options. This option is only available when FFmpeg has been compiled with –enable-opencl.

          options must be a list of key=value option pairs separated by ’:’.

          See the “OpenCL Options” section in the ffmpeg-utils manual for the list of supported options.



3.3 音视频选项

这些选项直接由libavformat, libavdevice和libavcodec库提供,它们可以分成两类:

generic : 这些选项可以用于设置所有容器,codec或设备。

          一般的选项都列在AVFormatContext容器/设备之下,并根据AVCodecContext中选择编解码器。

private : 这些选项用于设置指定的容器,设备和codec. 私有选项都列在它们对应的容器/设备/codec下。



例如:

写一个ID3v2.3头来代替默认的ID3v2.4头到一个MP3文件,使用MP3混合器的”id3v2_version”私有选项:

    ffmpeg -i input.flac -id3v2_version 3 out.mp3



所有AVOption选项可作用于每个流,因此使用流指示符来指示作用于特定流。

Note: the ‘-nooption’ syntax cannot be used for boolean AVOptions, use ‘-option 0’/‘-option 1’.

Note: the old undocumented way of specifying per-stream AVOptions by prepending v/a/s to the options name is now obsolete and will be removed soon.



3.4 主选项

-f format :Force format to use.

-unit     :Show the unit of the displayed values.

-prefix   :Use SI prefixes for the displayed values. 

            Unless the “-byte_binary_prefix” option is used all the prefixes are decimal.

-byte_binary_prefix

          :Force the use of binary prefixes for byte values.

-sexagesimal

          :Use sexagesimal format HH:MM:SS.MICROSECONDS for time values.

-pretty:Prettify the format of the displayed values, 

             it corresponds to the options “-unit -prefix -byte_binary_prefix -sexagesimal”.

-of, -print_format writer_name[=writer_options]

          :设置输出打印格式

            writer_name 指定写入器的名字,

            writer_options指定传输给写入器的选项

           For example for printing the output in JSON format, specify:

            -print_format json

           For more details on the available output printing formats, see the Writers section below.

-sections:Print sections structure and section information, and exit. The output is not meant to be parsed by a machine.

-select_streams stream_specifier:

           选择邮stream_sepcifier指定的流

           This option affects only the options related to streams (e.g. show_streams, show_packets, etc.).

           For example to show only audio streams, you can use the command:

              ffprobe -show_streams -select_streams a INPUT

           To show only video packets belonging to the video stream with index 1:

              ffprobe -show_packets -select_streams v:1 INPUT

-show_data: 显示负载数据, as a hexadecimal and ASCII dump.

             Coupled with ‘-show_packets’, it will dump the packets’ data. 

             Coupled with ‘-show_streams’, it will dump the codec extradata.

             The dump is printed as the “data” field. It may contain newlines.

show_error:显示出错信息

            The error information is printed within a section with name “ERROR”.

-show_format:显示容器格式信息

            All the container format information is printed within a section with name “FORMAT”.

-show_format_entry name

            Like ‘-show_format’, but only prints the specified entry of the container format information, 

            rather than all. This option  may be given more than once, then all specified entries will be shown.

            This option is deprecated, use show_entries instead.

-show_entries section_entries:Set list of entries to show.

            Entries are specified according to the following syntax. 

            section_entries contains a list of section entries separated by :. 





            Each section entry is composed by a section name (or unique name), 

            optionally followed by a list of entries local to that section, separated by ,.





If section name is specified but is followed by no =, all entries are printed to output, 

together with all the contained sections. Otherwise only the entries specified in the local section entries list are printed. In particular, if = is specified but the list of local entries is empty, then no entries will be shown for that section.





Note that the order of specification of the local section entries is not honored in the output, 

and the usual display order will be retained.

The formal syntax is given by:

LOCAL_SECTION_ENTRIES ::= SECTION_ENTRY_NAME[,LOCAL_SECTION_ENTRIES]

SECTION_ENTRY         ::= SECTION_NAME[=[LOCAL_SECTION_ENTRIES]]

SECTION_ENTRIES       ::= SECTION_ENTRY[:SECTION_ENTRIES]

For example, to show only the index and type of each stream, and the PTS time, duration time, and stream index of the packets, 





you can specify the argument:

  packet=pts_time,duration_time,stream_index : stream=index,codec_type

To show all the entries in the section “format”, but only the codec type in the section “stream”, specify the argument:

  format : stream=codec_type

To show all the tags in the stream and format sections:

  format_tags : format_tags

To show only the title tag (if available) in the stream sections:

  stream_tags=title





-show_packets:显示每个包信息

        The information for each single packet is printed within a dedicated section with name “PACKET”.

-show_frames :Show information about each frame and subtitle contained in the input multimedia stream.

        The information for each single frame is printed within a dedicated section with name “FRAME” or “SUBTITLE”.

-show_streams:Show information about each media stream contained in the input multimedia stream.

       Each media stream information is printed within a dedicated section with name “STREAM”.

-show_programs:

       Show information about programs and their streams contained in the input multimedia stream.

       Each media stream information is printed within a dedicated section with name “PROGRAM_STREAM”.

-show_chapters

       Show information about chapters stored in the format.

       Each chapter is printed within a dedicated section with name “CHAPTER”.

-count_frames

       Count the number of frames per stream and report it in the corresponding stream section.

-count_packets

       Count the number of packets per stream and report it in the corresponding stream section.

-read_intervals read_intervals

       Read only the specified intervals. 

       read_intervals must be a sequence of interval specifications separated by “,”. 

       ffprobe will seek to the interval starting point, and will continue reading from that.

       Each interval is specified by two optional parts, separated by “%”.





The first part specifies the interval start position. It is interpreted as an abolute position, 

or as a relative offset from the current position if it is preceded by the “+” character. 

If this first part is not specified, no seeking will be performed when reading this interval.





The second part specifies the interval end position. It is interpreted as an absolute position, 

or as a relative offset from the current position if it is preceded by the “+” character. 

If the offset specification starts with “#”, it is interpreted as the number of packets to read 

(not including the flushing packets) from the interval start. If no second part is specified, 

the program will read until the end of the input.





Note that seeking is not accurate, thus the actual interval start point may be different from the specified position. 

Also, when an interval duration is specified, the absolute end time will be computed by adding 

the duration to the interval start point found by seeking the file, rather than to the specified start value.





The formal syntax is given by:

INTERVAL  ::= [START|+START_OFFSET][%[END|+END_OFFSET]]

INTERVALS ::= INTERVAL[,INTERVALS]

A few examples follow.

Seek to time 10, read packets until 20 seconds after the found seek point, 

then seek to position 01:30 (1 minute and thirty seconds) and read packets until position 01:45.

    10%+20,01:30%01:45

Read only 42 packets after seeking to position 01:23: 

    01:23%+#42

Read only the first 20 seconds from the start:

    %+20

Read from the start until position 02:30: 

    %02:30





-show_private_data, -private

   Show private data, that is data depending on the format of the particular shown element. 

   This option is enabled by default, but you may need to disable it for specific uses, 

   for example when creating XSD-compliant XML output.

-show_program_version

   Show information related to program version.

   Version information is printed within a section with name “PROGRAM_VERSION”.

-show_library_versions

   Show information related to library versions.

   Version information for each library is printed within a section with name “LIBRARY_VERSION”.



-show_versions

   Show information related to program and library versions. 

   This is the equivalent of setting both ‘-show_program_version’ and ‘-show_library_versions’ options.



-bitexact

   Force bitexact output, useful to produce output which is not dependent on the specific build.



-i input_file

   读取input_file.



4. 写入器

写入器定义了ffprobe的输出格式,将用于打印所有输出

A writer may accept one or more arguments, which specify the options to adopt. 

The options are specified as a list of key=value pairs, separated by “:”.

All writers support the following options:

‘string_validation, sv’ Set string validation mode.

The following values are accepted.

‘fail’

   The writer will fail immediately in case an invalid string (UTF-8) sequence or code point is found in the input. 

   This is especially useful to validate input metadata.



‘ignore’

   Any validation error will be ignored. This will result in possibly broken output, especially with the json or xml writer.



‘replace’

   The writer will substitute invalid UTF-8 sequences or code points with the string specified with the 



‘string_validation_replacement’.

   Default value is ‘replace’.

‘string_validation_replacement, svr’

   Set replacement string to use in case ‘string_validation’ is set to ‘replace’.



In case the option is not specified, the writer will assume the empty string, that is it will remove the 

invalid sequences from the input strings.

A description of the currently available writers follows.



4.1 默认值 

默认格式 

Print each section in the form: 

   [SECTION]

   key1=val1

   …

   keyN=valN

   [/SECTION]

Metadata tags are printed as a line in the corresponding FORMAT, STREAM or PROGRAM_STREAM section, 

and are prefixed by the string “TAG:”.



A description of the accepted options follows.

‘nokey, nk’

   If set to 1 specify not to print the key of each field. Default value is 0.



‘noprint_wrappers, nw’

   If set to 1 specify not to print the section header and footer. Default value is 0.



4.2 compact, csv

Compact and CSV format.

The csv writer is equivalent to compact, but supports different defaults.

Each section is printed on a single line. If no option is specifid, the output has the form:

   section|key1=val1| … |keyN=valN

Metadata tags are printed in the corresponding “format” or “stream” section. 

A metadata tag key, if printed, is prefixed by the string “tag:”.



The description of the accepted options follows.



‘item_sep, s’

Specify the character to use for separating fields in the output line. It must be a single printable character, it is “|” by 



default (“,” for the csv writer).



‘nokey, nk’

If set to 1 specify not to print the key of each field. Its default value is 0 (1 for the csv writer).



‘escape, e’

Set the escape mode to use, default to “c” (“csv” for the csv writer).



It can assume one of the following values:



‘c’

Perform C-like escaping. Strings containing a newline (’\n’), carriage return (’\r’), a tab (’\t’), a form feed (’\f’), 

the escaping character (’\’) or the item separator character SEP are escaped using C-like fashioned escaping, so that a 

newline is converted to the sequence “\n”, a carriage return to “\r”, ’\’ to “\\” and the separator SEP is converted to 

“\SEP”.

‘csv’

Perform CSV-like escaping, as described in RFC4180. Strings containing a newline (’\n’), a carriage return (’\r’), a double 

quote (’”’), or SEP are enclosed in double-quotes.

‘none’

Perform no escaping.

‘print_section, p’

Print the section name at the begin of each line if the value is 1, disable it with value set to 0. Default value is 1.



4.3 flat

Flat format.

A free-form output where each line contains an explicit key=value, such as “streams.stream.3.tags.foo=bar”. The output is shell 

escaped, so it can be directly embedded in sh scripts as long as the separator character is an alphanumeric character or an 

underscore (see sep_char option).

The description of the accepted options follows.

‘sep_char, s’

Separator character used to separate the chapter, the section name, IDs and potential tags in the printed field key.

Default value is ’.’.

‘hierarchical, h’

Specify if the section name specification should be hierarchical. If set to 1, and if there is more than one section in the 

current chapter, the section name will be prefixed by the name of the chapter. A value of 0 will disable this behavior.

Default value is 1.



4.4 ini

INI format output.

Print output in an INI based format.

The following conventions are adopted:

all key and values are UTF-8

’.’ is the subgroup separator

newline, ’\t’, ’\f’, ’\b’ and the following characters are escaped

’\’ is the escape character

’#’ is the comment indicator

’=’ is the key/value separator

’:’ is not used but usually parsed as key/value separator

This writer accepts options as a list of key=value pairs, separated by “:”.

The description of the accepted options follows.

‘hierarchical, h’

Specify if the section name specification should be hierarchical. If set to 1, and if there is more than one section in the 

current chapter, the section name will be prefixed by the name of the chapter. A value of 0 will disable this behavior.

Default value is 1.



4.5 json

JSON based format.

Each section is printed using JSON notation.

The description of the accepted options follows.

‘compact, c’

If set to 1 enable compact output, that is each section will be printed on a single line. Default value is 0.

For more information about JSON, see http://www.json.org/.



4.6 xml

XML based format.

The XML output is described in the XML schema description file ‘ffprobe.xsd’ installed in the FFmpeg datadir.

An updated version of the schema can be retrieved at the url http://www.ffmpeg.org/schema/ffprobe.xsd, which redirects to the 

latest schema committed into the FFmpeg development source code tree.

Note that the output issued will be compliant to the ‘ffprobe.xsd’ schema only when no special global output options (‘unit

’, ‘prefix’, ‘byte_binary_prefix’, ‘sexagesimal’ etc.) are specified.

The description of the accepted options follows.

‘fully_qualified, q’

If set to 1 specify if the output should be fully qualified. Default value is 0. This is required for generating an XML file 

which can be validated through an XSD file.

‘xsd_compliant, x’

If set to 1 perform more checks for ensuring that the output is XSD compliant. Default value is 0. This option automatically 

sets ‘fully_qualified’ to 1.

For more information about the XML format, see http://www.w3.org/XML/.





5. 时间戳

ffprobe supports Timecode extraction:



MPEG1/2 timecode is extracted from the GOP, and is available in the video stream details 

(‘-show_streams’, see timecode).MOV timecode is extracted from tmcd track,

so is available in the tmcd stream metadata (‘-show_streams’, see TAG:timecode).

DV, GXF and AVI timecodes are available in format metadata (‘-show_format’, see TAG:timecode).

ffmpeg, ffplay, ffprobe用法

1. 使用ffmepg

将input.avi转码成output.ts,并设置视频的码率为640kbs

用法举例:ffmpeg -i input.avi -b:v 640k output.ts



详细使用说明(英文):http://ffmpeg.org/ffmpeg.html

将多张图片压缩成一个视频

[html] view plain copy

 print?在CODE上查看代码片派生到我的代码片

  1. ffmpeg -framerate 1/5 -i images2\%03d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4  

-framerate 1/5: 输入帧率为5秒,也就说一张图片,在压缩后的out.mp4中要显示5秒。

-i image\%03d.png: 表示输入图片的位置

-c:v libx264: 压缩算法

-r 30:表示输出图片的帧速率为 30帧/秒

-pix_fmt yuv420p:表示像素格式

out.mp4:输出的文件名(当前文件夹)

下面是我测试的过程:

我输入了8张jpg图片,可以在这里下载

压缩后生成的out.mp4文件格式为:

播放时间正好为40秒 = 8张图片 × 5秒/张

帧速率为: 30帧/秒

然后我又播放了一下这个out.mp4,测验的结果为:

[javascript] view plain copy

 print?在CODE上查看代码片派生到我的代码片

  1. Index: 1, packet’s size: 176396  
  2. Index: 2, packet’s size: 556  
  3. Index: 3, packet’s size: 115  
  4. Index: 4, packet’s size: 107  
  5. Index: 5, packet’s size: 116  
  6. //…  
  7. Index: 1198, packet’s size: 31  
  8. Index: 1199, packet’s size: 31  
  9. Index: 1200, packet’s size: 39  

说明一共有1200个Frame。计算一下,30帧/秒 × 40秒 = 1200帧,验证了。

参考:https://trac.ffmpeg.org/wiki/Create%20a%20video%20slideshow%20from%20images


2. 使用ffplay

详细使用说明(英文):http://ffmpeg.org/ffplay.html




3. 使用ffprobe

简介:用于查看文件格式的应用程序。

详细使用说明(英文):http://ffmpeg.org/ffprobe.html



先测试一个aac格式的音频文件(wavinflag.aac),结果如下:

[cpp] view plain copy

 print?在CODE上查看代码片派生到我的代码片

  1. hwh@Mountain:~/ffmpeg/ffmpeg-2.1.4/doc/examples$ ffprobe wavinflag.aac  
  2. ffprobe version 2.1.4 Copyright (c) 2007-2014 the FFmpeg developers  
  3.   built on Mar  7 2014 15:39:45 with gcc 4.7 (Ubuntu/Linaro 4.7.2-2ubuntu1)  
  4.   configuration:  
  5.   libavutil      52. 48.101 / 52. 48.101  
  6.   libavcodec     55. 39.101 / 55. 39.101  
  7.   libavformat    55. 19.104 / 55. 19.104  
  8.   libavdevice    55.  5.100 / 55.  5.100  
  9.   libavfilter     3. 90.100 /  3. 90.100  
  10.   libswscale      2.  5.101 /  2.  5.101  
  11.   libswresample   0. 17.104 /  0. 17.104  
  12. [aac @ 0xa55c820] Estimating duration from bitrate, this may be inaccurate  
  13. Input #0, aac, from ‘wavinflag.aac’:  
  14.   Duration: 00:03:51.27, bitrate: 122 kb/s  
  15.     Stream #0:0: Audio: aac, 44100 Hz, stereo, fltp, 122 kb/s  







aac:Advanced Audio Coding。一种专为声音数据设计的文件压缩格式,与Mp3不同,它采用了全新的算法进行编码,更加高效,具有更高的“性价比”。利用AAC格式,可使人感觉声音质量没有明显降低

的前提下,更加小巧。;

44100 Hz:表示一秒钟采样44100次即采样精度,常见的还有22050 Hz等。

stereo:表示立体声(就是有2个声道);

fltp:AV_SAMPLE_FMT_FLTP格式的数据( float, 4bit , planar);

122 kb/s:222kbps就是每秒钟有122k的信息量。码率越高,文件所含的信息量就越大,音质就越高。

播放时间(00:03:51.27):我们可以用“总播放时间=文件大小*8/比特率”粗略计算播放时间。

该文件大小为:3535748B

比特率为:122kb/s,即数据传输时单位时间传送的数据位数,一般我们用的单位是kbps即千位每秒。

那么播放时间 = (3235748 * 8)/122000 ~ 231秒。





再测试一个视频文件(start.avi),结果如下:

[cpp] view plain copy

 print?在CODE上查看代码片派生到我的代码片

  1. hwh@Mountain:~/ffmpeg/ffmpeg-2.1.4/doc/examples$ ffprobe start.avi  
  2. ffprobe version 2.1.4 Copyright (c) 2007-2014 the FFmpeg developers  
  3.   built on Mar  7 2014 15:39:45 with gcc 4.7 (Ubuntu/Linaro 4.7.2-2ubuntu1)  
  4.   configuration:  
  5.   libavutil      52. 48.101 / 52. 48.101  
  6.   libavcodec     55. 39.101 / 55. 39.101  
  7.   libavformat    55. 19.104 / 55. 19.104  
  8.   libavdevice    55.  5.100 / 55.  5.100  
  9.   libavfilter     3. 90.100 /  3. 90.100  
  10.   libswscale      2.  5.101 /  2.  5.101  
  11.   libswresample   0. 17.104 /  0. 17.104  
  12. [avi @ 0x9877820] non-interleaved AVI  
  13. Input #0, avi, from ‘start.avi’:  
  14.   Duration: 00:00:10.55, start: 0.000000, bitrate: 1838 kb/s  
  15.     Stream #0:0: Video: msvideo1 (CRAM / 0x4D415243), rgb555le, 352×288, 20 tbr, 20 tbn, 20 tbc  
  16.     Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, stereo, s16p, 320 kb/s  







这里有2个流,视频流和音频流,音频流我们就略过了,直接看视频流。

msvideo1:表示微软是视频格式。

rgb555le:表示颜色标准,常见的还有yuv420p等。

352×288:视频尺寸。

tbr, tbn, tbc:25 tbr代表帧率;1200k tbn代表文件层(st)的时间精度,即1S=1200k,和duration相关;50 tbc代表视频层(st->codec)的时间精度,即1S=50,和strem->duration和时间戳相关

ffmpeg对mp4视频进行TS切片及m3u8索引文件支持hls

要想利用HLS来实现视频的在线播放,就得需要将一个完整的视频文件切割成多个ts视频流,然后利用m3u8的索引文件来播放。基本是利用开源的ffmpeg对mp4视频进行TS切片及建立m3u8索引文件支持hls,提升播放速度。

1.ffmpeg切片命令,以H264和AAC的形式对视频进行输出

ffmpeg -i input.mp4 -c:v libx264 -c:a aac -strict -2 -f hls output.m3u8

2.ffmpeg转化成HLS时附带的指令 

-hls_time n: 设置每片的长度,默认值为2。单位为秒

-hls_list_size n:设置播放列表保存的最多条目,设置为0会保存有所片信息,默认值为5

-hls_wrap n:设置多少片之后开始覆盖,如果设置为0则不会覆盖,默认值为0.这个选项能够避免在磁盘上存储过多的片,而且能够限制写入磁盘的最多的片的数量

-hls_start_number n:设置播放列表中sequence number的值为number,默认值为0

3.对ffmpeg切片指令的使用

ffmpeg -i output.mp4 -c:v libx264 -c:a aac -strict -2 -f hls -hls_list_size 0 -hls_time 5 output1.m3u8 

将输出的 M3u8 可直接使用vlc打开,发现拖动的时候会出现画面丢失的现象,待解决。

ffmpeg  -i output.mp4 -vcodec h264 -vb 1000k -profile baseline -acodec aac -ac 1 -ar 44100 -ab 24 -hls_list_size 0 -hls_time 10 -f hls output.mp4.m3u8

ffmpeg处理RTMP流媒体的命令大全

 ffmpeg处理RTMP流媒体的命令大全,将文件当做直播送至live,将直播媒体保存至本地文件,将其中一个直播流,视频改用h264压缩,音频不变,送至另外一个直播服务流,将其中一个直播流,视频改用h264压缩,音频改用faac压缩,送至另外一个直播服务流,将一个高清流,复制为几个不同视频清晰度的流重新发布,其中音频不变,功能一样,只是采用-x264opts选项….

1、将文件当做直播送至live

[plain] view plain copy

  1. ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/live/streamName  

2、将直播媒体保存至本地文件

[plain] view plain copy

  1. ffmpeg -i rtmp://server/live/streamName -c copy dump.flv  

3、将其中一个直播流,视频改用h264压缩,音频不变,送至另外一个直播服务流

[plain] view plain copy

  1. ffmpeg -i rtmp://server/live/originalStream -c:a copy -c:v libx264 -vpre slow -f flv rtmp://server/live/h264Stream  

4、将其中一个直播流,视频改用h264压缩,音频改用faac压缩,送至另外一个直播服务流

[plain] view plain copy

  1. ffmpeg -i rtmp://server/live/originalStream -c:a libfaac -ar 44100 -ab 48k -c:v libx264 -vpre slow -vpre baseline -f flv rtmp://server/live/h264Stream  

5、将其中一个直播流,视频不变,音频改用faac压缩,送至另外一个直播服务流

[plain] view plain copy

  1. ffmpeg -i rtmp://server/live/originalStream -acodec libfaac -ar 44100 -ab 48k -vcodec copy -f flv rtmp://server/live/h264_AAC_Stream  

6、将一个高清流,复制为几个不同视频清晰度的流重新发布,其中音频不变

[plain] view plain copy

  1. ffmpeg -re -i rtmp://server/live/high_FMLE_stream -acodec copy -vcodec x264lib -s 640×360 -b 500k -vpre medium -vpre baseline rtmp://server/live/baseline_500k -acodec copy -vcodec x264lib -s 480×272 -b 300k -vpre medium -vpre baseline rtmp://server/live/baseline_300k -acodec copy -vcodec x264lib -s 320×200 -b 150k -vpre medium -vpre baseline rtmp://server/live/baseline_150k -acodec libfaac -vn -ab 48k rtmp://server/live/audio_only_AAC_48k  

7、功能一样,只是采用-x264opts选项

[plain] view plain copy

  1. ffmpeg -re -i rtmp://server/live/high_FMLE_stream -c:a copy -c:v x264lib -s 640×360 -x264opts bitrate=500:profile=baseline:preset=slow rtmp://server/live/baseline_500k -c:a copy -c:v x264lib -s 480×272 -x264opts bitrate=300:profile=baseline:preset=slow rtmp://server/live/baseline_300k -c:a copy -c:v x264lib -s 320×200 -x264opts bitrate=150:profile=baseline:preset=slow rtmp://server/live/baseline_150k -c:a libfaac -vn -b:a 48k rtmp://server/live/audio_only_AAC_48k  

8、将当前摄像头及音频通过DSSHOW采集,视频h264、音频faac压缩后发布

[plain] view plain copy

  1. ffmpeg -r 25 -f dshow -s 640×480 -i video=”video source name”:audio=”audio source name” -vcodec libx264 -b 600k -vpre slow -acodec libfaac -ab 128k -f flv rtmp://server/application/stream_name  

9、将一个JPG图片经过h264压缩循环输出为mp4视频

[plain] view plain copy

  1. ffmpeg.exe -i INPUT.jpg -an -vcodec libx264 -coder 1 -flags +loop -cmp +chroma -subq 10 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -flags2 +dct8x8 -trellis 2 -partitions +parti8x8+parti4x4 -crf 24 -threads 0 -r 25 -g 25 -y OUTPUT.mp4  

10、将普通流视频改用h264压缩,音频不变,送至高清流服务(新版本FMS live=1)

[plain] view plain copy

  1. ffmpeg -i rtmp://server/live/originalStream -c:a copy -c:v libx264 -vpre slow -f flv “rtmp://server/live/h264Stream live=1″ 

利用ffmpeg将MP4文件切成ts和m3u8(苹果官方推荐ffmpeg脚本)

1、将MP4转成m3u8

ffmpeg -i test.mp4 -codec copy -bsf h264_mp4toannexb test.ts

2、将ts转成m3u8

网上很多垃圾文章推荐segmenter工具,但用的时候,3.5G的ts文件丢了一半的数据,于是想到了ffmpeg转。

在国外网站找到命令,一句话搞定,没报半句错:

ffmpeg -i 12生肖.ts -c copy -map 0 -f segment -segment_list playlist.m3u8 -segment_time 10 output%03d.ts

顺便共享给各位国内的同仁,免得深受其苦。毕竟,大家都说HLS代表future,rtsp已经是过去式了。

苹果官方推荐ffmpeg脚本

#!/bin/sh

BR=800k

ffmpeg -i $1 -f mpegts -acodec libmp3lame -ar 48000 -ab 64k -s 320×240 -vcodec libx264 -b $BR -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate $BR -bufsize $BR-rc_eq ‘blurCplx^(1-qComp)’ -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30-async 2 sample_$BR_pre.ts

segmenter sample_$BR_pre.ts 10 sample_$BR stream-$BR.m3u8 http://www.ioncannon.net/

rm -f sample_$BR_pre.ts