Jun 5, 2013

Batch file that converts AC3/DTS/EAC3 movie files to MP3

I watch movies through iPad or iPhone. I connect them to TV so that I can enjoy the big screen on my sofa. The WiFi speed is fast enough to carry high definition movies but I found one small problem. Apple is banning AC3/EAC3/DTS movie players. Currently there is no movie play app on iOS that is able to play AC3/EAC3/DTS and access network shared video files.

I think it is some kind of license of patent issues that Apple is complying. The best way for me to work around is to convert those movie files to normal MP3 audio format. So I made a batch file that detect audio format and convert them if necessary.

Audio format can be detected by two softwares: ffmpeg and mplayer. I am using ffmpeg in this article.
There are several transcoding software but I decided to use HandBrake.

Here is my batch file. Save it as c:\batch\convert_audio.bat.
@ECHO OFF
SET ConvertedFolderName=Converted
SET ConvertedExt=mp4
SET VideoSpeed=medium

SET ffmpeg=C:\Utils\ffmpeg\ffmpeg-20130603-git-2976e2a-win32-static\bin\ffmpeg.exe
SET HandBrakeCLI=C:\Program Files\Handbrake\HandBrakeCLI.exe

REM Removing quotation marks from input file path...
SET InputFile=%*
SET InputFile=%InputFile:"=%
SET InputFile=%InputFile:"=%
IF "x%InputFile%" == "x=" SET InputFile=

IF NOT EXIST "%ffmpeg%" GOTO :ERROR_ARG
IF NOT EXIST "%HandBrakeCLI%" GOTO :ERROR_ARG
IF NOT EXIST "%InputFile%" GOTO :ERROR_ARG
REM SET DebugCmd=ECHO

ECHO === Checking audio type: %InputFile% ===
SET AudioProblemDetected=
FOR /F "usebackq tokens=*" %%A IN (`CALL "%ffmpeg%" -i "%InputFile%" 2^>^&1 ^| find "Audio: ac3" `) DO SET AudioProblemDetected=True
FOR /F "usebackq tokens=*" %%A IN (`CALL "%ffmpeg%" -i "%InputFile%" 2^>^&1 ^| find "Audio: dts" `) DO SET AudioProblemDetected=True
FOR /F "usebackq tokens=*" %%A IN (`CALL "%ffmpeg%" -i "%InputFile%" 2^>^&1 ^| find "Audio: eac3" `) DO SET AudioProblemDetected=True
IF "x%AudioProblemDetected%" == "x" GOTO :END
ECHO === Audio needs to be converted ===

SET DriveLetter=
SET BaseDir=
SET FileNameOnly=
FOR %%A IN ("%InputFile%") DO (
    SET DriveLetter=%%~dA
    SET BaseDir=%%~pA
    SET FileNameOnly=%%~nA
)

SET InputDir=%DriveLetter%%BaseDir%
IF NOT EXIST "%InputDir%" GOTO :ERROR_PARSE

SET OutputDir=%InputDir%%ConvertedFolderName%
IF NOT EXIST "%OutputDir%" MKDIR "%OutputDir%"
IF NOT EXIST "%OutputDir%" GOTO :ERROR_OUTPUT_DIR

SET OutputFile=%OutputDir%\%FileNameOnly%.%ConvertedExt%
IF EXIST "%OutputFile%" GOTO :ERROR_OUTPUT_FILE_EXIST

ECHO === [%DATE% %TIME%] Converting file to %OutputFile% ... ===
%DebugCmd% "%HandBrakeCLI%" -i "%InputFile%" -o "%OutputFile%" --encoder x264 --x264-preset %VideoSpeed% --aencoder faac --rate 29.97 --pfr
IF NOT EXIST "%OutputFile%" GOTO :ERROR_FAILED_HANDBRAKE

ECHO === [%Date% %TIME%] Convertion finished ===
GOTO :END

:ERROR_FAILED_HANDBRAKE
ECHO *** It seems like HandBrake didn't generate the file ***
GOTO :ERROR_OUTPUT_FILE_EXIST_DUMP

:ERROR_OUTPUT_FILE_EXIST
ECHO *** Output file already exists ***
:ERROR_OUTPUT_FILE_EXIST_DUMP
ECHO OutputFile = %OutputFile%
GOTO :ERROR_OUTPUT_DIR_DUMP

:ERROR_OUTPUT_DIR
ECHO *** Output directory cannot be created ***
:ERROR_OUTPUT_DIR_DUMP
ECHO OutputDir = %OutputDir%
GOTO :ERROR_PARSE_DUMP

:ERROR_PARSE
ECHO *** Parsing error ***
:ERROR_PARSE_DUMP
ECHO Drive letter = %DriveLetter%
ECHO Folder name = %BaseDir%
ECHO File name = %FileNameOnly%
ECHO InputDir = %InputDir%
GOTO :ERROR_ARG_DUMP

:ERROR_ARG
ECHO *** Incorrect arguments ***
:ERROR_ARG_DUMP
ECHO ffmpeg = %ffmpeg%
ECHO HandBrakeCLI = %HandBrakeCLI%
ECHO InputFile = %InputFile%
GOTO :END

:END

As an optional step, I also made another batch file that takes a folder name and convert every files under the subfolders. Save it as c:\batch\convert_audio_folder.bat.
@ECHO OFF
SET CONVERT_AUDIO_FILE=C:\batch\convert_audio.bat

SET BaseDir=%*
SET BaseDir=%BaseDir:"=%
SET BaseDir=%BaseDir:"=%
REM SET DebugCmd=ECHO

IF NOT EXIST "%CONVERT_AUDIO_FILE%" GOTO :ERROR_ARG
IF "x%BaseDir%" == "x=" SET BaseDir=
IF NOT EXIST "%BaseDir%" GOTO :ERROR_ARG

FOR /F "tokens=*" %%A IN ('dir /b /s /a-d "%BaseDir%"') DO @%DebugCmd% %CONVERT_AUDIO_FILE% %%A

GOTO :END

:ERROR_ARG
ECHO *** Incorrect arguments ***
:ERROR_ARG_DUMP
ECHO CONVERT_AUDIO_FILE = %CONVERT_AUDIO_FILE%
ECHO BaseDir = %BaseDir%
GOTO :END

:END

This converting seems to take forever and I will be running these batch files for a while... lol

1 comment:

Jay said...

This looks like a good reference for the transcoding speed/quality.

http://blog.dest-unreach.be/2009/09/08/x264-presets