Compressing Videos Easily on Windows w/ FFMPEG and Registry Files
Tags: productivity, tutorial, python, batch
Published: 2020-11-02 23:59:53

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](https://dev-to-uploads.s3.amazonaws.com/i/y820l09icakjhqrbrucw.png)

#### After Compression:

![After](https://dev-to-uploads.s3.amazonaws.com/i/3hzennnekr489w7d6ytm.png)

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
```

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.](https://phoenixnap.com/kb/how-to-install-python-3-windows#htoc-step-5-add-python-path-to-environment-variables-optional).

Next, install `ffmpeg`:
```
scoop install ffmpeg
```

**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:

```python
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])])
```

**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:

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

**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\""
```

**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](https://i.imgur.com/GwqdC8c.png)

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:

{% gist https://gist.github.com/M4cs/983f79573ccbd7c8dd6af86f30048a92 %}

### 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