DEV Community

lia
lia

Posted on

How to encode files preserving folder hierarchy

Using the Handbrake GUI can be amazing to encode all types of files, but it's hard to encode files inside their own original folders. You need to manually change the output path one by one.

So, this post will feature a CLI (command line interface) which uses the Handbrake CLI executable to encode all files inside a particular folder, preserving all folder hierarchy!

The main purpose of this CLI was to reduce files' sizes to occupy the least amount of space in the disk, but with the custom command feature, any Handbrake command can be used only requiring specific placeholders regarding the original file path and the encoded file path.

This CLI tool will also create a final output file containing statistics about the files encoded, including:

  • which files were encoded successfully and not successfully;
  • number of files encoded successfully and not successfully;
  • original file size;
  • encoded file size.

Installing

You will need Python installed. You can do that here.

For this CLI to work, the HandBrake CLI version must be installed. This installation can be found in this link under 'Downloads->Other->Command Line Version'

The CLI can be downloaded from here and the GitHub repository can be found here.

pip install havc
Enter fullscreen mode Exit fullscreen mode

To check if the program was successfully installed, you can run the following command in the console:

havc -h
Enter fullscreen mode Exit fullscreen mode

How to

For anymore information please have a look at this file.

Configuration

Initially, you will have to configure:

  • (-r) where your Handbrake CLI executable is located;
  • (-c) the folder which contains files to encode
  • (-e) as many file extensions you want to be encoded into another extension;
  • (-t) the target extension.

Like this:

havc -r "C:\Users\<username>\Desktop\HandBrakeCLI.exe" -c "C:\Users\<username>\Desktop\folder to convert" -e mp4 mkv -t m4v
Enter fullscreen mode Exit fullscreen mode

Important
Note that the target extension cannot be the same as any extension searched for.

After this, an external configuration file will be configured and then the following command becomes valid (always using the saved configurations):

havc
Enter fullscreen mode Exit fullscreen mode

This means that instead of always changing the folder to convert, you can have a specific folder for this purpose and then you don't have to reconfigure (at least the folder).

Update configuration

If any argument has to be modified just run the command with the necessary argument.
For instance, if the extensions to search for have to be modified, you simply run:

havc -e mov .avi mp4
Enter fullscreen mode Exit fullscreen mode

Original Files

All original files will be moved to a different folder called 'TO-DELETE' by default. This name can also be change for something else by doing:

havc -d 'AnotherNameForFolder'
Enter fullscreen mode Exit fullscreen mode

This folder will contain all the original files, and it's main purpose is to save all those files just in case any encoding goes wrong.
If you think everything's OK then this folder can be deleted.

Custom Handbrake command

By default, this CLI will run the following basic HandBrake CLI command:

HandBrakeCLI.exe --preset Very Fast 1080p30 -i "C:\Users\<username>\Desktop\original-file\originalFile" -o "C:\Users\<username>\Desktop\output-file\outputFile"
Enter fullscreen mode Exit fullscreen mode

But you can also input a custom command using specific placeholders replacing both the original file and the output file, like this:

havc -cc "--preset Fast 720p30 -i {of} -o {cf}"
Enter fullscreen mode Exit fullscreen mode
  • 'of' stands for 'original file';
  • 'cf' stands for 'converted file'.

Inside the program, {of} and {cf} will be replaced for the path of each file encountered in the configured folder. Then those files will be encoded to the target extension configured.

This custom command is saved until you replace it for another command or setting this feature to 'off':

havc -cc "off"
Enter fullscreen mode Exit fullscreen mode

If 'off', the default command will be used.

Safety question

If you don't want to be bothered with the safety question and are certain the folder is the correct one to modify, you can also disable this feature by:

havc --no-safety-question
Enter fullscreen mode Exit fullscreen mode

This will get saved, so be careful when modifying the folder to convert.

Shutdown computer when finished

If you have a long list of files to convert, you can always enable the shutdown feature which will shutdown your computer when all files were converted.

havc --shutdown
Enter fullscreen mode Exit fullscreen mode

This will get saved, so be careful not to shutdown your computer by mistake. At the beginning of the encoding, the program will give a warning that the computer will be shutdown in the end.

Conclusion

I'm fully available to implement new features, fixing bugs and improve anything. Just comment here or open an issue in the GitHub repository.
Any suggestions I'm more than glad to consider them.
Any feedback is really appreciated.

For anymore information please have a look at this file.

The CLI can be downloaded from here and the GitHub repository can be found here.

Thanks for reading :D

Top comments (0)