ffmpeg的常用选项

map选项

默认情况下只在输入文件中的每种流里选择第一个流。

需要指定放入输出文件的流就需要使用-map选项。

用法举例:

-map 0
From input index #0 (the 1st input) select all streams.

-map 1:a
From input index #1 (the 2nd input) select all audio streams.

-map 3:s:4
From input index #3 (the 4th input) select subtitle stream index #4 (the fifth subtitle stream)。

-map 0 -map -0:s
Will select all streams from input index #0 (the 1st input) except subtitles.

改变视频帧率

有两种方法可以改变输出文件的视频流帧率:

  1. 使用 -r 选项
  2. 使用 fps滤镜。
1
2
ffmpeg -i [inputfile] -r 30 [output]
ffmpeg -i [inputfiel] -filter:v fps=30 [output]

有两种方法可以改变输出文件的视频流帧率:

改变默认音轨

使用ffmpeg修改默认的音频轨道,先取消第二音轨的默认值,在设置为第一音轨。

1
ffmpeg -i input.mkv -map 0:0 -map 0:1 -map 0:2  -c copy -disposition:a:1 0 -disposition:a:0 default -y output.mp4

改变音频采样率

-ar 48k 44.1k 96k

1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k output.flac

改变音频通道数量

-ac

1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k -ac:0 2 -filter:0 aformat=channel_layouts=stereo output.flac
1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k -ac:0 6 -filter:0 aformat=channel_layouts=5.1 output.flac

改变音频位深 bit depth

-sample_fmt

1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k -sample_fmt s24 -ac:0 6 -filter:0 aformat=channel_layouts=5.1 output.flac

生成空白音频

可以使用anullsrc来生成静音音频:

1
ffmpeg -f lavfi -i anullsrc=r=48000:cl=stereo -t 0.875 /Users/redtux/Movies/converted/silent-audio.aac
  • sample_rate, r
    指定采样率,默认44100。

  • channel_layout, cl
    指定通道布局,详见 libavutil/channel_layout.cchannel_layout_map 定义,常见mono stereo 5.1

  • -t
    文件时长,单位事秒,不指定就一直生成。

截取视频片段

1
ffmpeg  -i 源文件名 -vcodec copy -acodec copy -ss 00:00:10 -to 00:00:15 目标文件名 -y

裁剪视频文件的多个部分,并将其合并成一个新的视频

调整播放速度

调整视频文件中视频速度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//2倍速播放
ffmpeg -i test.mp4 -an -filter:v "setpts=0.5*PTS" out_test.mp4

-i test.mp4是输入文件名

-an 将音频禁掉 (可以不加)

-filter:v 对视频进行处理

"setpts=0.5PTS" 设置时间戳参数PTS为原先的一半,可接受调整范围为[0.25,4]

out_test.mp4 输出视频文件

还可以在命令中加上指定fps(-r 60),使得不会丢帧

调整视频文件中音频速度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ffmpeg -i test.mp4 -filter:a "atempo=2.0" -vn out_test.mp4
-i 后满test.mp4是输入文件名

-filter:a 对音频进行处理

"atempo=2.0" 设置播放速度是原来的2倍 , 倍率调整范围[0.5, 2.0]

-vn 将视频禁掉 (可以不加)

out_test.mp4 输出视频文件

需要调整到4倍可以采取以下方法:

ffmpeg -i test.mp4 -filter:a "atempo=2.0,atempo=2.0" -vn out_test.mp4

同时调整视频文件的视频、音频:

1
ffmpeg -i test.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]"  out_test.mp4

如果不想丢帧就同时设置 -r ,把帧率设置为原帧率的2倍。

metadata的使用

1
ffmpeg -i /Users/redtux/Movies/Becoming.Jane.2007.1080p.BluRay.x265.2Audio.mp4  -metadata title="Becoming.Jane.2007.1080p.BluRay.x265.2Audio-RARBG" -map 0:0 -c:0 copy -metadata:s:0 title='Becoming.Jane.2007.1080p.BluRay.x265-RARBG' -metadata:s:0 handler='Becoming.Jane.2007.1080p.BluRay.x265-RARBG' -map 0:1 -c:1 copy -metadata:s:1 title="国语配音" -metadata:s:1 handler="国语配音" -metadata:s:1 language="chi"  -disposition:1 default -map 0:2 -c:2 copy -metadata:s:2 title="English" -metadata:s:2 handler="English" -metadata:s:2 language="eng"  -disposition:2  0 /Users/redtux/Movies/Becoming.Jane.2007.1080p.BluRay.x265.2Audio-RARBG.mp4
作者

redtux

发布于

2023-08-06

更新于

2023-11-22

许可协议

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

评论

You forgot to set the shortname for Disqus. Please set it in _config.yml.