Hi folks,
I thought this could be a good place to discuss tricks for working with ffmpeg. For those who haven’t heard of it, ffmpeg
is a command line program, a sort of swiss army knife to record, edit and convert audio-video files.
Installation
Mac
On a Mac, you can install it using Homebrew with brew install ffmpeg
, or MacPorts with port install ffmpeg
.
Linux
Linux users can use your package manager and I’m guessing are all pros! Debian-derived systems: sudo apt install ffmpeg
Windows
Windows folks, if you want to use it in the command prompt, here’s a tutorial. Alternatively, you can install it in Windows Subsystem for Linux: apt-get install ffmpeg
Snippets you can use on the command line
When I was first working in Linux or the mac’s command line years ago I had trouble deciphering these little snippets, so ask if you have questions, and if you have any other snippets or tips, share them here. Here’s some code snippets I use all of the time.
Convert one video format to another, simply. Here’s mp4 to avi
ffmpeg -i input.mp4 output.avi
Extract the sound from a video and save it as MP3:
ffmpeg -i {{video.mp4}} -vn {{sound}}.mp3
Combine numbered images (frame_1.jpg, frame_2.jpg, etc) into a video or GIF:
ffmpeg -i {{frame_%d.jpg}} -f image2 {{video.mpg|video.gif}}
Trim a video from a given start time mm:ss to an end time mm2:ss2 (omit the -to flag to trim till the end):
ffmpeg -ss {{mm:ss}} -to {{mm2:ss2}} -i {{video.mp4}} -codec copy {{output.mp4}}
Convert AVI video to MP4. AAC Audio @ 128kbit, h264 Video @ CRF 23:
ffmpeg -i {{input_video}}.avi -codec:audio aac -b:audio 128k -codec:video libx264 -crf 23 {{output_video}}.mp4
Capture live video from your facetime camera, stopping by entering Control-C
ffmpeg -f avfoundation -i "FaceTime" sampleFaceTime.mkv
Convert frames from a video or GIF into individual numbered images
ffmpeg -i {{video.mpg|video.gif}} {{frame_%d.png}}
Combine numbered images (frame_1.jpg, frame_2.jpg, etc) into a video or GIF
ffmpeg -i {{frame_%d.jpg}} -f image2 {{video.mpg|video.gif}}
Quickly extract a single frame from a video at time mm:ss and save it as a 128x128 resolution image
ffmpeg -ss {{mm:ss}} -i {{video.mp4}} -frames 1 -s {{128x128}} -f image2 {{image.png}}
Remux MKV video to MP4 without re-encoding audio or video streams
ffmpeg -i {{input_video}}.mkv -codec copy {{output_video}}.mp4
Convert MP4 video to VP9 codec. For the best quality, use a CRF value (recommended range 15-35) and -b:video MUST be 0
ffmpeg -i {{input_video}}.mp4 -codec:video libvpx-vp9 -crf {{30}} -b:video 0 -codec:audio libopus -vbr on -threads {{number_of_threads}} {{output_video}}.webm
Convert long and varied-color video clip to animated gif, resizing and reducing file size
ffmpeg -i input.mp4 -filter_complex "[0:v] fps=12,scale=w=480:h=-1,split [a][b];[a] palettegen=stats_mode=single [p];[b][p] paletteuse=new=1" animation.gif
Reduce video file size using h265. Change crf value lower (perhaps between 18-30) for higher quality/larger file size
ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4
Upscale + pillarbox. No cropping, respects ratio, useful if converting PAL/NTSC to 1280x720
ffmpeg -i input.mkv -vf "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1:color=black" output.mkv
Alternative CLI software to make it easier: ffmfriend
@sarahghp’s ffmfriend is a commandline program that wraps around ffmpeg so you don’t have to look up some of the most common use-cases below.
You can install it by downloading and unzipping it. Then enter the root directory and run: npm install && npm link
Help info is available for ffmfriend by typing ffmfriend -h
Tutorials
- How To Make Gifs with ffmpeg via Giphy
- How can I reduce a video’s size with ffmpeg? via StackExchange