之前的文章把ffmpeg的命令行大致的options都列了一遍 现在就来测试一遍
在测试之前 我们需要知道利用ffmpeg转码的时候 具体的流程是什么样的
关于过滤器的使用 简单过滤其实可以看做是在解码和编码之间插入的一个步骤
音视频的过滤器的使用 是通过-vf -af来使用的(之后看例子吧)
对于复杂的过滤其实可以看做下面这样
而复杂的过滤使用方法也是不一样的 他其实可以算是一个全局选项 官网上说的那个 -filter_complex
我是没有找到 只看到了设置线程数量的-filter_complex_threads 选项 以及还有一个 -filter(之后测试下吧)比较好理解的例子就是overlay的这个filter(有两个视频 一个音频)
我们来按照官网的要求准备下视频文件(他要三个 我只有一个 那就自己转 就是没有字幕)
第一个 >ffmpeg -i cyq.avi -s 640x360 -ac 2 a.avi
第二个 >ffmpeg -i cyq.avi -s 1920x1080 -ac 5 b.mp4
第三个 >ffmpeg -i cyq.avi -s 1280x720 -ac 2 c.mkv
在准备好素材以后 就来执行官网上面的例子:
ffmpeg -i A.avi -i B.mp4 out1.mkv out2.wav -map 1:a -c:a copy out3.mov
单单看一下 就知道有两个输入 三个输出
在没有指定具体哪个流的时候 ffmpeg会自动的帮你选择最好的流映射到输出文件
也就是out1.mkv (mkv支持音视频以及字幕 ffmpeg会自动选择的)视频会选择B.mp4的stream0 音频会选择B.mp4的音频流 字幕的话 没有就不选择了
out2.wav 只会选B.mp4的音频流
对于第三个out3.mov 不会自动选了 而是按照 -map 1:a 来选(第二个视频的音频流 ——0是第一个输入)
后边的-c:a copy的意思就是指定编码 直接copy就好
因为我们没有字幕 我就把有关字幕的选项以及测试省略了
ffmpeg -i A.avi -i C.mkv -i B.mp4 -filter_complex "overlay" out1.mp4 out2.srt
这个需要改一下(srt格式 是只支持text格式的字幕 我这边就删了 官网为了说明选流才把b.mp4放上去的)
ffmpeg -i a.avi -i c.mkv -filter_complex "overlay" out_over.mp4
这样我们就可以来测试一下filter 结果没有看到我想象中的overlay的样子
我就试了下
ffmpeg -i a.avi -i c.mkv -filter "overlay" out_over.mp4
结果就执行不了
Simple filtergraph 'overlay' was expected to have exactly 1 input and 1 output. However, it had >1 input(s) and 1 output(s). Please adjust, or use a complex filtergraph (-filter_complex) instead.
叫我改用-filter_complex(我的help里面没有显示啊)
关于-filter_complex的使用 (标签一类的使用)我们就需要注意下官网的说明了
Example: labeled filtergraph outputs
ffmpeg -i A.avi -i B.mp4 -i C.mkv -filter_complex "[1:v]hue=s=0[outv];overlay;aresample" \
-map '[outv]' -an out1.mp4 \
out2.mkv \
-map '[outv]' -map 1:a:0 out3.mkv
The above command will fail, as the output pad labelled [outv] has been mapped twice. None of the output files shall be processed.
ffmpeg -i A.avi -i B.mp4 -i C.mkv -filter_complex "[1:v]hue=s=0[outv];overlay;aresample" \
-an out1.mp4 \
out2.mkv \
-map 1:a:0 out3.mkv
This command above will also fail as the hue filter output has a label, [outv], and hasn’t been mapped anywhere.
The command should be modified as follows,
ffmpeg -i A.avi -i B.mp4 -i C.mkv -filter_complex "[1:v]hue=s=0,split=2[outv1][outv2];overlay;aresample" \
-map '[outv1]' -an out1.mp4 \
out2.mkv \
-map '[outv2]' -map 1:a:0 out3.mkv
The video stream from B.mp4 is sent to the hue filter, whose output is cloned once using the split filter, and both outputs labelled. Then a copy each is mapped to the first and third output files.
The overlay filter, requiring two video inputs, uses the first two unused video streams. Those are the streams from A.avi and C.mkv. The overlay output isn’t labelled, so it is sent to the first output file out1.mp4, regardless of the presence of the -map option.
The aresample filter is sent the first unused audio stream, that of A.avi. Since this filter output is also unlabelled, it too is mapped to the first output file. The presence of -an only suppresses automatic or manual stream selection of audio streams, not outputs sent from filtergraphs. Both these mapped streams shall be ordered before the mapped stream in out1.mp4.
The video, audio and subtitle streams mapped to out2.mkv are entirely determined by automatic stream selection.
out3.mkv consists of the cloned video output from the hue filter and the first audio stream from B.mp4.
ffmpeg -i INPUT -map 0 -c:v libx264 -c:a copy OUTPUT
用libx 264对所有视频流进行编码,并复制所有音频流。
ffmpeg -i INPUT -map 0 -c copy -c:v:1 libx264 -c:a:137 libvorbis OUTPUT
将复制所有的流,除了第二个视频,它将用libx 264编码,和138音频,它将被编码的libvorbis。
看了这两个之后 我们就可以发现一个问题 那就是流的指定说明 类似于什么 X:X 出现字母那就是表示音频 视频 字幕一类的 出现数字那就是具体指的是哪个文件 哪个文件的哪个流(从0开始计数的)
ffmpeg -i in.avi -metadata title="my title" out.flv
用于设置输出文件中的标题。
ffmpeg -i INPUT -metadata:s:a:0 language=eng OUTPUT
设置第一个音频流的语言
ffmpeg -i in.mp4 -i IMAGE -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic out.mp4
添加嵌入的封面/缩略图,并不是所有的muxer都支持嵌入式缩略图,而那些支持嵌入缩略图的用户只支持几种格式,比如jpeg或png。
ffmpeg -i cyq.avi -b:v 64K test.avi
目的是为了设置输出文件的视频比特率为64kbit/s 如果是设置音频那就是-b:a +数值
如果直接就是-b 那意思就是音视频的总和了?其实不是的 我们看之前的命令行解释 可以发现他也是设置视频的比特率 音频有一个是-ab 当然 最好不要这么用 他会提示你这种用法是ambiguous(含糊的)
ffmpeg -i cyq.avi -r 30 test1.avi
这个是为了设置视频的帧率
这里需要注意下ffmpeg的几个高级选项 一个是之前说的-filter_complex 一个是-map
********************************************************************************************************
-filter_complex filtergraph (global)
Define a complex filtergraph, i.e. one with arbitrary number of inputs and/or outputs. For simple graphs – those with one input and one output of the same type – see the -filter options. filtergraph is a description of the filtergraph, as described in the “Filtergraph syntax” section of the ffmpeg-filters manual.
Input link labels must refer to input streams using the [file_index:stream_specifier] syntax (i.e. the same as -map uses). If stream_specifier matches multiple streams, the first one will be used. An unlabeled input will be connected to the first unused input stream of the matching type.
Output link labels are referred to with -map. Unlabeled outputs are added to the first output file.
Note that with this option it is possible to use only lavfi sources without normal input files.
For example, to overlay an image over video
ffmpeg -i video.mkv -i image.png -filter_complex '[0:v][1:v]overlay[out]' -map
'[out]' out.mkv
Here [0:v] refers to the first video stream in the first input file, which is linked to the first (main) input of the overlay filter. Similarly the first video stream in the second input is linked to the second (overlay) input of overlay.
Assuming there is only one video stream in each input file, we can omit input labels, so the above is equivalent to
ffmpeg -i video.mkv -i image.png -filter_complex 'overlay[out]' -map
'[out]' out.mkv
Furthermore we can omit the output label and the single output from the filter graph will be added to the output file automatically, so we can simply write
ffmpeg -i video.mkv -i image.png -filter_complex 'overlay' out.mkv
To generate 5 seconds of pure red video using lavfi color source:
ffmpeg -filter_complex 'color=c=red' -t 5 out.mkv
********************************************************************************************************
-map [-]input_file_id[:stream_specifier][?][,sync_file_id[:stream_specifier]] | [linklabel] (output)
Designate one or more input streams as a source for the output file. Each input stream is identified by the input file index input_file_id and the input stream index input_stream_id within the input file. Both indices start at 0. If specified,sync_file_id:stream_specifier sets which input stream is used as a presentation sync reference.
The first -map option on the command line specifies the source for output stream 0, the second -map option specifies the source for output stream 1, etc.
A - character before the stream identifier creates a "negative" mapping. It disables matching streams from already created mappings.
A trailing ? after the stream index will allow the map to be optional: if the map matches no streams the map will be ignored instead of failing. Note the map will still fail if an invalid input file index is used; such as if the map refers to a non-existent input.
An alternative [linklabel] form will map outputs from complex filter graphs (see the -filter_complex option) to the output file. linklabel must correspond to a defined output link label in the graph.
For example, to map ALL streams from the first input file to output
ffmpeg -i INPUT -map 0 output
For example, if you have two audio streams in the first input file, these streams are identified by "0:0" and "0:1". You can use -map to select which streams to place in an output file. For example:
ffmpeg -i INPUT -map 0:1 out.wav
will map the input stream in INPUT identified by "0:1" to the (single) output stream in out.wav.
For example, to select the stream with index 2 from input file a.mov (specified by the identifier "0:2"), and stream with index 6 from input b.mov (specified by the identifier "1:6"), and copy them to the output file out.mov:
ffmpeg -i a.mov -i b.mov -c copy -map 0:2 -map 1:6 out.mov
To select all video and the third audio stream from an input file:
ffmpeg -i INPUT -map 0:v -map 0:a:2 OUTPUT
To map all the streams except the second audio, use negative mappings
ffmpeg -i INPUT -map 0 -map -0:a:1 OUTPUT
To map the video and audio streams from the first input, and using the trailing ?, ignore the audio mapping if no audio streams exist in the first input:
ffmpeg -i INPUT -map 0:v -map 0:a? OUTPUT
To pick the English audio stream:
ffmpeg -i INPUT -map 0:m:language:eng OUTPUT
Note that using this option disables the default mappings for this output file.
音视频的捕获 其实也就是通过设备获取数据
ffmpeg -f oss -i /dev/dsp -f video4linux2 -i /dev/video0 /tmp/out.mpg
ffmpeg -f alsa -ac 1 -i hw:1 -f video4linux2 -i /dev/video0 /tmp/out.mpg
-f 制定了设备种类 -i 指定了特定的设备
音视频格式转化
这个比较简单 关键就是多写几条 熟悉各个选项的意思