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:


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

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.

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.

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

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”

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.

.\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.



PowerShell Script – OptimizeMedia.ps1
Download ffmpeg –

4 thoughts on “Save storage by reducing media size

    • I am also using freeNAS. I have the media shared via smb to a windows server that is dedicated to running the script. This process is very CPU intensive so I didn’t want it to run it from a server that is doing anything else. I created a windows VM, shared the storage over a symlink (or mapped drive) and then run the script from a dedicated windows server.


  1. As I’ve said, I would like to apply this method in freenas FreeBSD environment.

    Can you make an instruction or script to reduce media size on Dataset/zvol on Freenas please?
    Or is it even possible to reduce media size on Freenas?


    • Hi Ric,
      All of my media is on FreeNAS, but I don’t run the script from the FreeNAS FreeBSD operating system. I share the media directory to a Windows server and then run the script from there.
      Firstly, do you have a windows operating system that you can run the PowerShell script from? any windows desktop, laptop, server or VM.
      All you need to do is enable SMB shares within FreeNAS (if you haven’t already) and share your media directory. Log on to your Windows OS, map a new network drive (assuming z:\)and enter the credentials that have root permissions, or permission to write and delete files.
      Finally, run this PowerShell script from Windows and enter the directory as Z:\{your media directory}.

      As I tried to explain before, this process is very CPU intensive and if you run it directly on FreeNAS then you may end up consuming all of your CPU resources and FreeNAS may stop working. It is much better to run a CPU intensive process like this on a separate computer or VM.


Comments are closed.