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

Any of you have good ffmpeg settings for stream clips to keep bandwith down (libx264)? I was clipping a 20 min 540p30 clip and could barely get it under 100mb with ass visual quality. I'll try a slower preset next time.

Also, just want to spread the word since we are talking about youtube-dl and ways to download streams and vods
Bash:
streamlink --twitch-disable-hosting --twitch-disable-ads --ffmpeg-copyts --ffmpeg-start-at-zero -o "FILENAMEOUT" TWITCH_URL_HERE 720p60,best[or just best, or other size]
I use streamlink to download twitch streams with ads with no issues.
 
Any of you have good ffmpeg settings for stream clips to keep bandwith down (libx264)? I was clipping a 20 min 540p30 clip and could barely get it under 100mb with ass visual quality. I'll try a slower preset next time.
A really greasy trick is to drop the frame rate to 23.976. Depending on the video most people won't notice(so don't tell them), we're so used to that framerate. Of course, if there's something important in each individual frame, then don't do it. Then there's the audio, it might be a talking head video using 160kbps AAC in stereo. It will sound exactly the same at 96kbps or even 64kbps mono. AAC is good so maybe give 48kbps a listen(extract the audio track and recompress it separately to find how much small it can get). Always consider mono! When you start going down from 500-1000MB to under 100MB the size of the audio will be felt, 13 megs of audio on a half-gig video is nothing but that's 13% at 100MB.
 
Last edited:
Double post to make this into a second post filled with stupidity:

It seems possible, it should be possble, but I can't find a vay to control flag and control the variable bitrate for different sections manually. It's gives thee 10 seconds 187kbps and the next 7 270kbps, it's very smart in how it does that but... but to get down into a Kiwi video the first 10 seconds only need 110kbs and the next 7 340kbps. I swear there were tools to mark this up in the past but that could have been during the mastering DVDs era or of mastering blue-rays. This have been my holy grail for a while and I have had some success and some blowbacks in developing a repeatable procedure to get it done. Right now I'm approaching a triple layer turdburger that might give fantastic results at small file sizes while also removing some sanity and humanity from the user, but it should be reproducible so a guide can be written.
 
Last edited:
  • Informative
Reactions: Rozzy
Using FFMPEG to make a video with just a single image.

ffmpeg -loop 1 -i image.png -c:v libx264 -t 74 -pix_fmt yuv420p -vf scale=1920:1080 video.mp4

The above example creates 74 second long video.mp4 from image.png.

Using FFMPEG to add sound/music to a video.

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

The above example combines video.mp4 and audio.mp3 creating output.mp4. It helps if both are of the same duration.
 
Using FFMPEG to make a video with just a single image.

ffmpeg -loop 1 -i image.png -c:v libx264 -t 74 -pix_fmt yuv420p -vf scale=1920:1080 video.mp4
I'd advise you to add -tune:v stillimage to alter the video codec preset. I bet it doesn't make a big difference but it's available.

Here's the command I use to make the classic music over a still image video (assume that your image is already 1920x1080 size):
Bash:
ffmpeg -loop 1 -i ${image} -i ${audio} -pix_fmt yuv420p -tune:v stillimage -preset superfast -t ${time} -b:a 512k ${out}
Sometimes ffmpeg acts weird when you use the -shortest tag and leaves a few seconds of silence after the audio ends so i recommend to add -t for the length you want.
 
Cutting a segment from an mp4 video.

ffmpeg -ss 00:00:20 -i input.mp4 -c copy -t 00:01:30 output.mp4

Alternative.

ffmpeg -i input.mp4 -ss 00:00:20 -t 00:01:30 -async 1 -strict -2 output.mp4

In the above examples it cuts input.mp4 at 20 seconds for a duration of 1 minute 30 seconds, creating output.mp4

Cutting a segment from an mp3 music file.

ffmpeg -ss 35 -i input.mp3 -t 120 -c copy output.mp3

In the above example it cuts input.mp3 at 35 seconds for a duration of 120 seconds, creating output.mp3

