Linux Shell FFmpeg


Common codes

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

Cutting screen

#Width: width
#Height: height
#Left: left margin
#Top: top margin
ffmpeg -i input.mp4 -vf crop=[width]:[height]:[left]:[top] -b 1M -vcodec h264 output.mp4

#Keep the bottom left quarter
# 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 to mov

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

Picture to video

# mspf: millisecond per frame
# %06d.jpg: 000001.jpg, 000002.jpg, ...
#VF: images with odd width and height are converted to videos with even width and height, and black is used to fill the gaps
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

Generate thumbnails

#One per second
ffmpeg -i input.mp4 -vf fps=1 screenshot_%d.png

#One per minute
ffmpeg -i input.mp4 -vf fps=1/60 screenshot_%d.png

#One every ten minutes
ffmpeg -i input.mp4 -vf fps=1/600 screenshot_%d.png

De watermark

1. Show parameter is used to display the green box on the video to estimate the watermark position

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

2. After the watermark position is determined, the watermark is removed

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

3. Remove watermark and add text

ffmpeg -i " input.mkv " -c:a aac -b:a 320K -c:v h264_ nvenc -b:v 5M -maxrate 20M -vf delogo=x=1680:y=1030:w=230:h=44:show=0,drawtext=fontfile= YouSheBiaoTiHei.ttf : text = text content: x = 1772: y = 1054: fontcolor= white:fontsize=25  " output.mp4 "

Connect video and audio

1. No transcoding

#- shortest, the duration of the output file = the shortest of all input files
#Otherwise, the duration of the output file = the longest of all input files
ffmpeg -i video.mp4 -i audio.m4a -c copy -shortest output.mp4

2. Transcoding

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

3. Replace existing audio

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

Connect multiple audio

1. New file list.txt The contents are as follows

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

2. Connection

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

Inverse color

If you need to support MacOS QuickTime playback, add “- pix”_ FMT yuv420p “parameter.

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

Modify video playback speed

PTS (presentation time stamp)

#Speed up
ffmpeg -i input.mp4 -vf setpts=PTS/8 output.mp4
#Slow down
ffmpeg -i input.mp4 -vf setpts=PTS/(1/4) output.mp4

Modify audio playback speed

The Atempo parameter ranges from 0.5 to 2.0

It needs to be used in combination to achieve 8x or 0.25x playback

#Speed up
ffmpeg -i input.mp4 -af atempo=2.0,atempo=2.0,atempo=2.0,atempo=2.0 output.mp4
#Slow down
ffmpeg -i input.mp4 -af atempo=0.5,atempo=0.5 output.mp4

Modify video and audio playback speeds at the same time

#Speed up
ffmpeg -i input.mp4 -vf setpts=PTS/8 -af atempo=2.0,atempo=2.0,atempo=2.0,atempo=2.0 output.mp4
#Slow down
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


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