Skip to content

Instantly share code, notes, and snippets.

@raytiley
Created June 17, 2010 20:30
Show Gist options
  • Save raytiley/442730 to your computer and use it in GitHub Desktop.
Save raytiley/442730 to your computer and use it in GitHub Desktop.
<?php
require_once("ctnDBConnect.php");
require_once('nusoap.php');
$contentPaths = array("/Volumes/VS400/", "/Volumes/SXServer/", "/Volumes/SXSafe/");
$destinationPath = "/Volumes/VODContent/VOD/";
$ffmpegOptions = "-qscale 8 -r 25 -ar 44100 -y";
$ccVODprojectsIDs = array(55, 58, 59); //Add 55 and 58
$report = "";
$skippedReport = "";
$emailReport = array(
"ray@ctn5.org",
"beth@ctn5.org",
"tom@ctn5.org");
$ffmpeg = "/opt/local/bin/ffmpeg";
//Mail Headers
$headers = 'From: ray@ctn5.com'."\r\n".'Reply-To: ray@ctn5.org'."\r\n".'X-Mailer: PHP/'.phpversion();
//Create connection to Cablecast System
$ccWSclient = new nusoap_client("http://192.168.0.136/CablecastWS/CablecastWS.asmx?WSDL", 'wsdl'); // Creates New SOAP client using WSDL file
//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 stringFromInteger($str) {
$len = strlen($str);
for ($i = 0, $int = ''; $i < $len; $i++) {
if (is_numeric($str[$i])) {
$int .= $str[$i];
} elseif(!is_numeric($str[$i]) && $int) {
$i = $len;
}
}
return (int) $int;
}
function cleanFileName($fileName) {
$cleanName = str_replace(" ", "\ ", $fileName);
$cleanName = str_replace("(", "\(", $cleanName);
$cleanName = str_replace(")", "\)", $cleanName);
$cleanName = str_replace("'", "\'", $cleanName);
return $cleanName;
}
foreach($ccVODprojectsIDs as $category)
{
$result = $ccWSclient->call('AdvancedShowSearch', array(
'ChannelID' => 2,
'searchString' => "",
'eventDate' => date("Y-m-d")."T12:00:00",
'dateComparator' => "<",
'restrictToCategoryID' => $category,
'restrictToProducerID' => 0,
'restrictToProjectID' => 0,
'displayStreamingShowsOnly' => 'false',
'searchOtherSites' => 'false'), '', '', false, true);
if ($result['AdvancedShowSearchResult']['SiteSearchResult']['Shows'])
{
$tmpShows = $result['AdvancedShowSearchResult']['SiteSearchResult']['Shows']['ShowInfo'];
if($result['AdvancedShowSearchResult']['SiteSearchResult']['Shows']['ShowInfo']['ShowID'])
{
$VODShows[] = $tmpShows['ShowID'];
}
else
{
foreach($tmpShows as $show )
{
$VODShows[] = $show['ShowID'];
}
}
}
}
//Check the database to determine which shows have not been transcoded already
if($VODShows)
{
foreach($VODShows as $VODShow)
{
$vod_query = "Select * FROM vods where show_id = $VODShow";
if ($vod_result = mysql_query($vod_query))
{
if (mysql_num_rows($vod_result) == 0)
{
$transcodeShows[] = $VODShow;
}
}
}
}
//Find all the shows files
if($transcodeShows)
{
echo var_dump($transcodeShows)."\n\n";
$report = $report."There are ".count($transcodeShows)." shows to be transcoded. \n";
foreach($contentPaths as $path)
{
$directoryListing = dirList($path);
foreach($transcodeShows as $show)
{
$search_show_id = (int) $show;
$key = array_search($search_show_id, $directoryListing);
if($key)
{
$file_name = cleanFileName($directoryListing[$key]);
if(!$confirmedFiles[(int)$show])
{
$confirmedFiles[(int)$show] = array("showID" => (int)$show, "path" => $path."".$file_name);
}
else
{
$confirmedFiles[(int)$show] = "duplicate";
$report = $report."More than one file was found for show id: ".$show." Please Remove all but one\n";
}
}
}
}
}
echo "confirmed files:\n".var_dump($confirmedFiles)."\n\n";
if($confirmedFiles)
{
foreach($confirmedFiles as $file)
{
if(in_array($file['showID'], $transcodeShows))
{
$key = array_search($file['showID'], $transcodeShows);
unset($transcodeShows[$key]);
}
}
// Take file and transcode them.
foreach($confirmedFiles as $encode)
{
$showInfo = $ccWSclient->call('GetShowInformation', array('ShowID' => $encode["showID"]), '', '', false, true);
$ccShowLength = (int)$showInfo["GetShowInformationResult"]["TotalSeconds"];
$cmd = "$ffmpeg -i ".$encode['path']." 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time))
{
$ffmpegShowLength = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
if(!(($ffmpegShowLength > $ccShowLength + 5) || ($ffmpegShowLength < $ccShowLength - 5)))
{
$cmd = "$ffmpeg -i ".$encode['path']." $ffmpegOptions $destinationPath".$encode['showID'].".flv";
exec($cmd, $output, $return_var);
if(!$return_var)
{
$second = rand(1, ($ffmpegShowLength - 1));
$cmd = "$ffmpeg -i ".$encode['path']." -deinterlace -an -ss $second -t 00:00:01 -s 320x240 -r 1 -y -vcodec mjpeg -f mjpeg $destinationPath/thumbnails/".$encode['showID'].".jpg 2>&1";
$return = `$cmd`;
$success_query = "Insert into vods (show_id, encoded, url, created_at, updated_at) Values ('".$encode['showID']."', '".date( 'Y-m-d H:i:s')."', 'http://www.ctn5.org/VODTEST/".$encode['showID'].".flv', '".date( 'Y-m-d H:i:s')."', '".date( 'Y-m-d H:i:s')."')";
$success_result = mysql_query($success_query);
$successReport = $successReport."The Show with ShowID: ".$encode["showID"]." was successfully transcoded\n";
}
else
{
$failedReport = $failedReport."The Show with ShowID: ".$encode["showID"]." transcoding failed\n";
}
}
else
{
$skippedReport = $skippedReport."Encoding for ShowID: ".$encode["showID"]." was skipped because file's length does not agree with Show Record\n";
}
}
else
{
$report = $report."The file for Show ID: ".$encode["showID"]." can not be processed by the system\n";
}
}
}
if($transcodeShows)
{
foreach($transcodeShows as $show)
{
$missingFileReport = $missingFileReport."A file for ShowID: $show could not be found\n";
}
}
//email a report
if($report != "")
{
$report = "VOD Report for: ".date("Y-m-d @ g:i a")."\n\n".$report.$missingFileReport.$skippedReport.$successReport.$failedReport;
foreach($emailReport as $recipitent)
{
mail($recipitent, 'VOD Report for '.date('m/d/Y @ g:i a'), $report, $headers);
}
}
echo $report;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment