#!/usr/bin/php * * version 0.1 * * date November 23, 2005 * * dependencies -PHP 4.1.0 * * -getID3 library (http://www.getid3.org/) * ******************************************************************************** * * * *************** * * * ERRORLEVELS * * * *************** * * * * 0 Everything fine * * 1 No parameter given or more than one parameter * * 2 File not found * * 3 File is not an MP3 file * * 4 File doesn't have a variable bitrate * * 5 Error decoding to wave file * * 6 Error re-encoding to 320 kbps CBR file * * 7 Error moving temporary file over original file * *******************************************************************************/ // This lib does wonders... "Terrific!" in one word. require_once("getid3.php"); echo basename($_SERVER[argv][0]) . " - Converts a variable bitrate MP3 file "; echo "to one with a constant bitrate of 320 kbps,\nwhile retaining/repairing "; echo "the ID3 tags\n\n"; // Check for parameter (only one allowed: the path to an MP3 file). if ($_SERVER[argc] != 2) { echo "FATAL ERROR: no path to an MP3 file given! Exiting.\n"; exit(1); } // Check whether file exists. if (! file_exists($_SERVER[argv][1])) { echo "FATAL ERROR: file \"" . $_SERVER[argv][1] . "\" not found! Exiting.\n"; exit(2); } $id3 = new getID3; getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'write.php', __FILE__, true); // Now, do your magic. $info = $id3->analyze($_SERVER[argv][1]); // Check for MP3 file. if ($info[audio][dataformat] != "mp3") { echo "FATAL ERROR: \"" . $info[filename] . "\" is not an MP3 file! "; echo "Exiting.\n"; exit(3); } getid3_lib::CopyTagsToComments($info); /* // Check for VBR. if ($info[audio][bitrate_mode] != "vbr") { echo "FATAL ERROR: \"" . $info[filename] . "\" doesn't have a variable "; echo "bitrate! Exiting.\n"; exit(4); } */ // Decode to wave file. exec( "lame -S --decode " . escapeshellarg($info[filename]) . " " . escapeshellarg(substr($info[filename], 0, -3) . "wav") ." 2>&1 >/dev/null", $output, $return_var ); // Decoding has gone wrong. Clean up the mess and exit. if ($return_var > 0) { echo "FATAL ERROR: decoding \"" . $info[filename] . "\" has failed! "; echo "Exiting.\n"; // Clean up. if (file_exists(substr($info[filename], 0, -3) . "wav")) { unlink(substr($info[filename], 0, -3) . "wav"); } exit(5); } $tmp = tempnam("~/tmp", "mp3v2c_"); // Re-encode to 320 kbps CBR file. exec( "lame -S --cbr -b 320 -h -m s " . escapeshellarg( substr($info[filename], 0, -3) . "wav" ) . " " . $tmp . " 2>&1 >/dev/null", $output, $return_var ); // Re-encoding has gone wrong. Clean up the mess and exit. if ($return_var > 0) { echo "FATAL ERROR: re-encoding \"" . substr($info[filename], 0, -3) . "wav\ "; echo "has failed! Exiting.\n"; // Clean up. if (file_exists(substr($info[filename], 0, -3) . "wav")) { unlink(substr($info[filename], 0, -3) . "wav"); } if (file_exists($tmp)) { unlink($tmp); } exit(6); } // Clean up. if (file_exists(substr($info[filename], 0, -3) . "wav")) { unlink(substr($info[filename], 0, -3) . "wav"); } // (Re)write ID3 tags to temporary file. if ( $info[comments][artist][0] != "" || $info[comments][title][0] != "" || $info[comments][album][0] != "" || $info[comments][year][0] != "" || $info[comments][track][0] != "" || $info[comments][genre][0] != "" || $info[comments][totaltracks][0] != "" || $info[comments][tracknum][0] != "" ) { $tagwriter = new getid3_writetags; $tagwriter->filename = $tmp; $tagwriter->tagformats = array("id3v1", "id3v2.4"); $tagwriter->remove_other_tags = true; $tagdata = array(array()); if ($info[comments][artist][0] != "") { echo "\$info[comments][artist][0] = \"" . $info[comments][artist][0] . "\"\n"; $tagdata[ARTIST][0] = $info[comments][artist][0]; } if ($info[comments][title][0] != "") { echo "\$info[comments][title][0] = \"" . $info[comments][title][0] . "\"\n"; $tagdata[TITLE][0] = $info[comments][title][0]; } if ($info[comments][album][0] != "") { echo "\$info[comments][album][0] = \"" . $info[comments][album][0] . "\"\n"; $tagdata[ALBUM][0] = $info[comments][album][0]; } if ($info[comments][year][0] != "") { echo "\$info[comments][year][0] = \"" . $info[comments][year][0] . "\"\n"; $tagdata[YEAR][0] = $info[comments][year][0]; } if ($info[comments][track][0] != "") { echo "\$info[comments][track][0] = \"" . $info[comments][track][0] . "\"\n"; $tagdata[TRACK][0] = $info[comments][track][0]; } if ($info[comments][genre][0] != "") { echo "\$info[comments][genre][0] = \"" . $info[comments][genre][0] . "\"\n"; $tagdata[GENRE][0] = $info[comments][genre][0]; } if ($info[comments][totaltracks][0] != "") { echo "\$info[comments][totaltracks][0] = \"" . $info[comments][totaltracks][0] . "\"\n"; $tagdata[TOTALTRACKS][0] = $info[comments][totaltracks][0]; } if ($info[comments][tracknum][0] != "") { echo "\$info[comments][tracknum][0] = \"" . $info[comments][tracknum][0] . "\"\n"; $tagdata[TRACKNUMBER][0] = $info[comments][tracknum][0]; } $tagwriter->tag_data = $tagdata; if ($tagwriter->WriteTags()) { if (! empty($tagwriter->warnings)) { echo "Some warnings have arisen while (re)writing the ID3 tags.\nThey "; echo "were:\n" . implode("\n", $tagwriter->warnings) . "\n"; } } else { echo "Error (re)writing ID3 tags to the re-encoded MP3 file. Keeping "; echo "converted MP3\nthough.\n"; } } // Move temporary file over the original file. if (rename($tmp, $info[filename])) { echo "Successfully converted \"" . $info[filename] . "\" to a 320 kbps CBR "; echo "file.\n"; } else { echo "ERROR: there was an error moving the temporary file to \""; echo $info[filename] . "\"! The temporary file is retained as \"$tmp\".\n"; exit(7); } ?>