FFmpeg and why I love you (#21)

I've used ffmpeg to clip videos for ages. It does the job fast, and lets me do my small clipping and editing quick and dirty. In this article you will find an updating list of use-cases I have found, and used.

These cases are for windows, that means that special cases like the "Do this to all files in this folder" where I use windows command line options will not work verbatim on linux.

Case 1: "I need to extract the two audio streams from this .mp4 file (Nvidia experience 2 stream audio) into *_local.aac and *_discord.aac"
Code: ffmpeg -hide_banner -i "filename.mp4" -vn -acodec copy -map 0:1 "filename_discord.aac" -map 0:2 "filename_local.aac"
By "llogan" at https://stackoverflow.com/questions/32922226/extract-every-audio-and-subtitles-from-a-video-with-ffmpeg/32925753#32925753

Case 2: "What about having ffmpeg do that to all the .mp4-files in a directory?"
Code: FOR /F "tokens=*" %G IN ('dir /b ".mp4"') DO ffmpeg -hide_banner -i "%G" -vn -acodec copy -map 0:1 "%~nG_computerAudio.aac" -map 0:2 "%~nG_LocalAudio.aac"
By "lxs" at https://stackoverflow.com/questions/5784661/how-do-you-convert-an-entire-directory-with-ffmpeg/24273691

the For-Command"I always remember there being a FOR command, and that you can use them to run through a list in CMD, I just never remember the exact command structure.

Case 3: "Now, maybe the audio setup is fine, and I only need to mix both streams together?"
Code: ffmpeg -hide_banner -i "filename" -vn -filter_complex "[0:1][0:2]amerge=inputs=2" "filename_mixedAudio.aac"
By "Chris K." at https://forum.videohelp.com/threads/355836-Audio-Stream-Mixing-in-FFMPEG

Case 4: "Can we combine the two previous processings with our 'Batch folder processing'?"
Code: FOR /F "tokens=*" %G IN ('dir /b ".mp4"') DO ffmpeg -hide_banner -i "%G" -vn -acodec copy -map 0:1 "%~nG_discordGame.aac" -map 0:2 "%~nG_local.aac" && ffmpeg -hide_banner -i "%G" -vn -filter_complex "[0:1][0:2]amerge=inputs=2" "%~nG_mixedAudio.aac"

Chaining commands on windows cmdYou can chain commands in Windows CMD by using && at the end of one command, we are doing that behind the first ffmpeg that strips out our two audio tracks. Honestly, we could've specified the two audio files just as easily as grabbing them from the file.

Case 5: "There's this clip where that one dude says something really funny, can we grab that audio?"
Code: ffmpeg -hide_banner -i "filename" -vn -acodec copy -map 0:1 -ss 00:05:50.5 -to 00:05:54.7 output.aac

-ss, and -to"The time after -ss is the start time in the video, the time after -to is when the clip is supposed to end

Case 6: "Can you grab this bit of video?"
Code: ffmpeg -hide_banner -i "filename.mp4" -ss hh:mm:ss -to hh:mm:ss -c copy output.mp4

-ss, and -to"The time after -ss is the start time in the video, the time after -to is when the clip is supposed to end, -c copy tells ffmpeg to copy all encoders

Case 7: "Can you make a GIF out of this bit of video?"
Code: ffmpeg -hide_banner -ss 0:18:59 -t 5.5 -i "filename.mkv" -f gif funnymoment.gif
By "Collin Burger" at https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/

-ss, -t, and -f" The time after -ss is the start time in the video, the number after -t is how long you want to clip in seconds, "-f gif" tells ffmpeg to convert it to a gif.

Case 8: "Hey, I have this .mp4 -video, can we turn that into .mp3?"
Code: ffmpeg -hide_banner -i "somevideo.mp4" somevideo.mp3

audio switches -ab bitrate' - Set the audio bitrate in bit/s (default = 64k).
Yes, it really is that easy ^,^

Case 9: "Can we get the pictures from this clip?"
Code: ffmpeg -hide_banner -i "C:\Applications\FFMPEG\aa.mp4" "frames/out-%03d.jpg"

Case 10: "I have a bunch of videos, can we make one video-file out of them all?"
Code: ffmpeg -hide_banner -i "concat:video1.avi|video2.avi|video3.avi" -c copy somevideo.mp4
- When attempted with .mkv/.mp4-files this method proved unsuccessful, see code block below.
Code: ffmpeg -hide_banner -f concat -safe 0 -i "filelist.txt" -c copy somevideo.mp4
Format for filelist.txt can be found here

Case 11: "What if I have this 1080p-video here, and I want to make it 720p?"
Code: ffmpeg -hide_banner -i MyMovie.mkv -vf scale=-1:720 -c:v libx265 -crf 0 -preset veryslow -c:a copy MyMovie_720p.mkv
Code: ffmpeg -hide_banner -i MyMovie.mkv -vf scale=-1:720 -c:v libx265 -crf 22 -preset veryslow -c:a copy MyMovie_720p.mkv
By "Nikola Dimitrijevic" at https://superuser.com/a/714835

-crf , -vf scale, -preset Now let's see what we have here: The scale video filter is for resizing the video. You just set one size – which is the height in this example – and use -1 for the other dimension. ffmpeg will recalculate the correct value automatically while preserving the aspect ratio. Quality controlled with the -crf option: The range of the quantizer scale is 0-51: where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28. Consider 22 to be visually lossless or nearly so: it should look the same or nearly the same as the input but it isn't technically lossless. The range is exponential, so increasing the CRF value +6 is roughly half the bitrate while -6 is roughly twice the bitrate. General usage is to choose the highest CRF value that still provides an acceptable quality. If the output looks good, then try a higher value and if it looks bad then choose a lower value. You can find more info in the x264 encoding guide. You control the tradeoff between video encoding speed and compression efficiency with the -preset options. Those are ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow. Default is medium. The veryslow option offers the best compression efficiency (resulting in a smaller file size for the same quality) but it is very slow – as the name says. The audio will be stream copied directly from the input file to the output file without any changes.

Case 12: "What if I want to convert a folder full of big (1080p+) files to 720 and .mp3?"
Code:

@echo off

if not exist "720p" mkdir 720p  2>nul
if not exist "mp3" mkdir mp3  2>nul

Echo "Stealing .mp3 audio from 1080p source, please wait."
echo.
FOR %%g IN ("*.mp4") DO (
	if not exist "mp3\%%~g.mp3" ffmpeg -hide_banner -i "%%~g" "mp3\%%~g.mp3"
)
echo "Encoding 720p video from 1080p source, please wait."
echo.
FOR %%g IN ("*.mp4") DO (
	if not exist "720p\%%~g" ffmpeg -hide_banner -i "%%~g" -vf scale=-1:720 -c:v libx264 -crf 18 -preset veryslow -c:a copy "720p\%%~g"
)
	

Note how this is formatted as a bunch of lines of code, this is what is called a .bat, or .cmd-file. I prefer .bat, but either will work. copy, and paste that into an empty text-file, then save it as something.bat and pick file-type "All" or something similar.
After you've done this, you'll end up with a file that you can run. It will go through the folder you're in, and convert all .mp4-files to both a .mp3-file, and a 720p .mp4-file. I run two for-loops because I found that the .mp3-creation runs along at 100-120x speed, meaning it'll be done with the .mp3s much much faster than the video conversions.

Case 13: "I have this video, I just wish the file size was smaller without compromising quality too much."
Code: ffmpeg -hide_banner -i input.mp4 -vcodec libx265 -crf 20 output.mp4
-crf is a number from 0 to 51 where 0 is lossless, and 51 is the most loss-full.

Case 14: "Can we make ffmpeg show less useless information during startup?"

Code: ffmpeg -hide_banner -help
-hide_banner -hide_banner will get rid of that massive banner, with all the compilation options and stuff

Case 15: "I have this video with a 60 fps framerate, what about changing that to 30?"
Code: ffmpeg -hide_banner -i input.mp4 -filter:v fps=30 output.mp4

Case 16: "So, I have this video, but I only want a part of it. Can we crop video X?"
Code: ffmpeg -hide_banner -ss 00:00:09 -i "X.mp4" -filter:v "crop=out_w:out_h:x:y" -vcodec libx265 -crf 30 output.mp4

crop=out_w:out_h:x:y" Stackexchange source
  • out_w is the width of the output rectangle
  • out_h is the height of the output rectangle
  • x and y specify the top left corner of the output rectangle

Case 17: "So, I have this video, and this audio. Can we replace the regular audio in the video?"
Code: ffmpeg -hide_banner -i Video.mp4 -i "BestMusic.mp3" -map 0:v -map 1:a -c:v copy -shortest output.mp4

-map, -shortest, -c:v copy" Stackexchange source
  • -map 0:v tells ffmpeg to use the first input as video
  • -map 1:a tells ffmpeg to use the second input as audio
  • -c:v copy makes ffmpeg copy the video
  • -shortest Makes ffmpeg use the shortest of the two files to cut the longest.

Case 18: "I like this video, but somebody screwed up their encode settings and included good subtitles. How do I extract subtitles from one video file, and splice them onto a different one? "
Code:
1: ffmpeg -hide_banner -i "badVideoGoodSubtitle.mkv" -map 0:s:0 subs.srt
2: ffmpeg -hide_banner -i "goodVideoNoSubtitle.mkv" -i subs.srt -c:v copy -c:a copy -c:s:0 srt -metadata:s:s:0 language:eng "goodVideoWithSubtitle.mkv"

-map, -c:v copy -c:a copy[ Superuser source 1 | Stackexchange source 2]

  • 1: -map 0:s:0 subs.srt makes ffmpeg extract subtitle:0 (first subtitle in file) to file subs.srt
  • 2: -c:a copy makes ffmpeg copy audio
  • 2: -c:v copy makes ffmpeg copy video
  • 2: -i subs.srt have to input subtitle file to get ffmpeg to do something with it.
  • 2: -c:s:0 srt -metadata:s:s:0 language:eng Define what stream (video-file wise) the subtitle will occupy, define the metadata of the language to be eng (Make sure to change this to whatever is correct)

Case 19: "I like this video, but the sound is atrocious, and it would be better without. How do I get rid of the sound? "

Code: ffmpeg -hide_banner -i "badSoundGoodVideo.mkv" -c copy -an "onlyVideoNoAudio.mkv"

Case 20: "I have all the frames of a video in image form, can I make that into a video file?"

Code: (unix) ffmpeg -hide_banner -framerate 30 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p out.mp4 [1]
Code: (Windows) ffmpeg -hide_banner -framerate 30 -pattern_type sequence -i %01d.png -c:v libx264 -pix_fmt yuv420p out.mp4[2]


  1. -pattern_type glob -i '*.png' - looks for pictures that is a .png. ↩︎

  2. -pattern_type sequence -i %01d.png - looks for pictures that start with 1 digit and is a .png. If you need more than one digit, change the 1-number, to however many digits you need. ↩︎

Case 21: "Can I encode this file to the new av1 codec?"

Code 1: ffmpeg -hide_banner -i "bigFile.avi" -vcodec libsvtav1 -crf 35 "av1_encoded_file.mp4" [1] [2] [3]
Code 2: ffmpeg -hide_banner -i "bigFile.avi" -vcodec libaom-av1 -crf 35 "av1_encoded_file.mp4"


  1. Information from ffmpeg.org docs: https://trac.ffmpeg.org/wiki/Encode/AV1 ↩︎

  2. The libstav1 codec was really fast on my computer, if this is not the case, you can try using libaom-av1 - there are differences in encoding as described in ↩︎

  3. I found av1 to be very efficient in encoding, so a crf 35 was completely fine for my uses. ↩︎

Subscribe to Stories and Articles

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe