乐趣区

Linux-Shell-FFmpeg

常用编码

ffmpeg -i input.mov -s 800x450 -b 1M -vcodec h264 -acodec aac output.mp4

裁切画面

# width: 宽度
# height: 高度
# left: 左侧边距
# top: 顶部边距
ffmpeg -i input.mp4 -vf crop=[width]:[height]:[left]:[top] -b 1M -vcodec h264 output.mp4

# 保留左下角四分之一画面
# iw: input width
# ih: input height
#  -----------
# |     |     |
# |-----+-----|
# |/////|     |
#  -----------
ffmpeg -i input.mp4 -vf crop=iw/2:ih/2:0:ih/2 -b 1M -vcodec h264 output.mp4

MP4 转 MOV

ffmpeg -i input.mp4 -acodec copy -vcodec copy -f mov output.mov

图片转视频

# mspf: millisecond per frame
# %06d.jpg: 000001.jpg, 000002.jpg, ...
# vf: 宽高为奇数的图片转换成宽高为偶数的视频,并使用黑色填充空缺部分
ffmpeg -r 1000/$mspf -f image2 -i "%06d.jpg" -vcodec libx264 -pix_fmt yuv420p -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2:color=black" -an "$videofile"

截图

ffmpeg -i input.mp4 -ss 00:00:00.000 -vframes 1 screenshot.png

旋转

# 0 = 90CounterCLockwise and Vertical Flip (default)
# 1 = 90Clockwise
# 2 = 90CounterClockwise
# 3 = 90Clockwise and Vertical Flip
ffmpeg -i input.mp4 -vf "transpose=1" output.mp4

生成缩略图

# 每秒一张
ffmpeg -i input.mp4 -vf fps=1 screenshot_%d.png

# 每分钟一张
ffmpeg -i input.mp4 -vf fps=1/60 screenshot_%d.png

# 每十分钟一张
ffmpeg -i input.mp4 -vf fps=1/600 screenshot_%d.png

去水印

1、用 show 参数在视频上显示绿色框预估水印位置

ffmpeg -i eagles.mpg -vf delogo=x=700:y=0:w=100:h=50:show=1 nologo.mpg

2、确定水印位置后,去水印

ffmpeg -i eagles.mpg -vf delogo=x=730:y=0:w=70:h=46 nologo.mpg

连接视频和音频

1、不转码

# -shortest,输出文件的时长 = 所有输入文件中最短的时长
# 否则,输出文件的时长 = 所有输入文件中最长的时长
ffmpeg -i video.mp4 -i audio.m4a -c copy -shortest output.mp4

2、转码

ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -strict experimental output.mp4

3、替换已有音频

ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 output.mp4

连接多个音频

1、新建文件 list.txt,内容如下

file '1.mp3'
file '2.mp3'
file '3.mp3'
file '4.mp3'
file '5.mp3'

2、连接

ffmpeg -f concat -i list.txt -c copy output.mp3

反色

如果需要支持 macOS 的 QuickTime 播放,加上“-pix_fmt yuv420p”参数。

ffmpeg -i input.mp4 -vf lutrgb="r=negval:g=negval:b=negval" -pix_fmt yuv420p -c:a copy output.mp4

修改视频播放速度

PTS(Presentation Time Stamp,显示时间戳)

# 加速
ffmpeg -i input.mp4 -vf setpts=PTS/8 output.mp4
# 减速
ffmpeg -i input.mp4 -vf setpts=PTS/(1/4) output.mp4

修改音频播放速度

由于 atempo 参数取值范围在 0.5~2.0

需通过组合使用来实现 8 倍速或 0.25 倍速播放

# 加速
ffmpeg -i input.mp4 -af atempo=2.0,atempo=2.0,atempo=2.0,atempo=2.0 output.mp4
# 减速
ffmpeg -i input.mp4 -af atempo=0.5,atempo=0.5 output.mp4

同时修改视频和音频播放速度

# 加速
ffmpeg -i input.mp4 -vf setpts=PTS/8 -af atempo=2.0,atempo=2.0,atempo=2.0,atempo=2.0 output.mp4
# 减速
ffmpeg -i input.mp4 -vf setpts=PTS/(1/4) -af atempo=0.5,atempo=0.5 output.mp4

Extract Audio from Video

ffmpeg -i input.mp4 -vn -acodec copy output-audio.aac

Extract Video from Audio

ffmpeg -i input.mp4 -an -vcodec copy output-video.mp4

Speeding up/slowing down video

You can change the speed of a video stream using the ​setpts video filter. Note that in the following examples, the audio stream is not changed, so it should ideally be disabled with -an.

To double the speed of the video, you can use:

ffmpeg -i input.mkv -filter:v "setpts=0.5*PTS" output.mkv

The filter works by changing the presentation timestamp (PTS) of each video frame. For example, if there are two succesive frames shown at timestamps 1 and 2, and you want to speed up the video, those timestamps need to become 0.5 and 1, respectively. Thus, we have to multiply them by 0.5.

Note that this method will drop frames to achieve the desired speed. You can avoid dropped frames by specifying a higher output frame rate than the input. For example, to go from an input of 4 FPS to one that is sped up to 4x that (16 FPS):

ffmpeg -i input.mkv -r 16 -filter:v "setpts=0.25*PTS" output.mkv

To slow down your video, you have to use a multiplier greater than 1:

ffmpeg -i input.mkv -filter:v "setpts=2.0*PTS" output.mkv

Smooth

You can smooth out slow/fast video with the ​minterpolate video filter. This is also known as “motion interpolation” or “optical flow”.

ffmpeg -i input.mkv -filter "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=120'" output.mkv

Other options include ​slowmoVideo and ​Butterflow.

Speeding up/slowing down audio

You can speed up or slow down audio with the ​atempo audio filter. To double the speed of audio:

ffmpeg -i input.mkv -filter:a "atempo=2.0" -vn output.mkv

The atempo filter is limited to using values between 0.5 and 2.0 (so it can slow it down to no less than half the original speed, and speed up to no more than double the input). If you need to, you can get around this limitation by stringing multiple atempo filters together. The following with quadruple the audio speed:

ffmpeg -i input.mkv -filter:a "atempo=2.0,atempo=2.0" -vn output.mkv

Using a complex filtergraph, you can speed up video and audio at the same time:

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
退出移动版