Skip to content

Instantly share code, notes, and snippets.

@alterebro
Last active January 14, 2024 23:02
Show Gist options
  • Save alterebro/5dfa154153d85c084b1b868c55ee8e76 to your computer and use it in GitHub Desktop.
Save alterebro/5dfa154153d85c084b1b868c55ee8e76 to your computer and use it in GitHub Desktop.
- RESAMPLE : ffmpeg -i [INPUT] -b:v 800k -b:a 128k [OUTPUT]
- RESAMPLE (codecs) : ffmpeg -i [INPUT] -c:v libx264 -preset medium -b:v 1M -c:a aac -b:a 64k [OUTPUT]
- NOISE : ffmpeg -i [INPUT] -codec:v copy -codec:a copy -bsf:a noise [OUTPUT]
- CUT : ffmpeg -i [INPUT] -ss 30 -c copy -t 180 [OUTPUT]
- CUT (sec) : ffmpeg -i [INPUT] -ss 00:00:05 -t 00:00:35 -async 1 -c copy cut.mp4
- SEGMENTS : ffmpeg -i [INPUT] -c copy -map 0 -segment_time 3 -f segment clip%04d.mp4
- CONCAT : ffmpeg -f concat -i files.txt -c copy [OUTPUT]
- SCALE : ffmpeg -i [INPUT] -vf scale=640:360 [OUTPUT]
- SPEED :
ffmpeg -i [input] -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" out-fast.mp4
ffmpeg -i [input] -filter_complex "[0:v]setpts=2.0*PTS[v];[0:a]atempo=0.5[a]" -map "[v]" -map "[a]" out-slow.mp4
ffmpeg -i [input] -filter_complex "[0:v]setpts=0.8*PTS[v];[0:a]atempo=1.25[a]" -map "[v]" -map "[a]" out-fast-II.mp4
- CHECK DURATION : ffprobe -i 01.mp4 -show_entries format=duration -v quiet -of csv="p=0"
(or) : ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 vid.mp4
- CROP : ffmpeg -i in.mp4 -filter:v "crop=out_w:out_h:x:y" out.mp4
- RESIZE AND FIT IN FIXED : ffmpeg -i input.mp4 -vf "scale=w=1080:h=1920:force_original_aspect_ratio=1,pad=1080:1920:(ow-iw)/2:(oh-ih)/2:color=#f0f0f0" output.mp4
---
* CROP : ffmpeg -i [input] -filter:v "crop=w:h:x:y" [output]
* CUT TIME : ffmpeg -i [input] -ss 00:00:04 -t 00:00:10 [output]
* SCALE w/auto-height : ffmpeg -i [input] -vf scale=1280:-1 [output]
* ANIMATED GIF : ffmpeg -i [input] [output].gif -hide_banner
* REVERSE VIDEO : ffmpeg -i [input] -vf reverse [output]
---
# Rotate
$ ffmpeg -i input.mp4 -vf "transpose=1" output.mp4 # 1 – Rotate by 90 degrees clockwise.
$ ffmpeg -i input.mp4 -vf "transpose=2" output.mp4 # 2 – Rotate by 90 degrees counter-clockwise.
# Convert
ffmpeg -i {input}.mov -vcodec h264 -acodec mp2 {output}.mp4
ffmpeg -i {input}.mov -vcodec h264 -acodec aac -strict -2 {output}.mp4
ffmpeg -i {input}.mov -crf 23 -preset medium -movflags +faststart -c:a aac {output}.mp4
ffmpeg -i {input}.mov -filter:v -movflags +faststart -vcodec h264 -acodec aac -strict -2 {output}.mp4
ffmpeg -i input.mov -qscale 0 output.mp4 # no encoding (fast)
ffmpeg -i input.mov -q:v 0 output.mp4 # re-encode (best quality)
# Extract audio
ffmpeg -i sample.avi -q:a 0 -map a sample.mp3
# MP4 to Animated GIF w/palette
$ ffmpeg -v warning -i "input.mp4" -vf "fps=30,scale=1080:-1:flags=lanczos,palettegen" -y "palette.png"
$ ffmpeg -v warning -i "input.mp4" -i "palette.png" -lavfi "fps=30,scale=1080:-1:flags=lanczos [x]; [x][1:v] paletteuse" -y output.gif
# Animated gif to MP4
$ ffmpeg -f gif -i infile.gif outfile.mp4
$ ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4
# Animated gif to frames
$ ffmpeg -i input.gif -vsync 0 output%03d.png
# Movie from frames
$ ffmpeg -r 30 -f image2 -s 540x540 -i "%07d.png" -vcodec libx264 -crf 17 -pix_fmt yuv420p output.mp4 # (src: 000000n.png)
# Create Animated GIF from Sequenze using palette (8 cols)
$ ffmpeg -i 001.png -vf palettegen=8 palette.png
$ ffmpeg -i %03d.png -i palette.png -lavfi paletteuse output.gif
# Fade In and Out
ffprobe -select_streams v -show_streams in.mp4 # 2214 frames (nb_frames)
ffmpeg -i in.mp4 -vf "fade=in:0:30, fade=out:2184:30" out.mp4
# Split 15 seconds chunks...
ffmpeg -i in.mp4 -ss 0 -t 15 out-01.mp4
ffmpeg -i in.mp4 -ss 15 -t 15 out-02.mp4
ffmpeg -i in.mp4 -ss 30 -t 15 out-03.mp4
ffmpeg -i in.mp4 -ss 45 -t 15 out-04.mp4
ffmpeg -i in.mp4 -ss 60 -t 15 out-05.mp4
## Optimize GIF
gifsicle -O3 --colors 64 --use-col web --lossy 100 --scale 0.8 in.gif -o out.gif
mogrify -layers 'optimize' -fuzz 7% in.gif
## Video from Sequenze
ffmpeg -framerate 30 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p out.mp4
ffmpeg -r 60 -f image2 -s 1920x1080 -i pic%04d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p test.mp4
---
# No Sound
ffmpeg -i [input] -c copy -an [output]
ffmpeg -i [input] -vcodec copy -an [output]
ffmpeg -i [input] -vf scale=1280:720 -vcodec h264 -b:v 512k -an [output]
ffmpeg -i [input] -vf scale=1280:720 -vcodec h264 -b:v 512k -an [output]
# Change Speed
ffmpeg -itsscale 0.76 -i input.mp4 -c copy output.mp4 # 0.76 is factor (BPM source / BPM target i.e 95/125)
---
# OPTIMIZE / RESAMPLE :
- H.265 (CRF 24 to 30 / lower CRF values = higher bitrates)
ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4
- H.264 (CRF 18 to 24 lower CRF = higher bitrate)
ffmpeg -i input.mp4 -vcodec libx264 -crf 20 output.mp4
- Fit to FileSize :
Calculate the bitrate you need by dividing your target size (in bits) by the video length (in seconds). For example for a target size of 1 GB (one gigabyte, which is 8 gigabits) and 10 000 seconds of video (2 h 46 min 40 s), use a bitrate of 800 000 bit/s (800 kbit/s):
ffmpeg -i input.mp4 -b 800k output.mp4
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment