Skip to content

Instantly share code, notes, and snippets.

@withmorten
Last active February 18, 2017 02:57
Show Gist options
  • Save withmorten/5784de8cfba1b2b4dd0c15efc9d1fad1 to your computer and use it in GitHub Desktop.
Save withmorten/5784de8cfba1b2b4dd0c15efc9d1fad1 to your computer and use it in GitHub Desktop.
cod4ff extract
<?php
const WAV_NEEDLE = ".wav";
const WAV_PACKID = "feffffff";
const DEFAULTPATH = "sound";
$ffdump = file_get_contents($argv[1]);
$lastpos = 0;
$wav_pathid = hex2bin("FFFFFF");
$wavs = array();
$c = 0;
while(($lastpos = strpos($ffdump, WAV_NEEDLE, $lastpos)) !== FALSE) {
$wav_start = strrpos($ffdump, $wav_pathid, -(strlen($ffdump) - ($lastpos - 1)));
$wav_id = bin2hex(substr($ffdump, $wav_start-1, 4));
$wav_name = substr($ffdump, $wav_start+3, ($lastpos+4)-($wav_start+3));
if($wav_id == WAV_PACKID) {
$wav_header = substr($ffdump, $wav_start-37, 36);
$wav_length = hexdec(bin2hex(strrev(substr($wav_header, 8, 4))));
$wav_sample = hexdec(bin2hex(strrev(substr($wav_header, 12, 4))));
$wav_chanel = hexdec(bin2hex(strrev(substr($wav_header, 20, 4))));
$wavs[$c] = array("name" => $wav_name, "start" => $lastpos+5, "length" => $wav_length, "sample" => $wav_sample, "channels" => $wav_chanel);
$c++;
}
$lastpos = $lastpos + strlen(WAV_NEEDLE);
}
if(!file_exists(DEFAULTPATH)) {
mkdir(DEFAULTPATH);
}
foreach($wavs as $c => $wav) {
$explodepath = explode("/", $wav["name"]);
$pathsize = count($explodepath);
$oldpath = DEFAULTPATH."/";
for($i = 1; $i <= $pathsize; $i++) {
$fullpath = $oldpath.$explodepath[$i-1];
if($i != $pathsize) {
if(!file_exists($fullpath)) {
mkdir($fullpath);
}
$oldpath = $fullpath."/";
} else {
file_put_contents($fullpath, substr($ffdump, $wav["start"], $wav["length"]));
$ffmpeg = "ffmpeg -y -f s16le -ac ".$wav["channels"]." -ar ".$wav["sample"]." -i $fullpath ".substr($fullpath, 0, -4)."_PCM.wav\n";
echo $ffmpeg;
}
}
}
?>
@withmorten
Copy link
Author

withmorten commented Jul 9, 2016

Works only with unzipped dumps. Use like this:

 for %I in ("*.dump") do php -f main.php "%I" >>convertmorewavs.bat

Works in conjunction with the following script that parses the ffmpeg commands for dupes:

https://gist.github.com/withmorten/06f9d65ec138808edeeb956501d9ec0a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment