The ffmpeg thread

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 commandline program, a sort of swiss army knife to record, edit and convert audio-video files.

Installation

Mac

On a Mac, you can install it with homebrew: brew install ffmpeg.

Linux

Linux users can use your package manager and I’m guessing are all pros! Ubuntu-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 in 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.

ffmfriend

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

5 Likes

Some more tips from tldr, the sane and simple commandline tool to help you figure out how to use programs/software.

 - 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
2 Likes

ffmpeg is pretty sweet!
If you like the command prompt you might also like The Imagemagic Tool Kit.

2 Likes

thanks for doing this! I’ve gotten it installed but haven’t really done anything with it just yet

I’ve been wanting to put together a series of images into videos easily so this should be great

I’d love to see this as a wiki so the tips could be added to the OP continually (reorganized as necessary) and wouldn’t end up spread throughout the thread

I’m working from a mac

and wanted to preserve the alpha channel from a series of png images(from butterfly II animation 3ds)

sample of an image
dinoboom0000

replace “dinoboom” with whatever your pictures are called

first I CD into the folder containing the images
then

ffmpeg -i dinoboom%04d.png -vf palettegen palette.png
made

palette

ffmpeg -framerate 10 -i dinoboom%04d.png -i palette.png -lavfi paletteuse=alpha_threshold=128 -gifflags -offsetting result.gif

which made
result

3 Likes

incredible! this is great.

by the way @respiratori i’m not sure how to turn this into a wiki on here. any ideas?

i just changed it to the wiki category. i think anyone can edit now :slight_smile:

1 Like

This is timely! I just wrote myself a small ffmpeg CLI wrapper because I got sick of remembering things: Sarah Groff Hennigh-Palermo / ffmfriend · GitLab

Please feel free to use / add to if you’d like.

7 Likes

This looks great!! Thanks

1 Like

I tried to edit the original post but wasn’t able to. Is there a trick? The pen/pencil tool just shows me the changes.

whoops - @lettuce can u edit it now ?

2 Likes

it wasn’t showing up for me before but is now. thank you!

yep, thanks! edited to make it more wiki-like!

I’ve been using ffmpeg to do some motion interpolation glitchy effects, which works best if you try and slow down a very short video (two frames in my case) to be verrrrry long.

Here’s a breakdown of the settings https://www.hellocatfood.com/motion-interpolation-for-glitch-aesthetics-using-ffmpeg-part-0/

ffmpeg -i input -filter:v "setpts=62.5*PTS,minterpolate='fps=25:mb_size=16:search_param=400:vsbmc=0:scd=none:mc_mode=aobmc:me_mode=bilat:me=umh'" output.mp4

Not the function’s intended use but may be useful for someone!

7 Likes

That’s a cool effect, hadn’t seen that use case before. Nice

haven’t tried this but looks wow

3 Likes

Thanks for putting this together @lettuce! :grinning:

3 Likes

wooo displacement maps! found via Crash-Stop: RED plus FFmpeg displacement.
example (the main source abstract.mkv and the output displaced.mkv are shown below):

ffmpeg -i abstract.mkv -i people.mkv -lavfi ‘[1]split[y],[0][y]displace’ displaced.mkv

2021-05-08-01-16-23-1.mkv-50fps-mint.mkv.mp4 displacement.mkv

in a live setting, one could set capture cards as inputs and route the ffmpeg output to ffplay…

ffmpeg -i /dev/video0 -i /dev/video2 -lavfi ‘[1]split[y],[0][y]displace’ -f rawvideo -pix_fmt yuv420p - | ffplay -f rawvideo -pix_fmt yuv420p -s 720x576 -

7 Likes

Hey, this is a cool introduction and script collection for creative effects with ffmpeg.

6 Likes