Sunday, April 5, 2020

Convert Non-AC3 Audio to AC3 for movies

6/4/2020: 
1. Fixed a bug where it was only processing files where the channels were greater than 6, not when then codec name was not compatible
2. Fixed a bug where special characters in filenames were causing issues.

Plex issues on the XBOX One

The Plex on the XBOX One is a decent movie player, however there's been an issue for a while (years) where the XBOX won't play videos properly in the case when the video needs to be transcoded.

The XBOX can play most common video formats, but it can't play some of the newer audio formats natively (TRUEHD, DOLBY ATMOS, DTX:X) and it's can't play regular old DTS either.  In order to play these Plex transcodes and although it play fine on other devices, for some reason it causes massive buffering on the XBOX one when transcoding is required.

There doesn't seem to be a solution in sight, and the PLEX devs seemed to have given up, so I've written this script to to go through my existing video library and convert any audio streams to be natively compatible with the XBOX One.


Preparing the Script

The script is a Power Shell script and to use it it's quite simple.

1. Download FFMpeg:

https://ffmpeg.zeranoe.com/builds/

2. Unzip just the contents of the "bin" folder to a folder of your choice.  You can unzip the other files but they are not needed.

e.g. ffmpeg.exe and ffprobe.exe to a folder called "ConvertToAC3"

3. Create a new file in the same folder as the FFMpeg files and paste the script into it.  Make sure the extension of the file is ps1 (i.e. convert.ps1)

4. Alter the script at the top changing the following variables

$videos_path
This needs to be set to the path of your video library

$preview
When 0, this will convert the files when the script is run. When the value is 1 it will only show you what it will attempt to convert and not actually do a conversion.

5. Save the changes to the script

6. Before we run the script, make sure you have double the space available on your hard drive - as it will duplicate each file.


Allowing Powershell Scripts To Run

You may need to run the following command in a power script session before getting started as Power Shell Scripts are disabled by default.  You need run the session as an admin to allow this command to run to allow the script to execute.
set-executionpolicy remotesigned
NOTE: to run as admin, right click on the Windows Power Shell icon and click "Run As Administrator".


What Does the Script do? 

1. The script goes and find every MKV file under the path that you have set (recursively).
2. It them inspects each one (using FFProbe) to see if it needs to be converted
3. If it decects that he MKV needs to be converted, it will use FFMPEG to convert audio (only the main stream) and creates a new a video file with the untouched video and the new audio as AC3.
4. It will take a while depeding the on the number of videos you have.
5. Once it's finished file it will rename the original the *.mkvold, so that Plex will ignore it and you can delete it once you are happy that the audio has been converted corrected

Thanks To...

I'd like to thank Robin Clarke who's work with FFMPEG gave me a basis for my script.

Here's the Script