Thursday, May 29, 2014

Batch Encoding mp4 Videos to Wav (Audio CD format) in Windows

At times, you want to listen to your favorite tunes while driving in congested roads or for extended period of time between cities. The problem is, your car's audio only support Audio CD. This dictates you to convert your favorite tunes to Audio CD. Moreover, you don't have enough time to convert them one by one. Enter the "batch encoding" realm..
Simply put, batch encoding is the process of encoding multiple files in one go. To do that, we use shell script, or batch file in Windows. Because most of my favorite tunes are mp4 videos, my batch encoding script converts those mp4s into wav files. I used VLC to carry out the encoding and I'm using a powershell script to automate the encoding into batch encoding. This is the powershell script that I use:
$outputExtension = ".wav"
$bitrate = 128
$channels = 2

foreach($inputFile in get-childitem -Filter *.mp4)
{ 
  $outputFileName = [System.IO.Path]::GetFileNameWithoutExtension($inputFile.FullName) + $outputExtension;
  $outputFileName = [System.IO.Path]::Combine($inputFile.DirectoryName, $outputFileName);
  
  echo "Output filename: $outputFileName" 
    
  $programFiles = ${env:ProgramFiles(x86)};
  echo "Program Files: $programFiles"
  
  if($programFiles -eq $null) { $programFiles = $env:ProgramFiles; }
  
  $processName = $programFiles + "\VideoLAN\VLC\vlc.exe"
  echo "processName: $processName"
  
  $processArgs = "-I dummy -vvv `"$($inputFile.FullName)`" --sout=#transcode{acodec=`"s16l`",ab=`"$bitrate`",`"channels=$channels`"}:standard{access=`"file`",mux=`"wav`",dst=`"$outputFileName`"} vlc://quit"

  echo "processArgs: $processArgs"
  
  start-process $processName $processArgs -wait

  }
It's not that complicated. It's a slightly modified script from VLC wiki (https://wiki.videolan.org/How_to_Batch_Encode/). There was one hiccup though because by default poweshell doesn't permit script execution. Therefore, I have to run powershell as administrator and then modify the execution policy to unrestricted like so:
Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.

PS C:\Users\zzz> Set-ExecutionPolicy Unrestricted
Anyway, the powershell script above assumes that you placed all of the mp4 in the current directory (directory where the powershell script located). The batch encoding result will be located in the current directory as well.
Now, to create audio CD, you'll need the application to burn the wav files into audio CD. I'm using cdrecord in Linux to do this because for some reason, the only machine with CD/DVD writer that I have is running Linux. You can use your favorite CD authoring application to do this.

Happy Encoding!
Post a Comment

No comments: