Skip to content

Instantly share code, notes, and snippets.

@halkyon
Created August 27, 2020 22:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halkyon/63babc0f6907e34c6a8ed0d701a37850 to your computer and use it in GitHub Desktop.
Save halkyon/63babc0f6907e34c6a8ed0d701a37850 to your computer and use it in GitHub Desktop.
FLAC to MP3 converter command line tool in PHP
#!/usr/bin/php
<?php
##
## Extract a zip containing FLAC files, or a directory of FLAC files,
## and encode them into MP3 using LAME.
##
## Requirements (make sure these are in your path):
## - php (5.3+, required to run this script)
## - flac (for decoding FLAC to WAV)
## - lame (for encoding WAV to MP3)
##
## This script will use "-Y -V 3" as the parameters to LAME, which
## gives you around ~175Kbps variable bitrate on the encoded mp3s.
## See http://wiki.hydrogenaudio.org/index.php?title=LAME#Recommended_settings_details
## for more information on recommended settings for LAME.
##
## Usage: php flactomp3.php C:\path\to\my\flacs.zip C:\destination
## php flactomp3.php /home/blah/something.zip ~/temp
##
## First argument is path to a zip containing your FLAC files.
## Second is the destination path. If not given, it will extract
## to the current working directory.
##
## TODO:
## * Add a batch file for easily running this on Windows
## * Add a bash script for easily running this on *nix
## * Figure out if the dependencies of flac and lame can be bundled or something
## * Supress output of cmd_exists() exec usage for both Windows and *nix platforms
## * Download album information off the net, allow user to choose and add the ID3 tag using lame
## * If source path given not a zip, just a dir with flacs, just use that instead of unzip etc
## * If destination contains encoded mp3s already, give warning
## * Allow choosing which LAME settings to encode with
## * Test this works on Linux and OS X
##
## Author: Sean Harvey <halkyon at gmail dot com>
## Version: 0.1 (16 May 2013)
##
error_reporting(E_ALL | E_STRICT);
if(php_sapi_name() != 'cli') {
echo 'Please execute this script from the command line.' . PHP_EOL;
die();
}
if(empty($argv[1])) {
echo 'Missing first argument. Please specify path to zip file.' . PHP_EOL;
exit(1);
}
if(!file_exists($argv[1])) {
echo 'Path does not exist. Please specify a valid path.' . PHP_EOL;
exit(1);
}
$src = $argv[1];
$dest = (isset($argv[2]) && file_exists($argv[2])) ? realpath($argv[2]) : (isset($argv[2]) ? $argv[2] : getcwd());
$error = false;
$count = 0;
$start = microtime(true);
$chkcmd = (substr(PHP_OS, 0, 3) == 'WIN') ? 'where' : 'which';
$flac_path = trim(`$chkcmd flac`) ?: 'flac';
$lame_path = trim(`$chkcmd lame`) ?: 'lame';
if(!file_exists($flac_path)) {
echo 'flac command not available. Please ensure it\'s installed and in your path.' . PHP_EOL;
exit(1);
}
if(!file_exists($lame_path)) {
echo 'lame not available. Please ensure it\'s installed and in your path.' . PHP_EOL;
exit(1);
}
echo sprintf('Extracting to %s', $dest) . PHP_EOL;
if(!file_exists($dest)) {
mkdir($dest);
}
$info = pathinfo($src);
if(isset($info['extension']) && $info['extension'] == 'zip') {
// uncompress the zip containing FLAC files into dest directory
$zip = new ZipArchive();
$zh = $zip->open($src);
if(!$zh) {
echo sprintf('Could not open zip at %s.', $src) . PHP_EOL;
exit(1);
}
$ze = $zip->extractTo($dest);
if(!$ze) {
echo sprintf('Could not extract zip %s.', $src) . PHP_EOL;
exit(1);
}
$zip->close();
} else {
// we're just going to encode a directory of FLAC files
$dest = $src;
}
foreach(scandir($dest) as $file) {
$info = pathinfo($file);
if($info['extension'] != 'flac') continue;
$path = $dest . DIRECTORY_SEPARATOR . $info['filename'];
exec(sprintf('%s -d -f "%s.flac"', $flac_path, $path), $output, $return_var);
if($return_var != 0) {
$error = true;
continue;
}
exec(sprintf('%1$s -h -V 3 -Y "%2$s.wav" "%2$s.mp3"', $lame_path, $path), $output, $return_var);
if($return_var != 0) {
$error = true;
continue;
}
unlink($path . '.flac');
unlink($path . '.wav');
$count++;
}
if($error) {
echo 'Failed. Please check output above.' . PHP_EOL;
exit(1);
}
$end = microtime(true);
echo PHP_EOL;
echo sprintf('Success! %s files processed. Time taken: %s seconds.', $count, round($end-$start, 0)) . PHP_EOL;
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment