Skip to content

Instantly share code, notes, and snippets.

@raytiley
Created September 13, 2010 22:47
Show Gist options
  • Save raytiley/578204 to your computer and use it in GitHub Desktop.
Save raytiley/578204 to your computer and use it in GitHub Desktop.
<?php
//Setup Content drives and Destination path
$contentPaths = array("/Volumes/VS400/", "/Volumes/SXServer/", "/Volumes/SXSafe/");
$destinationPath = "/Volumes/VODContent/mp4/";
$transcodeCommand = "HandBrakeCLI --preset \"iPhone & iPod Touch\" --width 320 --vb 500 --two-pass --turbo --optimize ";
//Define some functions to do various tasks
function dirList ($directory)
{
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = opendir($directory);
// keep going until all files in directory have been read
while ($file = readdir($handler)) {
// if $file isn't this directory or its parent,
// add it to the results array
if ($file != '.' && $file != '..')
$results[] = $file;
}
// tidy up: close the handler
closedir($handler);
// done!
return $results;
}
function cleanFileName($fileName) {
$cleanName = str_replace(" ", "\ ", $fileName);
$cleanName = str_replace("(", "\(", $cleanName);
$cleanName = str_replace(")", "\)", $cleanName);
$cleanName = str_replace("'", "\'", $cleanName);
return $cleanName;
}
$filesToTranscode = array();
foreach($contentPaths as $dir)
{
$files = dirList($dir);
foreach($files as $file) {
if(preg_match('/(^\b[0-9]+).*\.(mpg|mpeg|MPG|MPEG)/', $file, $matches))
{
if(!file_exists($destinationPath.$matches[1])) {
$filesToTranscode[] = array("showID" => $matches[1], "path" => cleanFileName($dir.$file));
}
}
}
}
$remaining = count($filesToTranscode);
$processed = 1;
foreach($filesToTranscode as $job)
{
echo "Transcoding file number: ".$processed."\n\n";
$command = $transcodeCommand."--input ".$job["path"]." --output ".$destinationPath.$job["showID"].".mp4";
exec($command, $output, $return);
$cmd = "ffmpeg -i ".$job["path"]." 2>&1";
$output = `$cmd`;
$second = 10;
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', $output, $time))
{
$ffmpegShowLength = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
$second = rand(1, ($ffmpegShowLength - 1));
}
$cmd = "ffmpeg -i ".$job["path"]." -deinterlace -an -ss $second -t 00:00:01 -s 320x240 -r 1 -y -vcodec mjpeg -f mjpeg /Volumes/VODContent/VOD/thumbnails/".$job["showID"].".jpg 2>&1";
$return = `$cmd`;
if(file_exists($destinationPath.$job["showID"].".mp4")) {
echo "Sweet VOD file is where it belongs!";
$query = "INSERT INTO vods (show_id, origianl_path, vod_path) VALUES (".$job["showID"].", '".$job["path"]."', '".cleanFileName($dir.$file)."')";
$result = mysql_query($query);
}
$processed++;
$remaining--;
echo "\n\n".$remaining." transcode jobs remaining\n\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment