Last active
January 31, 2023 07:26
-
-
Save jcarpenter/d915272a043148f0aff96fa7b70ade4c to your computer and use it in GitHub Desktop.
Collection of useful FFMPEG techniques
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------ IMAGE SEQUENCE ------ # | |
# Create QuickTime-friendly .MOV from a sequence of PNGs | |
# In this example they start with “test…” | |
# Note: The crop filter is so we don’t need to worry about input files | |
# being divisible by two. If they’re not, we crop them to fit | |
# with `-vf "crop=trunc(iw/2)*2:trunc(ih/2)*2"` | |
ffmpeg -r 60 -pattern_type glob -i "test*.png" -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" -vcodec libx264 -crf 5 -pix_fmt yuv420p output.mov | |
# ------ CONVERT TO MP4 ------ # | |
# For max compatibility, use profile option. May increase bit rate “quite a bit”: | |
# Disable audio stream with -an option. -pix_fmt yuv420p is for Apple Quicktime support. | |
# Basic: | |
ffmpeg -i logo.mov -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -level 3 -an logo.mp4 | |
# With filters: | |
ffmpeg -i logo.mov -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -level 3 -vf "pad=width=2456:height=1382:x=-1:y=-0:color=#000000, fillborders=left=644:right=644:mode=smear, scale=1000:-1" -an logo.mp4 | |
# ------ CONCATENATE ------ # | |
# Combine multiple clips into one. | |
# There are multiple ways to do this. | |
# - Docs: https://trac.ffmpeg.org/wiki/Concatenate | |
# - SO: https://stackoverflow.com/a/11175851 | |
# Prefer to use the "concat demuxer" | |
# Per SO thread: "Use this method when you want to avoid a re-encode and your format does not support file-level concatenation (most formats used by general users do not)." | |
# NOTE: Am using printf here so we can declare the inputs within a simple one-liner. | |
# Most examples have the user instead create a dedicated .txt file listing the files. | |
ffmpeg -f concat -safe 0 -i <(printf "file input-1.mp4 \n file input-2.mp4") -c copy output.mp4 | |
# ------ COMPRESS ------ # | |
# Filters crop and scale are just examples. | |
# Can remove the -vf lines if we don't want to apply filters. | |
# Create WebM | |
ffmpeg -y -i input.mp4 -c:v libvpx-vp9 -pass 1 -b:v 0 -crf 33 -threads 8 -speed 4 \ | |
-tile-columns 6 -frame-parallel 1 \ | |
-an -f webm /dev/null | |
ffmpeg -y -i input.mp4 -c:v libvpx-vp9 -pass 2 -b:v 0 -crf 33 -threads 8 -speed 2 \ | |
-vf crop=400:300 scale='min(1000, iw)':-2 \ | |
-tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 \ | |
-c:a libopus -b:a 64k -f webm output.webm | |
# Create Mp4 | |
ffmpeg -y -i input.mp4 -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -level 3 \ | |
-vf crop=400:300 scale='min(1000, iw)':-2 \ | |
-an output.mp4 | |
# ------ ROTATE ------ # | |
# SO: https://stackoverflow.com/questions/3937387/rotating-videos-with-ffmpeg | |
# Rotate without re-encoding | |
# (requires player that supports rotation metadata) | |
ffmpeg -i input.mp4 -map_metadata 0 -metadata:s:v rotate="90" -codec copy output.mp4 | |
# Check metadata first | |
# (am not sure if this always works reliably) | |
ffmpeg -i input.mp4 2>&1 | grep rotate | |
# ------ TIME ------ # | |
# Docs: https://trac.ffmpeg.org/wiki/Seeking | |
# FMPEG time unit syntax: | |
# Docs: https://trac.ffmpeg.org/wiki/Seeking#Timeunitsyntax | |
# "Note that you can use two different time unit formats: sexagesimal (HOURS:MM:SS.MILLISECONDS, as in 01:23:45.678), or in seconds. If a fraction is used, such as 02:30.05, this is interpreted as "5 100ths of a second", not as frame 5. For instance, 02:30.5 would be 2 minutes, 30 seconds, and a half a second, which would be the same as using 150.5 in seconds." | |
# Trim time to specified length, from specified start point. | |
# This clip would be 5.75 seconds long. | |
# -ss: Start time of the clip | |
# -to: Duration of the clip | |
# -c copy: Trim via stream copy (fast and performant) | |
ffmpeg -ss 00:01:00 -i input.mp4 -to 00:06.75 -c copy output.mp4 | |
# Trim from start to specified time | |
# (same as above, but leaving out -s sets it to default 00:00) | |
# -to: End time of the clip | |
# -c copy: Trim via stream copy (fast and performant) | |
ffmpeg -i input.mp4 -to 00:06.75 -c copy output.mp4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment