I won’t go in to the convoluted reason why I needed to write this script, but I will tell you that I recently used it to convert over 540 music files (using FFMPEG) from wma to mp3. You see, when I ripped my CD’s I used the wma format (at the time a higher quality for smaller file size codec), and now I need them in mp3 format.
My first try was to do dir command (dir folderName /s > fileList.txt) from console, open the resultant text file in Notepad++, and search and replace (can you say REGEX?) to construct a list of commands to run as a batch file (again from console). This worked fine, but when I realized the scope of the project, I decided to resort to my favorite fallback, PHP.
Here is the script I generated, and it worked flawlessly (for me). Feel free to use it as you see fit. If you like it (my script), give my blog props or e-mail me.
<?php function h_pr($array) { if (is_array($array)) { echo '<pre>'; print_r($array); echo '</pre>'; } } $path = realpath('music/'); // realpath used to get whole pathway, not relative //$path = 'music/'; // use if relative pathway is okay $searchFor = '.wma'; // include leading dot $replaceWith = '.mp3'; // include leading dot try { // just in case there's an error - such as directory is empty $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); } catch (Exception $e) { $msg[] = 'Error: ' . $e->getMessage(); } if (isset($objects)) { foreach ($objects as $name => $object) { $fileNameListArr[] = $name; // let's put all the names (folder/files) in an array } //h_pr($fileNameListArr); // use for debugging $matchText = '~\\' . $searchFor . '$~i'; // should yield ~\.wma$~i // regex patten starts (and ends) with ~ followed by escaped . , wma, $ (at string end) case insensitive i foreach ($fileNameListArr as $kF => $vF) { if (preg_match($matchText, $vF)) { $wmaFileListArr[] = $vF; // let's put all our wma's in a new array } } //h_pr($wmaFileListArr); // use for debugging if (isset($wmaFileListArr) && count($wmaFileListArr)) { foreach ($wmaFileListArr as $kW => $vW) { // replace wma with mp3 and add to new array $mp3FileListArr[] = str_ireplace($searchFor, $replaceWith, $vW); } //h_pr($mp3FileListArr); // use for debugging // command pieces put in one place so you can tweak to your desire $beginCommand = 'ffmpeg -i "'; // beginning of ffmpeg conversion command $middleCommand = '" "'; // middle joining/between input file and output file $endCommand = '"'; // end of conversion command if (count($wmaFileListArr) == count($mp3FileListArr)) { for ($i = 0; $i < count($wmaFileListArr); $i += 1) { // assemble commands and put in new array $commands[] = $beginCommand . $wmaFileListArr[$i] . $middleCommand . $mp3FileListArr[$i] . $endCommand; // could have skipped making mp3FileListArr and just done conversion on the fly here // ex: str_ireplace($searchFor, $replaceWith, $wmaFileListArr[$i]) } } else { $msg[] = 'Something went wrong! Counts do not match!'; } } else { $msg[] = 'No files found.'; } } else { $msg[] = 'Nothing to display. Not sure why.'; } if (isset($msg) && count($msg)) { h_pr($msg); // print messages } if (isset($commands) && count($commands)) { foreach ($commands as $kC => $vC) { // print out commands with new lines only (will look cluttered but necessary for batch file) echo $vC . PHP_EOL; } }
That’s all there is to it. Change the folder location as needed, run the script, save the page as text. Now change the extension of the saved text file to .bat and invoke from the console. Assuming you have FFMPEG in the environment path (I do), it should start chugging away. After the batch file is done, verify the files are where they should be, and delete the unneeded (wma’s) if desired.