DEV Community

Cover image for Compressing Videos Easily on Windows w/ FFMPEG and Registry Files
Max Bridgland
Max Bridgland

Posted on

Compressing Videos Easily on Windows w/ FFMPEG and Registry Files

I am always sharing clips and videos with my friends and often times the file size is ridiculously large to send over most social media (Discord, Twitter, Telegram, etc). To get around these massive file sizes, I use ffmpeg to compress the video. FFMPEG is a super powerful tool for all your media processing. It has a ton of libraries for handling different tasks involving visual and audio media.

Preface

Using ffmpeg we are able to compress videos down by ~10x. For example, if we have a 1 minute and 15 second clip that is 1080p 60fps @ 432Mb, we can use a simple one-line ffmpeg command to compress this video down to ~50Mb. This now allows us to send it on a lot of different platforms way easier.

Before Compression:

Before

After Compression:

After

All the while, we lose little to no quality in the video. This is a great solution but it can be a serious hassle to have to open your terminal every time you want to compress these videos. I thought it would be easier to simply make a right-click menu item to quickly compress videos. I've tested it with m4v, mkv, and mp4 videos and it works flawlessly.

Normally you could just run:

ffmpeg -i INPUT_FILE.mp4 -vcodec h264 -acodec aac OUTPUT.mp4
Enter fullscreen mode Exit fullscreen mode

but nobody wants to have to type this out every time. Since I already had an FFMPEG python compression script laying around I used that as the main program to be run.

Getting Setup

You need a couple of tools to start. First, get Python 3 (preferably above 3.5). Make sure you have Python3 installed to your PATH as well. See how here..

Next, install ffmpeg:

scoop install ffmpeg
Enter fullscreen mode Exit fullscreen mode

Make sure your scoop shims are in your PATH! (C:\Users<YOU>
scoop\shims is the default path)

Now you're ready to start compressing!

Making our script and right-click registry file

compress.py

Starting out you can make a compress.py file and place it any where. I placed it in C:\Users\Max\ffmpeg-compress\compress.py. The contents of that file are as follows:

import sys
from subprocess import run, PIPE
from pathlib import Path

args = sys.argv[1:]
video_file = Path(' '.join(args))
run(['ffmpeg', '-i', video_file.name, '-vcodec', 'h264', '-acodec','aac', video_file.name.replace('.' + video_file.name.split('.')[-1], '-compressed.' + video_file.name.split('.')[-1])])
Enter fullscreen mode Exit fullscreen mode

Going line by line:

1-3: Importing our modules. sys to get our arguments, run and PIPE from subprocess in order to call our command and get the output, and Path from pathlib to parse and clean up our path strings.

4: args is a list of arguments passed when calling the executable (this will be our filename)

5: video_file is a Path object with the args passed joined as a string

6: run([...]) is where we call the ffmpeg executable from Python and run our command to compress.

compress.bat

Next, you need to make a .bat file anywhere on your computer. I placed mine @ C:\Users\Max\compress.bat. The contents of that file are:

@echo OFF
SET subject=%*
for %%a in (%*) do set subject=%%a
python C:\\Users\\Max\\ffmpeg-compress\\compress.py %subject%
Enter fullscreen mode Exit fullscreen mode

Going line by line:

1: @echo OFF disables the printing of each line in the batch script to the terminal. A standard in batch scripts.

2-3: Will set all of our arguments to a variable subject

4: Calls our python script with the video as our argument.

ffmpeg-compress.reg

Finally, we need to make a file called ffmpeg-compress.reg anywhere. We can use Notepad to do so. In this file, paste the following but replace the filepath for compress.bat with yours.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\ffmpeg-compress]
@="Compress File w/ FFMPEG"

[HKEY_CLASSES_ROOT\*\shell\ffmpeg-compress\command]
@="\"C:\\Users\\Max\\compress.bat\" \"%1\""
Enter fullscreen mode Exit fullscreen mode

Going section by section:

For [HKEY_CLASSES_ROOT\*\shell\ffmpeg-compress], we are setting the title of our right-click menu button.

For [HKEY_CLASSES_ROOT\*\shell\ffmpeg-compress\command], we are setting the actual command string we want the button to run when clicked.

Conclusion

Now when we right-click on a file, we have the option to Compress w/ FFMPEG

Preview

This will now allows us to easily compress videos without having to open the terminal.

A gist is available with the files used in this here:

Why are you using Python? Couldn't you do this in pure batch?

I suck with batch and I really don't like having to deal with weirdness passing args and parsing them. Python is just a more comfortable language for me. I'm also able to do string replacing and what not easier, and I already had the Python file

Top comments (2)

Collapse
 
wavewater profile image
wavewater

Nice - you wrote a super concise tutorial and it works perfectly. Please Keep posting!

Collapse
 
benoitbdah profile image
Benoit Bahiminin Dah • Edited

You also suck with Python - not just Batch. You could have done it in Python without any Batch. :-)

But thank you for the post! It was an interesting introduction to ffmpeg for dev.