how to use ffmpeg to give videos a constant frame rate without (much) quality loss
I don't know who this will benefit, but I've been using Kdenlive as my video editor, and I love it! but I keep throwing variable frame rate vids at it, and it Does Not Like that.
or, more accurately, when I give it something VFR, it decides that it needs to transcode it into a constant frame rate, and then processes the file into something that is Unreasonably Large with a Very Stupid Bitrate.
like, take for example this JJK clip (from when I made a fanvid, dw about it):
baby. baby why are you 615 megabytes. babygirl why are you 19115kbps when you were barely 4000kbps before ??? you are a waste of space; there's no way you need to be that huge!!
and so being who I am and knowing that I can do better than that, I poked at ffmpeg until I could transcode the files myself so I don't waste multiple GB of hard drive space trying to make fanvids.
ffmpeg -i "original video.mp4" -vcodec libx264 -acodec copy -filter:v fps=fps=source_fps -crf 23 "transcoded video.mp4"
that's it. it keeps the audio codec the same, sets the frame rate to constant at the same fps as the source, and for this video, ends up with about the same bitrate.
see? that shit didn't need to be 615MB, it's fine being pretty much the same size and bitrate! what the fuck kdenlive! also...
if you're not just converting individual video clips, but cutting bits out of a longer video, do this:
ffmpeg -i "long video.mkv" -vcodec libx264 -acodec copy -ss 00:18:16.0 -to 00:19:32.0 -filter:v fps=fps=source_fps -crf 18 "video clip.mp4"
set the timestamps after the -ss to where you want the video clip to start and end (HH:MM:SS), and you should be good!
you may notice that in this one my -crf number is 18 instead of 23 - those numbers are to set the quality/bitrate of the output video, and for the clip I tested this on, 18 got me closer to the original bitrate, while 23 wasn't a high enough quality.
the quality/bitrate of the file goes up as the crf numbers go down. 0 is lossless, but in libx264, 18 is considered visually lossless, basically indistinguishable from the original unless you're a computer or an archivist. so feel free to mess with that number to get the bitrate and/or visual quality you desire!
(you can do this to .mp4s and .mkvs without problems btw, and I haven't tested it with other video containers but tbh? it'll probably be fine so long as your output is mp4. ffmpeg rocks!)
anyway I hope this helps someone else who, like me, gets annoyed at wasted hard drive space and no-reason file bloating ✨
(and yes, I know Kdenlive does have settings you can change for transcoding video - but they don't tell me much, and I am picky when it comes to stuff like this. also everything I tested still ended up way bigger than the original, so?)