FFmpeg 常用命令

2020年1月5日 · 513 字 · 2 分钟

ffplay 播放 mp4/mp3 和 歌词

## for mp4 file
ffplay -f lavfi "color,subtitles=xxxx.lrc[out0]; amovie=xxxx.mp4[out1]"

## for mp3 file
ffplay -vf subtitles=filename=xxxx.lrc xxxx.mp3

合并两张图片成左右排列

## 左右排列
ffmpeg -i a.jpg -i b.jpg -filter_complex hstack output.jpg

## 上下排列
ffmpeg -i a.jpg -i b.jpg -filter_complex vstack output.jpg

rtmp 推流

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

ffmpeg -re -i [localFile.mp4] -c:v libx264 -c:a aac -f flv [rtmp://server/live/streamName]

播放本地/网络文件

ffplay [url]

下载合并 .m3u8 指向的视频文件

ffmpeg -i [http://.../playlist.m3u8] -c copy [-bsf:a aac_adtstoasc] [output.mp4]

解析 h264 文件/ 解析 [NALUS]

ffmpeg -i [abc.h264] -c copy -bsf:v trace_headers -f null - [&> NALUS.txt]

#    Packet: 2055 bytes, key frame, no pts, dts -48000, duration 48000.
#    Sequence Parameter Set
#    0           forbidden_zero_bit                                          0 = 0
#    1           nal_ref_idc                                                11 = 3
#    3           nal_unit_type                                           00111 = 7
#    8           profile_idc                                          01100100 = 100
#    16          constraint_set0_flag                                        0 = 0
#    17          constraint_set1_flag                                        0 = 0
#    18          constraint_set2_flag                                        0 = 0
#    19          constraint_set3_flag                                        0 = 0
#    20          constraint_set4_flag                                        1 = 1
#    21          constraint_set5_flag                                        0 = 0
#    22          reserved_zero_2bits                                        00 = 0
#    24          level_idc                                            00001101 = 13

Retaining Quality 保留媒体质量转码

ffmpeg -i input.mp4 -sameq output.mkv

Extracting Clips 裁剪

# start recording 10 minutes and 30 seconds into the input file
# and run for 5 minutes and 24 seconds. It'll then copy that clip to a new file
ffmpeg -i input.mkv -ss 00:10:30 -t 00:05:24 -c clip.mkv

Formatting Video 改变分辨率

ffmpeg -i input.mkv -aspect 16:9 -s 1920x1080 output.mkv

ffmpeg -i input.mkv -cropbottom 200 output.mkv

Change Codec 转码

ffmpeg -i INPUT -map 0 -c:v libx264 -c:a copy OUTPUT

从 alsa 录制

## cat /proc/asound/cards
## cat /proc/asound/devices
ffmpeg -f alsa -i hw:0 alsaout.wav

Screen Capture 屏幕录制

ffmpeg -f x11grab -framerate 25 -video_size 1680x1050 -i :0.0+1680 -c:v libx264 out.mp4

音视频合并

ffmpeg -i video.mp4 -i audio.mp3 -c copy -map 0:v:0 -map 1:a:0 out.mp4

媒体文件拼接 concat

### audio.txt
## file 'audio.s1.mp3'
## file 'audio.s2.mp3'
## file 'audio.s3.mp3'

ffmpeg -f concat -i audio.txt out.mp3

影视频变速

## 音频变速 atempo 0.5~2.0, [ 0.5 倍速 ] [ 2.0 倍速 ]
## atempo 大小确定 【desired length / Audio length = atempo】
ffmpeg -y -i src.mp4 -codec:a libmp3lame -filter:a "atempo=0.624" -b:a 320K out.mp3
ffmpeg -y -i src.mp4 -codec:a libmp3lame -filter:a "atempo=2.0,atempo=2.0" src-4x.mp3 ## [ 4 倍速 ]

## 视频变速,修改 pts 
ffmpeg -i 10s.mp4 -filter:v "setpts=0.5*PTS" out.mp4 [ 1/0.5 倍速 ]
ffmpeg -i 10s.mp4 -filter:v "setpts=2.0*PTS" out.mp4 [ 1/2.0 倍速 ]

## 音视频变速
ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mkv