Save storage by reducing media size

I found that my media on my home server was starting to take up a lot of space and I set out to find a better way to manage it. First I thought that I would simply delete older videos that were over a certain date (with some exceptions), however I quickly came to the conclusion “why delete it, if I can first resize it”. So I wrote a script to recursively search through my media and then reduce the size of any videos that met my requirements.

As the script runs it will report on the progress with the number of files processed and the reduced size from the original.

Screen Shot 2017-11-05 at 8.42.35 pm

The script uses ffmpeg so you will need to ensure it is installed and then add the /bin directory in the windows PATH. The script utilises both FFMPEG and FFPROBE. Both of these executables should exist in the FFMPEG /bin directory.

  1. Start the System Control Panel applet (Start – Settings – Control Panel – System).
  2. Select the Advanced tab.
  3. Click the Environment Variables button.
  4. Under System Variables, select Path, then click Edit.
  5. You’ll see a list of folders with a “;” separator.
  6. Add the ffmpeg /bin folder to the end of the list. e.g ;C:\Program Files\ffmpeg-20171027-5834cba-win64-static\bin

The script can be executed with the following parameters:

-Directory
-OptimizeAfterDays
-ValidateOnly
-DeleteOriginalVideo
-ffmpegQuality
-ConstantRateFactor

-Directory
The directory to search for the media. This directory will be searched recursively.

-OptimizeAfterDays
The number of days a file must exist for before it is optimized. If the file creation date is older than the threshold, the media will be optimized.

-ValidateOnly
This is a switch. Including this parameter in the command will stop the script from optimizing or deleting any files. It will only report on what files are going to be modified.

-DeleteOriginalVideo
This is a switch. Including this parameter in the command will stop the script from deleting the original input file.

-ffmpegQuality
This parameter will allow you to tab-complete the possible settings. The Quality sets the output resolution 480p, 720p or 1080p. The parameter settings are “hd480”, “hd720”, “hd1080”

-ConstantRateFactor
The constant rate factor defines the rate control for the x264 encoding process. A lower rate factor means a higher quality. You can set this between 0-51. A setting of between 21 and 24 is a very good range to chose from.

Examples:
.\OptimizeMedia.ps1 -Directory "C:\Temp" -OptimizeAfterDays "30" -ValidateOnly
This will search c:\Temp for any media files that were created more than 30 days ago. No files will be optimised or deleted. Only a validation will run.

.\OptimizeMedia.ps1 -Directory "C:\Temp" -ffmpegQuality hd480 -ConstantRateFactor 21 -OptimizeAfterDays 30 -DeleteOriginalVideo
This will search c:\Temp for any media files that were created more than 30 days ago. Media files will be optimised to a lower quality (480p) and size (CRF21), then the original file will be deleted.

 

Downloads

PowerShell Script – OptimizeMedia.ps1
Download ffmpeg – https://www.ffmpeg.org/download.html