Skip to content

Commit

Permalink
merge script
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Mar 29, 2012
0 parents commit 268e894
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions mergeallaudio
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/php
<?php

$exe = "";
$dir = getcwd();
$dirlist = scandir($dir);

unset($dirlist[0]);
unset($dirlist[1]);

$audiof = array('aac','mka','mp3');

$esc = array('*', '?', '[', ']', '>', '<', '|', ';', '&');

// parms
$isDefault = false;
$toTrackCounts = 2;
foreach($argv as $key=>$val)
{
if($val == "-default")
$isDefault = true;
if($val == "-c")
{
if((int)$argv[$key + 1] > 0)
$toTrackCounts = (int)$argv[$key + 1];
}
}

foreach($dirlist as $content)
{
$format = strrpos($content, '.');
$name = substr($content, 0, $format);

if($format === false)
$format = '';
else
$format = substr($content, $format+1, strlen($content)-($format+1));

if($format == 'mkv' || $format == 'mp4')
{
$audio = "";
$af = array();
foreach($audiof as $f)
{
$sn = "{$name}.{$f}";
foreach($esc as $e)
$sn = str_replace($e, "\\".$e, $sn);
$find = `find -name '$sn'`;
$find = explode("\n", $find);
array_pop($find);
$af = array_merge($af, $find);
}

$new_name = $content;
$i = 1;

$currentTracks = $toTrackCounts;

foreach($af as $sound_track)
{
if($isDefault)
{
$torder = '';
for($or = 1; $or <= $currentTracks; $or++)
{
if($or == 2)
$torder .= '1:1,';
$torder .= "0:$or,";
}
$torder = substr($torder, 0, -1);

$deftrack = '';
for($or = 2; $or <= $currentTracks + 1; $or++)
$deftrack .= " --default-track $or:" . (($or == 2) ? 'yes' : 'no');

$exe .= "mkvmerge --track-order {$torder} '$new_name' '$sound_track' -o '{$name}_temp{$i}.mkv';\n";
$exe .= "rm -f '$new_name';\n";
$new_name = "{$name}_temp{$i}.mkv";
$i++;
$exe .= "mkvmerge{$deftrack} '$new_name' -o '{$name}_temp{$i}.mkv';\n";
$exe .= "rm -f '$new_name';\n";
$currentTracks++;
}
else
{
$exe .= "mkvmerge '$new_name' '$sound_track' -o '{$name}_temp{$i}.mkv';\n";
$exe .= "rm -f '$new_name';\n";
}
$new_name = "{$name}_temp{$i}.mkv";
$i++;
}
$exe .= "mv '$new_name' '{$name}.mkv';\n";
}
}

echo $exe;

?>

0 comments on commit 268e894

Please sign in to comment.