Confirmed that both methods work for me using the stated file types, other file types can give mixed results or not work at all.
 
Cutting a segment from an mp4 video.

ffmpeg -ss 00:00:20 -i input.mp4 -c copy -t 00:01:30 output.mp4

Alternative.

ffmpeg -i input.mp4 -ss 00:00:20 -t 00:01:30 -async 1 -strict -2 output.mp4

In the above examples it cuts input.mp4 at 20 seconds for a duration of 1 minute 30 seconds, creating output.mp4
Another tweak on that is
ffmpeg -ss 00:00:53 -to 00:02:30 -i input.mp4 -c copy output.mp4

It copies from 00:53 to 02:30 instead of 2½ minutes starting from 00:53, for a total of 1 minute 43 seconds which is obviously not correct and with lossless cutting this is something to take into consideration. The last keyframe was at 00:46 and the next one is at 00:54 while I want to start at 00:53(I obviously rigged it like this), so FFmpeg uses the previous one to avoid cutting something from the desired time range. This can be confusing and frustrating when some clips come out exactly like you want while others don't match up to the time you specified.

Youtube videos and streaming in general can be pretty frugal with their keyframes and Rozzy's alternative solution is the one you want for exact cuts, but it requires re-encoding and if the source is crap it won't come out looking prettier.
One tweak to that would be cutting and copying the original audio without re-encoding it, if possible. I tried and immediately gave up.
 
To put together an audio file over a still image (CD cover art) without re-encoding the audio for the purposes of sharing, here's what I used:

Code:
ffmpeg -loop 1 -i album_cover.jpg -i song.mp3 -filter:v scale=320:-1 -c:v libx264 -crf 28 -preset veryslow -tune stillimage -c:a copy -pix_fmt yuv420p -shortest output.mp4

Using CRF with the stillimage tune was the trick to get the file size close to only the size of the audio + image. Depending on complexity of the art the value will have to be toyed with until you get it right. For art with flat shading and a few colors, CRF 32 worked fine. For art that's based in a photograph, I had to bring it down to 28 to get it to look acceptable, at the cost of an additional 300kb.

For the pixel format, I followed the advice of ffmpeg - yuv420p allegedly has better compatibility with older devices and software.

You can see the results here.
 
  • Like
Reactions: Rozzy
Lightweight youtube video player (doesn't show ads too). Requires youtube-dl which can be installed with pip or any repository manager like apt (although in my experience the youtube-dl in apt is an old version...and doesn't include the "-g" feature unfortunately... so I would recommend pip or installing from the website.)

Other thing, this is a unix shell script, so I apologize to all the windows users out there, but you can probably figure out how to make your own executable using these commands.

Bash:
#!/bin/sh

# Use:
# ./this_script <youtube_vid_link>
#  OR
# ./this_script <youtube_vid_link> <format_code>
# Hint: run `youtube-dl -F <youtube_vid_link> to see available formats`

