Skip to content

Instantly share code, notes, and snippets.

@daltonbr
Last active September 11, 2020 08:07
Show Gist options
  • Save daltonbr/be6ef650d5e4abadc29b6cbc8cd1a487 to your computer and use it in GitHub Desktop.
Save daltonbr/be6ef650d5e4abadc29b6cbc8cd1a487 to your computer and use it in GitHub Desktop.
A few snippets for ffmpeg - Rescaling, trimming, converting & making gif's and popcorn 🍿
# Simple Image/video rescaling
ffmpeg -i input.avi -vf scale=320:240 output.avi
ffmpeg -i input.jpg -vf scale=320:240 output_320x240.png
# Keeping the Aspect Ratio
# If we'd like to keep the aspect ratio, we need to specify only one component, either width or height,
# and set the other component to -1.
ffmpeg -i input.jpg -vf scale=320:-1 output_320.png
# Removing/remapping audio
# First run ffmpeg -i file.mp4 to see which streams exists in your file. You should see something like this:
# Stream #0.0: Video: mpeg4, yuv420p, 720x304 [PAR 1:1 DAR 45:19], 23.98 tbr, 23.98 tbn, 23.98 tbc
# Stream #0.1: Audio: ac3, 48000 Hz, 5.1, s16, 384 kb/s
# Stream #0.2: Audio: ac3, 48000 Hz, 5.1, s16, 384 kb/s
# Then run bellow line to copy video stream and 2nd audio stream to new_file.mp4.
ffmpeg -i file.mp4 -map 0:0 -map 0:2 -acodec copy -vcodec copy new_file.mp4
# Getting only the audio track (assuming the 0:1 track) and cutting it, changing the audio coded to mp3
ffmpeg -i InputVideo.mp4 -ss 00:43 -to 00:55 -map 0:1 -acodec mp3 AudioEdited.mp3
# other audio codecs
# libvorbis (ogg); libmp3lame (mp3); libfaac (aac); ac3 (mp3)
# Cutting small sections
# more info: https://trac.ffmpeg.org/wiki/Seeking#Cuttingsmallsections
ffmpeg -i video.mp4 -ss 00:01:00 -to 00:02:00 -c copy cut.mp4
ffmpeg -i video.mp4 -ss 00:26 -to 00:32 -copyinkf -movflags +faststart cut.mp4
# copyinkf to avoid problems with keyframes missing
# Converting a photo into a movie
# One static frame
ffmpeg -i photo.jpeg -c:v libx264 photoAsVideo.mp4
# ...or looping that image
ffmpeg -loop 1 -i image.png -c:v libx264 -t 15 -pix_fmt yuv420p -vf scale=320:240 out.mp4
# The -t 15 makes it 15 seconds long.
# The -vf scale=320:240 sets the width/height.
# Converting to gif
ffmpeg -i yesbuddy.mov -pix_fmt rgb24 output.gif
# Note that you probably want to reduce the frame rate and size when you convert, as well as specify
# a start time and duration. You probably do not want to convert the entire file at its original
# resolution and frame rate.
ffmpeg -ss 00:00:00.000 -i yesbuddy.mov -pix_fmt rgb24 -r 10 -s 320x240 -t 00:00:10.000 output.gif
# The file size will still be huge. You may be able to use ImageMagick's GIF optimizer to reduce the size:
convert -layers Optimize output.gif output_optimized.gif
# When encoding with libx264, you can set the H.264 profile and level with:
# -profile:v – one of high, main, or baseline (and others, but this is irrelevant here)
# -level:v – as defined in Annex A of the H.264 standard, e.g., 4.0.
# For example:
ffmpeg -i input.mp4 -c:v libx264 -profile:v high -level:v 4.0 -c:a copy output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment