FFMPEG Tips and Tricks - Questions and suggestions about this video editing tool

  • 🐕 I am attempting to get the site runnning as fast as possible. If you are experiencing slow page load times, please report it.
If I'm not mistake youtube-dl does not currently have an option to download a section of a video without downloading the entire video first, however ffmpeg allowed me to fix that problem:

Use youtube-dl to get the actual URL of the video first:
youtube-dl --get-url -f best --no-cache "<Video URL>"
Parameters:
  • The -g tells youtube-dl to get the actual URL for the video​
  • The -f specifies the format you can change this to whatever you want but I chose best here​
  • The --no-cache parameter means youtube-dl will request a new URL instead of relying on a cached one​

Paste the URL you got from the above command into this ffmpeg command:
ffmpeg -ss <Start Time> -t <End Time> -i <URL From youtube-dl> out.mp4
Parameters:
  • The -ss parameter is the time you want to start downloading the video at for example 00:00:30 starts at 30 seconds
  • The -t parameter is the time you want to stop downloading the video at for example 00:01:40 stops downloading at 1 minute and 40 seconds
  • The -i parameter it the URL you got from youtube-dl from the first command
 
Last edited:
This command encodes a video with good quality, using slower preset to achieve better compression:
ffmpeg -i input.avi -c:v libx264 -preset slow -crf 22 -c:a copy output.mkv
Note that in this example the audio stream of the input file is simply stream copied over to the output and not re-encoded.

Convert with Dual Audio support
ffmpeg -i input.avi -c:v libx264 -preset slow -crf 22 -map 0:0 -map 0:1 -map 0:2 -c:a copy output.mp4
 
Last edited:
Download Youtube videos with automatically generated subtitle files.
youtube-dl --write-auto-sub --sub-lang en '<URL From youtube-dl>'

Download all subtitle files of YouTube videos . (skip-download)
youtube-dl --all-subs --write-auto-sub --skip-download <URL From youtube-dl>
 
Last edited:
Well, if we include youtube-dl too, I got another. I have my defaults set to this:

youtube-dl -o '%(upload_date)s %(title)s.%(ext)s' --write-description --write-thumbnail <Video URL>

-o sets the filename, which is set to the upload date and then the title of the video. And then a tag for writing the description of the video to a txt file and saving the default thumbnail. This is all specifically for youtube videos. If I'm grabbing something from a site that doesn't have an upload date, for example, it just writes"NA" in the filename and won't save anything for description or thumbnail if not present. So it doesn't fuck up anything for non-youtube sites.

The -o parameters apply to the description and thumbnail filename as well.

The upload date has a bit of a quirk. If it was a live stream that went into the next day, the upload date will be the date the stream ended and the video became an upload, instead of the date the stream started.
 
did youtube-dl ever get downloading on twitch for content you needed a subscription for? It used to let you feed in a login/pass but then twitch put in some hidden captcha thing
 
did youtube-dl ever get downloading on twitch for content you needed a subscription for? It used to let you feed in a login/pass but then twitch put in some hidden captcha thing
-u, --username USERNAME
-p, --password PASSWORD
Login with this account ID Account password. If this option is left out, youtube-dl will ask interactively.

-2, --twofactor TWOFACTOR
-n, --netrc
Two-factor authentication code Use .netrc authentication data

--video-password PASSWORD
Video password (vimeo, smotri, youku)

You may also want to configure automatic credentials storage for extractors that support authentication (by providing login and password with --username and --password) in order not to pass credentials as command line arguments on every youtube-dl execution and prevent tracking plain text passwords in the shell command history. You can achieve this using a .netrc file on a per extractor basis. For that you will need to create a .netrc file in your $HOME and restrict permissions to read/write by only you:

touch $HOME/.netrc
chmod a-rwx,u+rw $HOME/.netrc

After that you can add credentials for an extractor in the following format, where extractor is the name of the extractor in lowercase:
machine <extractor> login <login> password <password>

For example:
machine youtube login myaccount@gmail.com password my_youtube_password
machine twitch login my_twitch_account_name password my_twitch_password

To activate authentication with the .netrc file you should pass --netrc to youtube-dl or place it in the configuration file.
 
Last edited:
Paste the URL you got from the above command into this ffmpeg command:
ffmpeg -ss <Start Time> -t <End Time> -i <URL From youtube-dl> out.mp4
Parameters:
  • The -ss parameter is the time you want to start downloading the video at for example 00:00:30 starts at 30 seconds
  • The -t parameter is the time you want to stop downloading the video at for example 00:01:40 stops downloading at 1 minute and 40 seconds
  • The -i parameter it the URL you got from youtube-dl from the first command
Yep! Just a slight thing. Specifying -t gives the duration for the clip, not the end time. For the end time you want -to instead.
 
Capturing your Desktop / Screen Recording

Use the x11grab device:

ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i :0.0+100,200 output.mp4

This will grab the image from desktop, starting with the upper-left corner at x=100, y=200 with a width and height of 1024⨉768.

If you need audio too, you can use ALSA (see Capture/ALSA for more info):

ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i :0.0+100,200 -f alsa -ac 2 -i hw:0 output.mkv

Or the PULSE input device:

ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i :0.0+100,200 -f pulse -ac 2 -i default output.mkv

Cntrl+C to Exit and Save

Select recording area

ffmpeg -f [audio-driver] -i default -f x11grab -s [resolution] -i :0.0+[point-X],[point-Y] -r 25 -vcodec libx264 output.mkv

ffmpeg -f alsa -i default -f x11grab -s 720x480 -i :0.0+180,120 -r 25 -vcodec libx264 output.mkv
 
Last edited:
  • Informative
Reactions: ScatmansWorld
If I'm not mistake youtube-dl does not currently have an option to download a section of a video without downloading the entire video first, however ffmpeg allowed me to fix that problem:

Use youtube-dl to get the actual URL of the video first:
youtube-dl --get-url -f best --no-cache "<Video URL>"
Parameters:
  • The -g tells youtube-dl to get the actual URL for the video​
  • The -f specifies the format you can change this to whatever you want but I chose best here​
  • The --no-cache parameter means youtube-dl will request a new URL instead of relying on a cached one​

Paste the URL you got from the above command into this ffmpeg command:
ffmpeg -ss <Start Time> -t <End Time> -i <URL From youtube-dl> out.mp4
Parameters:
  • The -ss parameter is the time you want to start downloading the video at for example 00:00:30 starts at 30 seconds
  • The -t parameter is the time you want to stop downloading the video at for example 00:01:40 stops downloading at 1 minute and 40 seconds
  • The -i parameter it the URL you got from youtube-dl from the first command

That seems like it would encode the video again and youtube is already ass-quality. Start and end the cut on key frames instead to keep it lossless.

Probably pretty basic, but the only ffmpeg command I have saved is to combine multiple mp4's into one.

ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4
If you don't set -safe 0, it'll find reasons to not work.
list.txt is just a list of the mp4s to be joined:
file 'path/to/file.ext'

Avidemux is a very nice GUI for ffmpeg, use the append option to add shit together with any extra encoding, it writes the streams into a new container and enables you to play and preview the video before committing to anything.

edit: or the reverse, pull segments out without encoding them again.
 
Last edited:
Re-encode original with varying CRF values

ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -qphist -tune stillimage crfimage.mp4
ffmpeg -i input.mp4 -c:v libx264 -crf 23 crf23.mp4
ffmpeg -i input.mp4 -vcodec h264 -acodec mp2 output.mp4
ffmpeg -i input.mp4 -c:v libx264 -preset fast -crf 18 crf18fast.mp4
ffmpeg -i input.mp4 -c:v libx264 -b:v 500k 500knoaudioption.mp4
ffmpeg -i input.mp4 -r 25 framerate25fps.mp4
ffmpeg -i input.mp4 -b:v 64k -bufsize 64k videobitrate64kbits.mp4
ffmpeg -i input.mp4 -c:v libx264 -crf 28 crf28.mp4
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -acodec libmp3lame -b:a 16k -ac 1 -ar 16000 crf2811.mp4
ffmpeg -i input.mp4 -c:v libx264 -crf 26 -acodec libmp3lame -b:a 24k -ac 2 -ar 24000 crf2611.mp4

comp. qualite size remain artifacts

original 100.0% ----- 19.16 GB ---------- uncompress
crf=14 ------------- 15.7% 3.00 GB
crf=18 ------------- 10.1% 1.98 GB
crf=24 ------------- 5.1% 0.98 GB
crf=28 ------------- 3.5% 0.68 GB
crf=32 ------------- 2.4% 0.47 GB ----- minimal
crf=38 ------------- 1.9% 0.36 GB ----- usable
crf=46 ------------- 1.6% 0.30 GB ----- bad
crf=51 ------------- 1.4% 0.28 GB ----- unusable

You can use -crf 0 to encode a lossless output. Two useful presets for this are ultrafast or veryslow since either a fast encoding speed or best compression are usually the most important factors.

Lossless Example (fastest encoding)
ffmpeg -i input -c:v libx264 -preset ultrafast -crf 0 output.mkv

Lossless Example (best compression)
ffmpeg -i input -c:v libx264 -preset veryslow -crf 0 output.mkv
 
  • Informative
Reactions: Rozzy
Cropping a video.

ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4

Making a gif from video, in the example below it makes an animated gif starting at 5.25 seconds of a 3 second duration, 15fps, 640 pixels horizontal resolution.

ffmpeg -ss 00:00:05.25 -t 00:00:03 -i input.mp4 -vf "fps=15,scale=640:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif

Converting an animated gif to mp4 video which reduces the filesize to a fraction of that of a gif.

ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4

Add audio track to video.

ffmpeg -i input.mp4 -i input.mp3 -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k output.mp4

Add an overlay to a video, in the example below it adds overlay.png to the video starting at top left position 00,00 and from 0 seconds for a duration of 180 seconds.

ffmpeg -i input.mp4 -i overlay.png -filter_complex "[0:v][1:v] overlay=00:00:enable='between(t,0,180)'" -pix_fmt yuv420p -c:a copy output.mp4
 
Converting an animated gif to mp4 video which reduces the filesize to a fraction of that of a gif.

ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4
Unless...


Extracting the audio track without compressing it again.
ffmpeg -i [input filename / Path] -vn -c:a copy [output filename / Path]
 
  • Like
Reactions: Master Troll
Koala.gif is 23.4 MB
Koala.mp4 is 1.7 MB

Massive savings in bandwidth.

ffmpeg -i koala.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" koala.mp4

View attachment 1874141
Your settings work way better than mine, so thanks! What I meant with "unless..." was that heavily dithered gifs can make the mp4 larger than the gif.

I don't remember what gifs I've had trouble with in the past that went from 12MB to 30MB, so I just googled "dithered gif" and picked this one and used your settings.

dither1.gif
1.85MB
dither1.gif


dither1.mp4
2.95MB - it's much smaller than it would be if I used my settings but the dithering still messes things up.
 
  • Informative
Reactions: Rozzy
Back