Skip to content

Instantly share code, notes, and snippets.

@rugbyprof
Last active August 29, 2015 14:20
Show Gist options
  • Save rugbyprof/836fec8e3ce9a4c552b4 to your computer and use it in GitHub Desktop.
Save rugbyprof/836fec8e3ce9a4c552b4 to your computer and use it in GitHub Desktop.
Convert directory of avi files to mpg.
<?php
// Usage: php convert-avi-mpg.php nameofdir
$dirName = $argv[1];
//Make sure directory has trailing slash
$dirName = rtrim($dir,"/");
$dir = scandir($dirName);
//remove . and ..
array_shift($dir);
array_shift($dir);
foreach($dir as $infile){
//Crappy non cool brute force string cleaner as opposed to regex
$newfile = preg_replace('/\s+/', '_', $infile); //Make spaces ' ' underscores '_'
$newfile = str_replace("(","_",$newfile); //Make parenes '( )' underscores '_'
$newfile = str_replace(")","_",$newfile);
$newfile = str_replace("_-_","-",$newfile); //Replace '_-_' with just a dash
$newfile = str_replace("__","_",$newfile); //Replace '__' with single underscore '_'
$infile = escapeshellarg($infile);
exec("mv ./{$dirName}/{$infile} ./{$dirName}/{$newfile}");
//Finally do avi to mpg conversion
$path_parts = pathinfo(./{$dirName}/{$infile});
$outfile = $path_parts['filename'].".avi";
$command = "ffmpeg -i ./{$dirName}/{$infile} -f avi -c:v mpeg4 -b:v 4000k -c:a libmp3lame -b:a 320k ./{$dirName}/{$outfile}";
passthru($command);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment