Skip to content

Instantly share code, notes, and snippets.

@saurabheights
Created August 19, 2017 04:54
Show Gist options
  • Save saurabheights/0530e8637c14969f4631d6646180b4f9 to your computer and use it in GitHub Desktop.
Save saurabheights/0530e8637c14969f4631d6646180b4f9 to your computer and use it in GitHub Desktop.
FFMPEG
# Convert gif to video. The best/right way to serve GIF in today's date.
ffmpeg -f gif -i input.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -preset veryslow output.mp4
# Create a video from slideshow with framerate 1 frame per 2 seconds.
ffmpeg -framerate 1/2 -i %d_1x1.jpeg -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4
# Encode with same video but different audio
ffmpeg -i input.mp4 -vcodec copy -acodec {mp2,mp3,aac,ac3} output-vcodec_h264-acodec_mp2.mp4
# Encode with video as h264 and audio as aac with crf
ffmpeg -i input.mp4 -vcodec h264 -acodec aac -crf 28 output.mp4
# Create WEBM using libvpx and constant bitrate.
# Tip - Use constant bit rate when streaming and variable bitrate when packing with app.
ffmpeg -i input.mp4 -vcodec libvpx-vp9 -b:v 200K output.webm
ffmpeg -i input.mp4 -vcodec libvpx-vp8 -b:v 200K output.webm
# CBR(constant bitrate) is best used with two-pass encoding.
ffmpeg -y -i input.avi -c:v mpeg4 -vtag xvid -b:v 555k -pass 1 -an -f avi /dev/null
ffmpeg -i input.avi -c:v mpeg4 -vtag xvid -b:v 555k -pass 2 -c:a libmp3lame -b:a 128k output.avi
# Variable Bitrate - Select a video quality level with -qscale:v n (-q:v n), where n is a number from 1-31, 1 - highest quality/largest filesize & 31 being the lowest quality/smallest filesize.
# This is a variable bit rate mode, roughly analogous to using -qp (constant quantization parameter) with x264. Most of the time this should be the preferred method.
ffmpeg -i input.avi -c:v mpeg4 -vtag xvid -qscale:v 3 -c:a libmp3lame -qscale:a 4 output.avi
# Tip: Use preset veryslow for better compression. Takes time, so use it for the best 2-3 files with best compression.
# Note that other presets like slow and placebo exists, but sometimes placebo can give you worse results even though it is supposed to provide better.
ffmpeg -i input.mp4 -preset veryslow -vcodec h264 -acodec aac -crf 27 output.mp4
# Increase volume - doesn't do much.
ffmpeg -i input.mp4 -filter:a "volume=1.5" output.mp4
# Dynamically Compress audio
ffmpeg -i input.mp4 -vcodec copy -af dynaudnorm output.mp4
# Trim video from start time(ss) to a specific time(77.2 seconds)
ffmpeg -ss 0 -i Original.mp4 -t 77.2 -c copy Original-Trimmed.mp4
# Split video and audio streams
ffmpeg -i input.mp4 -map 0:1 -c copy input-Audio.m4a
ffmpeg -i input.mp4 -an -c copy input-Video.mp4
# Compress audio stream into aac(Android-medium bitrate) and ac3(IOS-64 Kbps) using Audacity
# Do compare the input-Audio.m4a with compressed audio before moving ahead.
# Compress Video stream using libx264
ffmpeg -i input.mp4 -vcodec libx264 -threads 4 output.mp4
# Remove Duplicated frames and use veryslow preset to get most compression.
ffmpeg -i input.mp4 -an -vf "mpdecimate" -preset veryslow -threads 4 output.mp4
# If the video is not compressed enough, perform lossy compression
crf=30
ffmpeg -i input.mp4 -preset veryslow -crf ${crf} -movflags +faststart -threads 4 OutputVideo_${crf}.mp4
ffmpeg -i OutputVideo_${crf}.mp4 -i input.ac3 -c copy -map 0:v:0 -map 1:a:0 Output_${crf}_iOS.mp4
ffmpeg -i OutputVideo_${crf}.mp4 -i input.mp3 -c copy -map 0:v:0 -map 1:a:0 Output_${crf}_Android.mp4
# Extract All frames of Video( be careful that the color profile if present will be removed while packing frames to video)
# Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(tv, bt709) <- Here bt709 is the color profile.
# Unpacking
mkdir frames
ffmpeg -i input.mp4 frames/frames-%4d.png
# Packing
ffmpeg -framerate 25 -i frames/frames-%4d.png -c:v libx264 -pix_fmt yuv420p output.mp4
# Run experimental stuff
ffmpeg -i input.mp4 -i Original-Audio.m4a -c:v copy -c:a aac -strict experimental output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment