Join the conversation

Join the community of Machine Learners and AI enthusiasts.

Sign Up
samihalawaΒ 
posted an update Mar 26
Post
2443
πŸ”₯ ULTRA VIDEO COMPRESSION (300MB β†’ 3MB!)
ffmpeg -i input.mp4 -vcodec libx264 -crf 28 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -y output.mp4

-i β†’ Input ⚑️ -vcodec libx264 β†’ H.264 codec ⚑️ -crf 28 β†’ Compression (lower = better quality) ⚑️-vf pad=... β†’ Even dimensions ⚑️ -y β†’ Overwrite

I have tried it on 72 MB video:

ffmpeg -y -hwaccel cuda -i /home/data1/protected/Media/Pictures/Pictures/Year-2025/03/2025-03-13/VID_20250313_154150.mp4 -vcodec libx264 -crf 28 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" output.mp4

and I included -hwaccel cuda which sped up probably.

Result is 17M video and I see nothing strange as human. Looks same, it means compression is really good. Though it wasn't 94%, rather 77%.

Different ffmpeg formula

I have a bit different conversion formula, for videos on websites, I follow Google guideliness and compress it to webm:

(defun video2webm-dired ()
  "Converts any video to webm"
  (interactive)
  (let* ((bitrate (read-number "Bitrate: " 500))
     (videos (dired-get-marked-files))
     (videos (mapcar 'video-mime-type-p videos))
     (videos (seq-remove 'null videos))
     (async-shell-command-buffer 'new-buffer)
     (command (format "ffmpeg -y -hwaccel cuda -i `?` -c:v libvpx-vp9 -b:v %sk -pass 1 -passlogfile `?` -speed 4 -c:a libopus -f webm /dev/null -async 1 -vsync passthrough && ffmpeg -y -hwaccel cuda -i `?` -c:v libvpx-vp9 -b:v %sk -pass 2 -passlogfile `?` -speed 1 -c:a libopus \`?`.webm -async 1 -vsync passthrough && rm `?`-0.log;" bitrate bitrate)))
    (dired-do-async-shell-command command nil videos)))

that however, takes much longer time than your formula. I have now converted to 1000 bit rate, and I have got result of 5.4 M for same video.

That is like 92.5%

I can't see as human any visible differences with that bit rate.

Better to 500 bit rate

With 500 bit rate, I have got 2.9M from 72M, that is fantastic. It took longer, but compression is so much better.

I have achieved basically 96% which is more than you! πŸ˜›

Β·

Hey! πŸ‘‹ Just gave it a shot!

You know, your approach is actually really similar to how they do things with ffmpeg.wasm. It's what you use when you absolutely have to do the encoding right there in the user's browser, like with those JavaScript video or Lottie editors. (I've used it on projects like that, and the compression you get is seriously good!) πŸ‘

((You're totally right about needing those extra bits and pieces though, and yeah, WebP isn't exactly everyone's favorite format straight out of the box. πŸ€”)

Still, that's a really smart point! Definitely saving this little nugget. πŸ’Ύ

Thanks so much for sharing! 🀝❀️

I just got 400 to 300 mb :(

Amazing, thank you very much. I created a Space for everyone to use easily.
https://huggingface.co/spaces/eienmojiki/video-compression

Β·

Thank you my boy!!