Skip to content

Instantly share code, notes, and snippets.

@artwilton
Last active August 26, 2022 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save artwilton/7359e045198996318af9f26983671666 to your computer and use it in GitHub Desktop.
Save artwilton/7359e045198996318af9f26983671666 to your computer and use it in GitHub Desktop.
FFmpeg commands I use often

FFmpeg commands I use often

Basic Encoding

Export a medium quality mp4 file at 1280 x 720

ffmpeg -i <input> -c:v libx264 -preset fast -crf 22 -s 1280x720 -c:a libfdk_aac -b:a 196k -ar 44100 -pix_fmt yuv420p <output>.mp4

Quickly convert audio to .wav file

ffmpeg -i <input> <output>.wav


Apple ProRes

Convert to Apple ProRes 4444 at 1920x1080

ffmpeg -i <input> -c:v prores_ks -profile:v 4 -qscale:v 8 -vendor ap10 -pix_fmt yuv422p10le -s 1920x1080 <output>.mov

Deinterlace DVD footage and convert to Apple ProRes 4444

ffmpeg -i <input> -c:v prores_ks -profile:v 4 -qscale:v 8 -vendor ap10 -pix_fmt yuv422p10le -vf scale=640x480,setsar=1:1,yadif <output>.mov


Avid DNxHD

Overlay a graphic file/watermark, ensure timecode data is passed through, and convert to Avid DNxHD 36

ffmpeg -i <input> -i <overlay-graphic>.png -filter_complex "overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2" -map 0:v -map 0:a -map 0:d -c:v dnxhd -b:v 36M -pix_fmt yuv422p -c:a pcm_s16le -ar 48000 <output>.mov


Rewrap / remux

Rewrap video file without re-encoding, example used is .m4v to .mp4

ffmpeg -i <input>.m4v -c:v copy -c:a copy <output>.mp4

An alternate syntax to the above code which ensures all video and audio streams are copied

ffmpeg -i <input>.m4v -c copy -map 0 <output>.mp4

Copy video without re-encoding, but remove audio

ffmpeg -i <input> -c:v copy -an <output>

Merge an mp4 file that doesn't have audio with an mp4 file that has audio but no video

ffmpeg -i <input-with-video-and-no-audio>.mp4 -i <input-with-audio-and-no-video>.mp4 -c:v copy -c:a copy <output>


Screen Capture

First, run the following to check for device inputs

ffmpeg -f avfoundation -list_devices true -i ""

In my case, this resulted in an output of:

AVFoundation video devices: [0] Capture screen 0

For best quality, capture the screen output to a lossless mkv file

ffmpeg -f avfoundation -i "0:0" -framerate 30 -c:v libx264 -crf 0 -preset ultrafast <output>.mkv

Cleaning Up Captured Footage / Removing Duplicate Frames

If you are capturing 23.976fps or 24fps content from your screen and want to remove the duplicate frames generated by capturing at 30 or 60fps, this command will remove those duplicate frames and output to an image sequence

ffmpeg -i <input> -vsync 0 -vf mpdecimate <output>%05d.png

You can also output to image sequence and keep original frame numbers if needed

ffmpeg -i <input> -vsync 0 -frame_pts true -vf mpdecimate <output>%05d.png

This command combines a few filters to trim a 1920x1200 video to 1920x1080, ensures a 23.976fps output, and encodes to ProRes 4444

ffmpeg -i <input> -vf mpdecimate,setpts=N/23.976/TB,crop=in_w:in_h-120 -c:v prores_ks -profile:v 4 -qscale:v 8 -vendor ap10 -pix_fmt yuv422p10le -s 1920x1080 -r 24000/1001 <output>.mov


Seeking and Trimming Video Clips

Basic seeking and trimming syntax

Seek to the point indicated by the first time stamp and set that as the first frame, trimming everything that comes before it. The second time stamp indicates the duration of the clip, starting from the first time stamp.

ffmpeg -ss HH:MM:SS.MILLISECONDS -i <input>.mp4 -t HH:MM:SS.MILLISECONDS <output>.mp4

More advanced use cases

Avoid audio offset when trimming by using rational numbers.

I've found that trimming with ffmpeg can sometimes offset audio streams by about half a frame, seemingly due to the fact that audio samples are timed on a sub-frame level. A fix for this is to use rational frames, for example here is how to trim a 29.97 NTSC video from frame 1,000 to 2,000:

ffmpeg -i <input>.mp4 -ss (1000/(30000/1001)) -to (2000/(30000/1001)) <output>.mp4

Without re-encoding, seek to a specific starting timecode in a video file and trim up to a specific ending timecode.

This will reset the beginning timecode of the output file to 0 with -avoid_negative_ts 2 The timecode format here can be HH:MM:SS.MILLISECONDS or in seconds.

ffmpeg -ss <starting timecode> -i <input>.mp4 -to <ending timecode> -c copy -copyts -avoid_negative_ts 2 <output>.mp4

More useful info and examples on seeking and trimming can be found here - https://trac.ffmpeg.org/wiki/Seeking


Scripting

Iterate through folder and convert video files to .mp4

First cd into the directory containing the video files you want to convert, then run this script:

#!/bin/bash

# add extensions and rename output path as needed
fileExt="*.mp4 *.mov *.avi *.m4v *.mxf"
outputPath="./converted"

shopt -s nullglob
shopt -s nocaseglob

mkdir -p "$outputPath"

for i in $fileExt; do ffmpeg -i "$i" "$outputPath"/"${i%.*}".mp4 ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment