FFmpeg

Linux下安装

1
2
3
4
5
6
yum install epel-release
rpm -v --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
yum install ffmpeg ffmpeg-devel
ffmpeg -version
ffmpeg -h

RPM 安装

1
2
3
yum list installed  | grep ffmpeg
yum remove ffmpeg.x86_64
yum -y localinstall ffmpeg-3.2.4-1.el7.centos.x86_64.rpm

Static Builds

参考

格式转换

相关参数

  • -c:v libx264 视频编码
  • -c:a aac 音频编码
  • -r Set frame rate
  • -aq Set the audio quality (codec-specific, VBR). This is an alias for -q:a.

wmv to mp4

1
2
ffmpeg -i input.wmv output.mp4
ffmpeg -i input.wmv -c:v libx264 -c:a aac output.mp4

批量转换

1
2
3
4
5
wmv转为mp4
for i in *.wmv; do ffmpeg -i "$i" "${i%.*}.mp4"; done

mp4视频转为mp3音频
for i in *.mp4; do ffmpeg -i "$i" -acodec mp3 "${i%.*}.mp3"; done

比例缩放

1
2
ffmpeg -i 1.mp4 -vf scale=-1:360 2.mp4
ffmpeg -i 1.mp4 -vf scale=850:480 2.mp4

调整fps和比例

1
ffmpeg -i 1.mp4 -filter:v fps=24,scale=-1:360 output/1.mp4

调整比特率和比例

1
ffmpeg -i 1.mp4 -b:v 500K -vf scale=-1:360 output/1.mp4

参考

Stream selection

specific video and audio track

1
2
3
4
5
6
7
8
// codec option has been set to copy
ffmpeg -i 01.mp4 -map 0:v:0 -map 0:a:0 -c:v copy -c:a copy output/01.mp4

// all included streams will be transcoded. The encoders chosen will be the default ones registered by each output format
ffmpeg -i 01.mp4 -map 0:v:0 -map 0:a:0 output/01.mp4

// encoders specifically
ffmpeg -i 01.mp4 -map 0:v:0 -map 0:a:0 -c:v libx264 -c:a ac3 output/01.mp4

截取段

指定时间段截取

1
ffmpeg -i 1.mp4 -ss 00:00:30.000 -t 00:01:01.001 -vcodec copy output/1.mp4

合并 (concat)

  • n Set the number of segments. Default is 2.
  • v Set the number of output video streams, that is also the number of video streams in each segment. Default is 1.
  • a Set the number of output audio streams, that is also the number of audio streams in each segment. Default is 0.
1
2
3
ffmpeg -i part1.mp4 -i part2.mp4 -i part3.mp4 -i part4.mp4 \
-filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] [3:v] [3:a] \
concat=n=4:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" full.mp4

h.264 - How to concatenate two MP4 files using FFmpeg? - Stack Overflow

排列(vstack & hstack)

1
2
3
4
ffmpeg -i left.mp4 -i right.mp4 -filter_complex \
"[1:v]tpad=start_duration=32.366:start_mode=clone[v1]; \
[0:v:0][v1]vstack=inputs=2,format=yuv420p[v];[0:a][1:a]concat=n=2:v=0:a=1[a]" \
-map "[v]" -map "[a]" -c:v libx264 -tune film -crf 16 -b:a 256k -movflags +faststart output.mp4

其他

查看信息 ffprobe

1
ffprobe -i 1.mp4 -v quiet -print_format json -show_format -show_streams

h.264 profile level

-profile:v high high, main, or baseline
-level:v 3.1
-refs 3

1
ffmpeg -i 1.mp4 -b:v 500K -profile:v high -level:v 3.1 -refs 3 2.mp4

-brand mp42

References