All2mp3

From xbe wiki

Jump to: navigation, search

This is a simple php shell script which converts all media files in a given directory (i.e. wma, flac, aac, etc.) to mp3. Basically it converts everything that mplayer supports, as it's a wrapper for mplayer & lame.

At the moment it only accepts an entire directory, not single files. And it doesn't check if the source type is mp3, it will convert it to mp3 again ;-). It checks if mplayer could produce an valid output though, so you can have not-audio file in the directory (i.e. cover shots) without running into problems..

Installation

Very simple, just copy&paste the source below into a new file and correct the first line of the script with the path to your php binary (if it's /usr/bin/php you don't have to change anything). Then do:

at t cp all2mp3.php /usr/bin/all2mp3
chmod +x /usr/bin/all2mp3

And it's done!

You don't have to copy it to /usr/bin naturally (can be any directory in your $PATH). If you don't want to have it in a directory in your $PATH, just execute it where you want ;-)

Usage

all2mp3 [directory]

Directory is the source & destination directory at the same time. If you do not provide this parameter, the script will take the current cwd (current directory you're in) as the working directory.

Source V1.0

Here's the source (don't copy the enclosing "<?php" and "?>"! That's added by the php highlight extension in the wiki and isn't necessary in your file..)

<?php

#!/usr/bin/php
<?
set_time_limit(0);

echo "--------------------------\n";
echo "all2mp3 v1.0\n";
echo "--------------------------\n";

checkTools();

$def_path $argv[1];
if(isset($def_path)) $path $def_path;
else $path getcwd();
if(!is_dir($path)) die("Error: Path \"{$path}\" is not a valid directory.\n"); 

// let's start..
if(substr($path,-1) != "/"$path .= "/";

$output = array();
if($dir opendir($path)) {
    while (($file readdir($dir)) !== false) {
           if($file != "." && $file != "..") {
               $filename substr($file0strrpos($file"."));               
               $mplayer_target $path.$filename.".wav";
               $lame_target $path.$filename.".mp3";
               
               echo "[mplayer] {$file} => {$filename}.wav ... ";

               exec("mplayer -ao pcm:file=\"{$mplayer_target}\" \"{$path}{$file}\" 2>&1"$output$return);
               if(file_exists($mplayer_target)) {
                   $mklame true;
                   echo "OK!\n";
               
                   echo "[lame] {$filename}.wav => {$filename}.mp3 ... ";
                   exec("lame -h -b 160 \"{$mplayer_target}\" \"{$lame_target}\" 2>&1"$output$return);    
                   if(file_exists($mplayer_target)) echo "OK!\n";
                   
                   echo "[general] Unlinking {$filename}.wav ... ";
                   unlink($mplayer_target);
                   if(!file_exists($mplayer_target)) echo "OK!\n";
                   else echo "FAILURE!\n";
               }else{
                   $mklame false;
                   echo "Not valid!\n";
               }
               
           }
    }
    closedir($dir);
}else{
    die("Error: Could not open directory handle for \"{$path}\"!\n");
}


function checkTools() {
    $tools = array(
        "mplayer",
        "lame"
    );
    $output = array();
    foreach($tools as $tool) {
        exec($tool." 2>&1"$output$return); 
        if($return != && $return != 1) {
            die("Error: Could not find needed app \"{$tool}\" in \$PATH! Has to be there..\n");
        }
    }
}
?>

?>
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox