Jump to content

Powershell Script to transcode MP4 & MKV files to HEVC/AAC


Recommended Posts

Posted (edited)

Hello folks, I needed space on the disks and have written one (two) script(s) in Powershell to convert my MP4 and MKV files into HEVC/AAC.

I decided to use Handbrake for Video and Audio Transcoding and FFmpeg for Audio Transcoding only because Handbrake has a better resulting quality at a lower file size. FFmpeg works better with multiple instances simultaneously. So FFmpeg was better for Audio transcoding.

I would like to share the result with you.

Warning!!

 

I Transcoded hundreds of files with no error, but this doesn't mean it will do the same at your PC. 

Please take a look at the code and change it to your needs.

 

 

What does the 1st script do:

  • Read the folder you choose, recursive into an Array.
  • Checking for Codecs used in the Files.
  • Deciding how to convert.
  • Runs 1 instance of Handbrake and up to 5 FFmpeg instances at the same time.
  • Runs on CPU only to get the best visual results.
  • Setting different parameters for Movies and Series (Series are scaled to 720P if resolution >720P (-w 720 --keep-display-aspect)).
  • Generating a ".ignore" file so Emby ignores the actual folder.
  • Converting into same Directory, but not deleting old file.
  • Checking if file was converted.
  • The path of the currently converting file is written to a .txt file.
  • Writing every step into the console.
  • This Script removes all Audio tracks except the first one
  • This Script removes all Subtitles

How the script decides to convert:

Checking for Codecs used in the Files

  • If Audio not AAC and Video not HEVC → convert Video and Video (Handbrake)
  • If Audio not AAC and Video is HEVC → convert Audio copy Video (FFmpeg)
  • If Audio is AAC and Video not HEVC → convert Video copy Audio (Handbrake)
  • If Audio is AAC and Video is HEVC → do nothing 

Audio Transcoding (FFmpeg) :

The script differentiates between Mono/Stereo and Surround

Mono/Stereo :

  • $aacbitrate = '128k'  (lower is possible, but not recommended)
  • $aachz = '44100'

Surround : 

  • $aacbitrate = '256k'
  • $aachz = '48000'

Video Transcoding (Handbrake)

Handbrake Series :

  •  -e x265 --encoder-preset medium -w 720 --keep-display-aspect --vfr --quality `"$videoquality`" -E `"$handbrakeaudiocodec`" -B `"$handbrakeaudiobitrate`" --normalize-mix `"$handbrakenormalize`" -i `"$oldfile`" -o `"$newfile`""

Handbrake Movie : 

  • -e x265 --encoder-preset medium --vfr --quality `"$videoquality`" -E `"$handbrakeaudiocodec`" -B `"$handbrakeaudiobitrate`" --normalize-mix `"$handbrakenormalize`" -i `"$oldfile`" -o `"$newfile`""

 

 

Download:

Video Transcoder.ps1

Edited by maximus1
  • Thanks 1
Posted

reserved

Here is my 2nd Script.

This Script cleans up after the 1st script has done his job.

What does the 2nd script do:

  • Read the folder you choose, recursive into an Array.
  • Comparing old file and new file → duration and file size
  • deleting old file if checks are OK
  • Renaming new file to old file
  • Checks for files maybe generated by Emby while transcoding 
  • Deleting these files if found.
  • Deleting ".ignore" file if found
  • Counting Videos "cleaned/renamed"
  • Counting Errors for file size and duration
  • Writing every step into the console.

Files to search for:

  •    '.neu' + $file.Extension
  •    '.nfo'
  •    '.neu' + '.nfo'
  •    '.neu-clearlogo.png'
  •    '.neu-fanart.jpg'
  •     '.neu-poster.jpg'
  •     '.neu-discart.png'
  •     '.neu-thumb.jpg'
  •     '.neu-landscape.jpg'
  •     '.neu-banner.jpg'
  •     '.neu-clearart.png'

Proof will come, I have to make something to clean :-)

Download:

Video Transcode cleanup.ps1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...