#!/usr/bin/php
<?php
/*******************************************************************************
* mpc2mp3.php - Converts a Musepack file to an MP3 file *
********************************************************************************
* filename mpc2mp3.php *
* author Richard Vrijhof <R.J.Vrijhof@bigfoot.com> *
* version 0.1 *
* date December 10, 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 MPC file *
* 4 Error decoding to wave file *
* 5 Error encoding to MP3 file *
*******************************************************************************/
// This lib does wonders... "Terrific!" in one word.
require_once("getid3.php");
echo basename($_SERVER[argv][0]) . " - Converts an MPC file to MP3\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 MPC 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 MPC file.
if ($info[audio][dataformat] != "mac") {
echo "FATAL ERROR: \"" . $info[filename] . "\" is not an MPC file! ";
echo "Exiting.\n";
exit(3);
}
$sans_ext = substr($info[filename], 0, -4);
// Decode to wave file.
echo "Decoding MPC file to a WAVE file. Please wait...";
exec(
"mppdec " . escapeshellarg($info[filename]) . " " . escapeshellarg($sans_ext) .
".wav 2>&1 >/dev/null",
$output,
$return_var
);
echo "done.\n";
// 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("$sans_ext.wav")) {
unlink("$sans_ext.wav");
}
exit(4);
}
// Encode to an MP3 320 kbps CBR file.
echo "Encoding to a 320 kbps CBR MP3 file. Please wait...";
exec(
"lame -S --cbr -b 320 -h -m s " . escapeshellarg("$sans_ext.wav") . " " .
escapeshellarg("$sans_ext.mp3") . " 2>&1 >/dev/null",
$output,
$return_var
);
echo "done.\n";
// Encoding has gone wrong. Clean up the mess and exit.
if ($return_var > 0) {
echo "FATAL ERROR: encoding \"$sans_ext.wav\" has failed! Exiting.\n";
// Clean up.
if (file_exists("$sans_ext.wav")) {
unlink("$sans_ext.wav");
}
if (file_exists("$sans_ext.mp3")) {
unlink("$sans_ext.mp3");
}
exit(5);
}
// Clean up.
if (file_exists("$sans_ext.wav")) {
unlink("$sans_ext.wav");
}
// Write ID3 tags to MP3 file.
getid3_lib::CopyTagsToComments($info);
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] != ""
) {
echo "Writing ID3 tags to MP3 file. Please wait...";
$tagwriter = new getid3_writetags;
$tagwriter->filename = "$sans_ext.mp3";
$tagwriter->tagformats = array("id3v1", "id3v2.4");
$tagwriter->remove_other_tags = true;
$tagdata = array(array());
if ($info[comments][artist][0] != "") {
$tagdata[ARTIST][0] = $info[comments][artist][0];
}
if ($info[comments][title][0] != "") {
$tagdata[TITLE][0] = $info[comments][title][0];
}
if ($info[comments][album][0] != "") {
$tagdata[ALBUM][0] = $info[comments][album][0];
}
if ($info[comments][year][0] != "") {
$tagdata[YEAR][0] = $info[comments][year][0];
}
if ($info[comments][track][0] != "") {
$tagdata[TRACK][0] = $info[comments][track][0];
}
if ($info[comments][genre][0] != "") {
$tagdata[GENRE][0] = $info[comments][genre][0];
}
if ($info[comments][totaltracks][0] != "") {
$tagdata[TOTALTRACKS][0] = $info[comments][totaltracks][0];
}
if ($info[comments][tracknum][0] != "") {
$tagdata[TRACKNUMBER][0] = $info[comments][tracknum][0];
}
$tagwriter->tag_data = $tagdata;
if ($tagwriter->WriteTags()) {
if (! empty($tagwriter->warnings)) {
echo "done.\nSome warnings have arisen while writing the ID3 tags.\nThey";
echo " were:\n" . implode("\n", $tagwriter->warnings) . "\n";
} else {
echo "done.\nSuccessfully converted \"" . $info[filename] . "\" to an ";
echo "MP3 file.\n";
}
} else {
echo "done.\nError writing ID3 tags to the MP3 file. Keeping MP3 file\n";
echo "though.\n";
}
} else {
echo "Successfully converted \"" . $info[filename] . "\" to an MP3 file.\n";
}
?>