qual="$2"
if [ $# == 1 ]
then
    qual="best"
fi

url=`youtube-dl -f "$qual" -g "$1"`
echo $url
ffplay -infbuf -autoexit "$url"
 
  • Informative
Reactions: Coffee Shits
Lightweight youtube video player (doesn't show ads too). Requires youtube-dl which can be installed with pip or any repository manager like apt (although in my experience the youtube-dl in apt is an old version...and doesn't include the "-g" feature unfortunately... so I would recommend pip or installing from the website.)

Other thing, this is a unix shell script, so I apologize to all the windows users out there, but you can probably figure out how to make your own executable using these commands.

Bash:
#!/bin/sh

# Use:
# ./this_script <youtube_vid_link>
#  OR
# ./this_script <youtube_vid_link> <format_code>
# Hint: run `youtube-dl -F <youtube_vid_link> to see available formats`

qual="$2"
if [ $# == 1 ]
then
    qual="best"
fi

url=`youtube-dl -f "$qual" -g "$1"`
echo $url
ffplay -infbuf -autoexit "$url"
yeah but what about MPV
 
To put together an audio file over a still image (CD cover art) without re-encoding the audio for the purposes of sharing, here's what I used:

Code:
ffmpeg -loop 1 -i album_cover.jpg -i song.mp3 -filter:v scale=320:-1 -c:v libx264 -crf 28 -preset veryslow -tune stillimage -c:a copy -pix_fmt yuv420p -shortest output.mp4

Using CRF with the stillimage tune was the trick to get the file size close to only the size of the audio + image. Depending on complexity of the art the value will have to be toyed with until you get it right. For art with flat shading and a few colors, CRF 32 worked fine. For art that's based in a photograph, I had to bring it down to 28 to get it to look acceptable, at the cost of an additional 300kb.

For the pixel format, I followed the advice of ffmpeg - yuv420p allegedly has better compatibility with older devices and software.

You can see the results here.
I tried it and the output file size was less than the original size of the mp3.

Don't know if I got this exactly right but I modified your method to output with aac audio.

ffmpeg -loop 1 -i album-art.jpg -i song.mp3 -filter:v scale=320:-1 -c:v libx264 -crf 28 -preset veryslow -tune stillimage -c:a aac -b:a 128k -pix_fmt yuv420p -shortest output.mp4
 
I tried it and the output file size was less than the original size of the mp3.

Don't know if I got this exactly right but I modified your method to output with aac audio.

ffmpeg -loop 1 -i album-art.jpg -i song.mp3 -filter:v scale=320:-1 -c:v libx264 -crf 28 -preset veryslow -tune stillimage -c:a aac -b:a 128k -pix_fmt yuv420p -shortest output.mp4
I would guess it's the lossy-to-lossy mp3->AAC encoding that's reducing the file size. It works if you're under a strict filesize limit, but audio quality suffers as a result.

It would be interesting to see how it stacks up against a -c:a copy of a mp3 v2, or transcoded from lossless like FLAC->AAC.
 
yeah but what about MPV
never heard of mpv before, just vlc and ffplay.

Here's a quick command to get a lossless screenshot of a video at any point.
Bash:
ffmpeg -loglevel panic -ss $TIME -i $INPUT -frames:v 1 -an "$OUT.png"
 
  • Informative
Reactions: Rozzy
Decent file size calculator if you don't want to do the math yourself.

A tip if you're going to shrink a 2+ hours movie that's 20 gigs in size, or a bluray, splice out a 5-10 minute long section with more movement and re-encode that with the settings/bitrate you're thinking about using. See how it looks.
 
is there a particular magic word to add about ten seconds of dead air to a video as you mash it down to toaster-style?
I suspect the easiest solution will be to merge ten seconds of dead air to the target video on the frontside but I figure it's worth checking
 
  • Thunk-Provoking
Reactions: Smaug's Smokey Hole
Sometimes when archiving YT videos I like encode them in av1 to save space. It takes a long time, so I let the command run in the background. The benefit is smaller video sizes with no appearant loss in quality, which makes it easy to back up.

ffmpeg -i (old vid name) -c:v libaom-av1 -crf 30 -b:v 0 (new vid name)

Sometimes when I want to save streams I may use this command to do it, but Im not sure if its worth it at the quality it producesL

ffmpeg -i (old video name) -c:v libx265 -crf 51 (new video name)
 
  • Like
Reactions: ditto
It should always be noted that AviDemux and Handbrake are frontends for most ffmpeg commands that you would want to use. Except oddities like @Rozzy's excellent gif-to-mp4 command mentioned earlier.
 
is there a particular magic word to add about ten seconds of dead air to a video as you mash it down to toaster-style?
I suspect the easiest solution will be to merge ten seconds of dead air to the target video on the frontside but I figure it's worth checking
Try the concat filter with anullsrc as an audio source. I didn't have any luck with the examples I found online but my ffmpeg version is old. The second link has an example using filter_complex with concat, that might do what you want with some fiddling.
 
Back