Skip to content

Instantly share code, notes, and snippets.

@Cozy19
Forked from rabssm/ffmpegtips.txt
Created March 7, 2021 19:43
Show Gist options
  • Save Cozy19/721431d29d9ce8f494e5934532fbb846 to your computer and use it in GitHub Desktop.
Save Cozy19/721431d29d9ce8f494e5934532fbb846 to your computer and use it in GitHub Desktop.
ffmpeg tips
# Interpolate video frames for a higher frame rate
ffmpeg -i source.mp4 -filter:v minterpolate -r 25 result.mp4
ffmpeg -i source.mp4 -vf minterpolate=50,tblend=all_mode=average,framestep=2 result.mp4
# Crop a video to the bottom right quarter
ffmpeg -i in.mp4 -filter:v "crop=in_w/2:in_h/2:in_w/2:in_h/2" -c:a copy out.mp4
# Resize a video to half size
ffmpeg -i input.mkv -filter_complex "scale=in_w/2:in_h/2" output.mkv
# Crop a video to window xsize:ysize:x:y
ffmpeg -y -i shot0020.avi -vf 'crop=320:240:380:380' cropped.mp4
# Crop a video and then rescale
ffmpeg -y -i input.avi -vf 'crop=500:300:500:800,scale=1000:600' output.mp4
# Overlay a video resized to 640x480 onto a background video in the bottom right corner
ffmpeg -i shot0020.avi -i cropped.mp4 -filter_complex "[1] scale=640:480 [over]; [0][over] overlay=640:480" result.mp4
########################################
# Watermark a video
ffmpeg -i input.mkv -vf "drawtext=fontfile=Arial.ttf: text='©2019 Somebody': x=(w-tw)/2: y=h/2: fontcolor=white: box=1: fontsize=36: boxcolor=0x00000099" output.mp4
# Watermark a video with a copyright image
convert -fill grey -transparent white -font Arial -pointsize 72 label:"©2019 Somebody" -shade 120x45 name.png
ffmpeg -i input.mkv -i name.png -filter_complex "[1]lut=a=val*0.4[a];[0][a]overlay=(W-w)/2:(H-h)/2" output.mp4
# Watermark a video with a stylish large copyright symbol
convert -size 1200x800 -gravity West -fill black -transparent white -font Arial -pointsize 800 label:"©" -shade 240x40 copyright.png
convert -fill black -transparent white -font Arial -pointsize 96 label:"@your_name" -shade 240x40 name.png
composite -gravity Center -geometry -40-20 name.png copyright.png stamp.png
convert stamp.png -resize 30% stamp.png # Resize the stamp as required for the video
ffmpeg -i input.mp4 -i stamp.png -filter_complex "[1]lut=a=val*0.20[a];[0][a]overlay=w/4:H-h" -vcodec h264 output.mp4
########################################
# Overlay a black triangle at the bottom of a video
convert -size 60x60 xc:none -fill black -stroke black -draw "path 'M 0,0 L 30,45 L 60,0 Z' " -flip triangle.png
ffmpeg -i input.mp4 -i triangle.png -filter_complex "[1]lut=a=val*1.0[a];[0][a]overlay=W/2-w/2:H-h" -vcodec h264 triangled.mp4
# Overlay a frame number on the video
ffmpeg -i input.mp4 -vf "drawtext=fontfile=Arial.ttf: text=%{n}: x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000099" output.mp4
# Overlay a date and time on the video
ffmpeg -i input.mp4 -vf "drawtext=fontfile=Arial.ttf: text='%{pts\:gmtime\:1507046400\:%d-%m-%Y %T}': x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000099" output.mp4
# Convert an animated gif file to mp4 video
ffmpeg -f gif -i input.gif -pix_fmt yuv420p output.mp4
# Convert a video to a gif
ffmpeg -i input.mkv -filter_complex "fps=25,scale=1280:-1:flags=lanczos[x];[x]split[x1][x2];[x1]palettegen[p];[x2][p]paletteuse" output.gif
# Convert a video to a gif - 2 step process
ffmpeg -y -i input.mkv -vf palettegen palette.png
ffmpeg -y -i input.mkv -i palette.png -filter_complex paletteuse -r 25 output.gif
# Make a video showing a single image (-t 1 for a 1s video)
ffmpeg -loop 1 -i input.jpg -c:v libx264 -t 1 -pix_fmt yuv420p output.mp4
# Trim the start and end of a video
ffmpeg -i input.mp4 -ss 00:00:03 -t 00:00:08 -vcodec copy output.mp4
#######################
# Concatenate 2 videos
ffmpeg -i concat:"input1.mkv|input2.mkv" output.mkv
# ... or
# Create a file e.g. mylist.txt showing the input videos:
file input1.mkv
file input2.mkv
# Then use the command
ffmpeg -f concat -i mylist.txt -vcodec copy output.mkv
#######################
# Stack 2 videos vertically (use hstack for side by side)
ffmpeg -i input0 -i input1 -filter_complex vstack=inputs=2 output
# Increase the brightness of a video
ffmpeg -i input.mkv -vf "lutrgb='r=3*val:g=3*val:b=3*val'" output.mkv
# Increase the brightness and contrast of a video
ffmpeg -i input.mkv -vf eq=brightness=0.45:contrast=2.0:saturation=2 output.mkv
# Simulate a camera tremble
ffmpeg -i input.mkv -filter:v "crop=in_w/1.01:in_h/1.01:(in_w-out_w)/2+((in_w-out_w)/2)*sin(n/10):(in_h-out_h)/2 +((in_h-out_h)/2)*sin(n/7)" -vcodec h264 tremble.mkv
# Get Keyframe positions by frame number
ffprobe -show_frames -select_streams v:0 -print_format csv $VIDEO_FILE 2> /dev/null | awk -F ',' '{ if ($4 > 0) print NR; }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment