Skip to content

Instantly share code, notes, and snippets.

@abdullahoguk
Last active February 3, 2023 16:30
Show Gist options
  • Save abdullahoguk/c856daab43d5e2ece4336e06774c026a to your computer and use it in GitHub Desktop.
Save abdullahoguk/c856daab43d5e2ece4336e06774c026a to your computer and use it in GitHub Desktop.
Useful FFMpeg Commands

Useful FFMpeg Commands

Extract audio

ffmpeg -i video.mp4 -vn -acodec copy audio.aac

Convert a gif to mp4 video

ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4

Convert mp4 video to gif

ffmpeg -i input.mp4 -r 10 agif_r10_320x180.gif -hide_banner

Convert a regular landscape video to 9:16 portrait without cropping and scaling (adds box blur to top and bottom)

For Instagram stories.
ffmpeg -i inputfile.mp4 -lavfi "[0:v]scale=iw:2*trunc(iw*16/18),boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch):chroma_power=1[bg];[bg][0:v]overlay=(W-w)/2:(H-h)/2,setsar=1" output2.mp4

Convert a video to 1:1 with blurred bg (replace 683s with square side length)

ffmpeg -i out2.mp4 -c:v libx264 -crf 23 -filter_complex "[0:v]split=2[blur][vid];[blur]scale=683:683:force_original_aspect_ratio=increase,crop=683:683,boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch)/20:chroma_power=1[bg];[vid]scale=683:683:force_original_aspect_ratio=decrease[ov];[bg][ov]overlay=(W-w)/2:(H-h)/2" out3.mp4 -y

Convert a regular landscape video to 1:1 square video by adding black offset to top and bottom

Change scale and pad values to longest side(probably width) of your video.

ffmpeg -i input.mp4 -vf "scale=1280:1280:force_original_aspect_ratio=decrease,pad=1280:1280:(ow-iw)/2:(oh-ih)/2:black" output.mp4

Convert series of images to mp4 video

Names of images in series should be in a specific pattern.
ffmpeg -framerate 60 -pattern_type glob -i "*.JPG" -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p output.mp4

Compress mp4 video

ffmpeg -i input.mp4 -vcodec h264 -acodec mp3 output.mp4
much small > ffmpeg -i input.mp4 -vcodec h264 -b:v 1000k -acodec mp3 output.mp4
a little small > ffmpeg -i output.mp4 -vcodec libx265 -acodec aac -crf 23 output.mp4

Speed Up 2x video by dropping frames

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

Convert 60fps video to 30fps by slowing down (no frame dropping).

PTS effects the length. 2*PTS makes a two times long video.
ffmpeg -i input.mp4 -vf setpts=2*PTS -r 30 -crf 18 output.mp4

Convert 60fps video to 30fps by dropping frames

Video length does not change, but every one of two frames will be dropped.
ffmpeg -i input.mp4 -r 30 -an -filter:v "setpts=1*PTS" output.mp4

Increase fps by frame interpolation

Duration will be same but new frames will be generated based on frame interpolation algorithm.
Default ffmpeg -i input.mp4 -filter:v "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=60'" output.mkv

Other motion estimation algorithms. Change me value (right before output) with fss ds hexbs or more (on ffmpeg minterpolate filter documentation).
ffmpeg -i input.mp4 -filter:v "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=60:me=fss'" output.mkv

Record your screen in timelapse

x11grab work on Linux. FFmpeg supports some external cli programs that you probably can install to your OS to use this feature.
The command below, gets a frame in every second and produce 30 fps video. change s with your screen resolution.
ffmpeg -framerate 1 -f x11grab -s 1366,768 -i :0.0+0,0 -vf settb=\(1/30\),setpts=N/TB/30 -r 30 -vcodec libx264 -crf 0 -preset ultrafast -threads 0 out.mkv
Record in half resolution with scale filter
ffmpeg -framerate 1 -f x11grab -s 1366,768 -i :0.0+0,0 -vf settb=\(1/30\),setpts=N/TB/30,scale=iw*.5:ih*.5 -r 30 -vcodec libx264 -crf 0 -preset ultrafast -threads 0 out.mkv

Record M3U8 Stream

ffmpeg -i HSLLINK -c copy -bsf:a aac_adtstoasc output.mp4
ffmpeg -i "hsl link" -bsf:a aac_adtstoasc -vcodec copy -c copy -crf 50 out.mp4

Slice video

ffmpeg -ss 00:01:00 -i input.mp4 -to 00:02:00 -c copy output.mp4

  • -ss: slicing start time
  • -to: slice duration
  • -c copy: This is an option to trim via stream copy. (NB: Very fast)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment