ffmpeg: scripting with the video swiss army knife

FFmpeg has become my go-to tool for routine video file and stream manipulation tasks. The documentation for the ffmpeg command line utility is enormous, as one might expect for a really powerful tool. While there’s no shortage of example scripts in the ffmpeg official documentation, I’ve always appreciated short-form, “cookbook” style lists of example scripts for given use-cases. Here are some of my most commonly used ffmpeg scripts:

View all of the metadata contained in a video file

 

ffmpeg -i ~/myVideoFile.mp4 2>&1

 

Fix incorrect or missing rotation metadata

 

After shooting video on your mobile device, you open the video file for playback on your laptop and notice that the image orientation isn’t right. The video appears to have been rotated 90 degrees one way or another, or flipped upside down (180 degrees). Check the “rotate” metadata property value for the video stream in your video file:

 

ffmpeg -i ~/myVideoFile.mp4 2>&1 | grep rotate

 

If the script doesn’t output anything, assume a value of 0. Add or subtract from the noted rotate value (in increments of 90 degrees) to arrive at a corrected number. Create a copy of the video with the rotate metadata property specified (at 270 degrees in this case):

 

ffmpeg -i ~/myVideoFile.mp4 -movflags use_metadata_tags -map_metadata 0 -metadata:s:v rotate="270" -c:v copy -c:a copy ~/myVideoFile_reoriented.mp4

 

Trim a video file

 

Create a short clip from a longer video, with a start time and length of your choosing:

 

ffmpeg -i ~/myVideoFile.mp4 -ss 00:30 -t 17.5 -c:v libx264 -c:a copy ~/myVideoFile_clipped.mp4

 

Extract audio from a video file

 

Extract an audio stream from a video file, then copy it to a new audio-only container file:

 

ffmpeg -i ~/myVideoFile.mp4 -qscale:a 0 -map a ~/myAudioFile.m4a