File: /home/httpd/html/freecams1.com/public_html/admin/encoder_cron.php
<?php
include_once('db.php');
include_once('getid3/getid3.php'); //class for getting codec info and duration of video etc.
if($multiple_servers == 1) {
$default_video_path = $video_path;
}
//get encoder status
$encoder_status = dbQuery("SELECT * FROM status",false);
$rrow = $encoder_status[0];
if($rrow['encoder_running'] == 1) {
exit("Encoding already marked running!");
}
//set iteration counter
$counter = 0;
//force even numbered thumb sizes, otherwise ffmpeg errors out.
if(is_odd($thumbwidth)) {
$thumbwidth++;
}
if(is_odd($thumbheight)) {
$thumbheight++;
}
$rresult = dbQuery("SELECT record_num FROM content WHERE approved = 1 AND photos = 0",false); //check if there is any videos
if(count($rresult) < 1) {
echo "[".date('Y-m-d H:i:s')."] No videos in queue, exiting.\n";
exit();
}
$encoderTotal = count($rresult);
dbQuery("UPDATE status SET encoder_running = 1, encoder_done = 0, encoder_total = '$encoderTotal'"); //set encoder status
$result = dbQuery("SELECT * FROM content WHERE approved = 1 AND photos = 0",false);
//There are no files to transcode, exit.
if(!is_array($result)) {
echo "No files to transcode!";
dbReconnect();
dbQuery("UPDATE status SET encoder_running = 0, encoder_total = 0, encoder_done = 0");
exit();
}
//start loop
foreach($result as $row) {
$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze($content_path."/".$row['orig_filename']);
if($ThisFileInfo['video']['fourcc'] != 'avc1') {
//file is not x264 video, force transcode because it won't play in HTML5
$forceEncodeThis = true;
}
//Update filename if file requires re-encoding or we are forcing.
if($forceEncodeAll || $forceEncodeThis) {
echo "[".date('Y-m-d H:i:s')."] Force encode all enabled, renaming output file.\n";
if(stripos($row[filename],'.mp4') !== false) {
$row[filename] = uniqid().'.mp4';
echo "[".date('Y-m-d H:i:s')."] Updating output filename in database.\n";
dbQuery("UPDATE content SET filename = '$row[filename]' WHERE record_num = '$row[record_num]'");
}
}
$replace = array("#"," ");
$row[filename] = str_replace($replace,'',$row[filename]);
dbQuery("UPDATE content SET filename = '$row[filename]' WHERE record_num = '$row[record_num]'");
$rrow[id] = $row['record_num'];
$first = $row['filename'][0];
$second = $row['filename'][1];
$third = $row['filename'][2];
$forth = $row['filename'][3];
$fifth = $row['filename'][4];
if($multiple_servers == 1) {
if($row['server'] != 0) {
$getServer = dbQuery("SELECT * FROM servers WHERE record_num = {$row['server']}",false);
if(count($getServer) > 0) {
$server = $getServer[0];
if(substr($server['nfs_path'],-1) == "/") {
$server['nfs_path'] = substr($server['nfs_path'],0,-1);
}
$video_path = $server['nfs_path'];
}
} else {
$video_path = $default_video_path;
}
}
echo "[".date('Y-m-d H:i:s')."] Generating subdirectories for videos.\n";
@mkdir($video_path.'/'.$first);
@mkdir($video_path.'/'.$first.'/'.$second);
@mkdir($video_path.'/'.$first.'/'.$second.'/'.$third);
@mkdir($video_path.'/'.$first.'/'.$second.'/'.$third.'/'.$forth);
@mkdir($video_path.'/'.$first.'/'.$second.'/'.$third.'/'.$forth.'/'.$fifth);
echo "[".date('Y-m-d H:i:s')."] Generating subdirectories for thumbs.\n";
@mkdir($thumb_path.'/'.$first);
@mkdir($thumb_path.'/'.$first.'/'.$second);
@mkdir($thumb_path.'/'.$first.'/'.$second.'/'.$third);
@mkdir($thumb_path.'/'.$first.'/'.$second.'/'.$third.'/'.$forth);
@mkdir($thumb_path.'/'.$first.'/'.$second.'/'.$third.'/'.$forth.'/'.$fifth);
$subdir = $first.'/'.$second.'/'.$third.'/'.$forth.'/'.$fifth.'/';
if((!$forceEncodeAll && !$forceEncodeThis) || $row['hotlinked']) {
if(stripos($row['orig_filename'],'.mp4') || stripos($row['orig_filename'],'.m4v')) {
//file is FLV/MP4 so we just copy instead of encoding
echo "[".date('Y-m-d H:i:s')."] File is H264, skipping encoding and copying.\n";
$q = "cp \"".$content_path."/".$row['orig_filename']."\" \"$video_path/$subdir".$row['filename']."\"";
echo "Copying... ".$row['orig_filename']." -> ".$row[filename]."\n";
echo "Command: ".$q."\n";
shell_exec($q);
echo "[".date('Y-m-d H:i:s')."] ...done\n";
echo "[".date('Y-m-d H:i:s')."] Starting MP4Box.\n";
shell_exec("$mp4box_path -inter 500 \"$video_path/$subdir".$row['filename']."\"");
echo "[".date('Y-m-d H:i:s')."] ...done\n";
} else {
//file is not flv/mp4 so we must encode.
if($encoder_software == 'handbrake') {
$q = "$handbrake_path -i \"".$content_path."/".$row['orig_filename']."\" -o \"$video_path/$subdir".$row['filename']."\" $handbrake_command ";
} else {
$q = "$ffmpeg_path -i \"".$content_path."/".$row['orig_filename']."\" $ffmpeg_command \"$video_path/$subdir".$row['filename']."\"";
}
echo "[".date('Y-m-d H:i:s')."] File is not H264, encoding...\n";
echo "Encoding... ".$row['orig_filename']." -> ".$row['filename']."\n";
echo "Command: ".$q."\n";
shell_exec($q);
echo "[".date('Y-m-d H:i:s')."] ...done\n";
echo "[".date('Y-m-d H:i:s')."] Starting MP4Box.\n";
shell_exec("$mp4box_path -inter 500 \"$video_path/$subdir".$row[filename]."\"");
echo "[".date('Y-m-d H:i:s')."] ...done\n";
}
} else {
if($encoder_software == 'handbrake') {
$q = "$handbrake_path -i \"".$content_path."/".$row['orig_filename']."\" -o \"$video_path/$subdir".$row['filename']."\" $handbrake_command ";
} else {
$q = "$ffmpeg_path -i \"".$content_path."/".$row['orig_filename']."\" $ffmpeg_command \"$video_path/$subdir".$row['filename']."\"";
}
echo "[".date('Y-m-d H:i:s')."] File is not H264, encoding...\n";
echo "Encoding... ".$row['orig_filename']." -> ".$row['filename']."\n";
echo "Command: ".$q."\n";
shell_exec($q);
echo "[".date('Y-m-d H:i:s')."] ...done\n";
echo "[".date('Y-m-d H:i:s')."] Starting MP4Box.\n";
shell_exec("$mp4box_path -inter 500 \"$video_path/$subdir".$row[filename]."\"");
echo "[".date('Y-m-d H:i:s')."] ...done\n";
$forceEncodeThis = false;
}
if($encodeMobile) {
//encode mobile if it's on
$mobFile = uniqid().'.mp4';
if($encoder_software == 'handbrake') {
$q = "$handbrake_path -i \"".$content_path."/".$row['orig_filename']."\" -o \"$video_path/$subdir".$mobFile."\" $handbrake_mobile_command ";
} else {
$q = "$ffmpeg_path -i \"".$content_path."/".$row['orig_filename']."\" $ffmpeg_mobile_command \"$video_path/$subdir"."$mobFile\"";
}
echo "[".date('Y-m-d H:i:s')."] Processing alternate version...\n";
echo "Encoding... ".$row['orig_filename']." -> ".$row['filename']."\n";
echo "Command: ".$q."\n";
shell_exec($q);
echo "[".date('Y-m-d H:i:s')."] ...done\n";
echo "[".date('Y-m-d H:i:s')."] Starting MP4Box.\n";
shell_exec("$mp4box_path -inter 500 \"$video_path/$subdir"."$mobFile\"");
echo "[".date('Y-m-d H:i:s')."] ...done\n";
if(file_exists($video_path."/$subdir".$mobFile) && filesize($video_path."/$subdir".$mobFile) > 0) {
dbReconnect();
dbQuery("UPDATE content SET mobile = '$mobFile' WHERE record_num = '$rrow[id]'");
}
}
if($encodeTrailer) {
//encode trailer if it's on
$trailer = uniqid().'.mp4';
if($encoder_software == 'handbrake') {
$q = "$handbrake_path -i \"".$content_path."/".$row['orig_filename']."\" -o \"$video_path/$subdir".$trailer."\" $handbrake_mobile_command --start-at duration:$trailerFrom --stop-at duration:$trailerDuration ";
} else {
$q = "$ffmpeg_path -i \"".$content_path."/".$row['orig_filename']."\" -ss $trailerFrom -t $trailerDuration $ffmpeg_command \"$video_path/$subdir".$trailer."\"";
}
echo "[".date('Y-m-d H:i:s')."] Processing trailer version...\n";
echo "Encoding... ".$row['orig_filename']." -> ".$trailer."\n";
echo "Command: ".$q."\n";
shell_exec($q);
echo "[".date('Y-m-d H:i:s')."] ...done\n";
echo "[".date('Y-m-d H:i:s')."] Starting MP4Box.\n";
shell_exec("$mp4box_path -inter 500 \"$video_path/$subdir"."$trailer\"");
echo "[".date('Y-m-d H:i:s')."] ...done\n";
if(file_exists($video_path."/$subdir".$trailer) && filesize($video_path."/$subdir".$trailer) > 0) {
dbReconnect();
dbQuery("UPDATE content SET trailer_filename = '$trailer' WHERE record_num = '$rrow[id]'");
}
}
if(file_exists($video_path."/$subdir".$row['filename']) && filesize($video_path."/$subdir".$row['filename']) > 0) {
$target_movie = $video_path."/$subdir".$row['filename'];
if(1) {
echo "[".date('Y-m-d H:i:s')."] Processing output with getid3...\n";
$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze("$video_path/$subdir".$row['filename']);
$reallengthid3 = $ThisFileInfo['playtime_string'];
$thisarray = explode(':',$reallengthid3);
if(count($thisarray) == 3) {
$length = ($thisarray[0]*3600)+($thisarray[1]*60)+$thisarray[2];
} else {
$length = ($thisarray[0]*60)+$thisarray[1];
}
$movie_width = $ThisFileInfo['video']['resolution_x'];
$movie_height = $ThisFileInfo['video']['resolution_y'];
$movie_fps = round($ThisFileInfo['video']['frame_rate']);
if(!$movie_fps) { $movie_fps = 30; }
if($numFrames == 0) {
$numFrames = $length*15;
}
dbReconnect();
dbQuery("UPDATE content SET length = '$length' WHERE record_num = '$rrow[id]'");
echo "[".date('Y-m-d H:i:s')."] ...done\n";
$dirname = str_replace('.flv','',$row['orig_filename']);
@mkdir('../media/thumbs/'.$subdir.$dirname);
//create thumbnails
echo "Total Length: $length\n\n";
$interval = floor($length/13);
$first = 1;
echo "[".date('Y-m-d H:i:s')."] Starting thumbnailing...\n";
for($i = 0; $i <11; $i++) {
if($first) {
$target_image = "$thumb_path/$subdir$dirname/".$row['orig_filename']."-$i.jpg";
$target_imageb = "$thumb_path/$subdir$dirname/".$row['orig_filename']."-$i"."b.jpg";
if($length) {
echo "Creating thumb #$i at second: $first... ";
if ($movie_height > $movie_width) {
$ct = "$ffmpeg_path -ss $first -i \""."$video_path/$subdir".$row['filename']."\" -vcodec mjpeg -vframes 1 -an -filter_complex '[0:v]scale=ih*16/9:-1,boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch)/20:chroma_power=1[bg];[bg][0:v]overlay=(W-w)/2:(H-h)/2,crop=h=iw*9/16' -f rawvideo -s ".$thumbwidth."x".$thumbheight." \"$target_image\"";
shell_exec($ct);
if($jpegoptim_path) { shell_exec("$jpegoptim_path $jpegoptim_command \"$target_image\""); }
shell_exec("$ffmpeg_path -ss $first -i \""."$video_path/$subdir".$row['filename']."\" -vcodec mjpeg -vframes 1 -an -filter_complex '[0:v]scale=ih*16/9:-1,boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch)/20:chroma_power=1[bg];[bg][0:v]overlay=(W-w)/2:(H-h)/2,crop=h=iw*9/16' -f rawvideo \"$target_imageb\"");
if($jpegoptim_path) { shell_exec("$jpegoptim_path $jpegoptim_command \"$target_imageb\""); }
} else {
shell_exec("$ffmpeg_path -ss $first -i \"" . "$video_path/$subdir" . $row['filename'] . "\" -vcodec mjpeg -vframes 1 -an -f rawvideo \"$target_imageb\"");
shell_exec("$convert_path -auto-orient -resize ".($thumbwidth)."x".($thumbheight)."^ -gravity Center -extent ".$thumbwidth."x".$thumbheight." \"$target_imageb\" \"$target_image\"");
if($jpegoptim_path) { shell_exec("$jpegoptim_path $jpegoptim_command \"$target_imageb\""); }
if($jpegoptim_path) { shell_exec("$jpegoptim_path $jpegoptim_command \"$target_image\""); }
}
if(file_exists($target_image)) {
echo "Success!\n\n";
} else {
echo "Failure! Unable to create thumbnail!\n\n";
}
if(file_exists("/usr/local/bin/mogrify")) {
shell_exec("/usr/local/bin/mogrify $imagick_command $target_image");
} else {
shell_exec("/usr/bin/mogrify $imagick_command $target_image");
}
//transfer to ceph storage and remove if needed.
if($ceph_enabled) {
if(cephUploadObject($target_image,str_replace("$basepath/media/","",$target_image))) {
echo "[".date('Y-m-d H:i:s')."] transfered $target_image to cloud storage.\n";
if($ceph_delete_local) {
if(file_exists($target_image)) {
@unlink($target_image);
}
}
}
if(cephUploadObject($target_imageb,str_replace("$basepath/media/","",$target_imageb))) {
echo "[".date('Y-m-d H:i:s')."] transfered $target_imageb to cloud storage.\n";
if($ceph_delete_local) {
if(file_exists($target_imageb)) {
@unlink($target_imageb);
}
}
}
}
//eof
}
$first = $first+$interval;
}
}
echo "[".date('Y-m-d H:i:s')."] ...done\n";
if($row['source_thumb_url']) {
$fileSource = file_get_contents($row['source_thumb_url']);
if(strlen($fileSource) > 48) {
$target_image = "$thumb_path/$subdir$dirname/".$row[orig_filename]."-1.jpg";
file_put_contents($target_image,$fileSource);
shell_exec("$mogrify_path -resize ".($thumbwidth)."x".($thumbheight)."^ -gravity Center -extent ".$thumbwidth."x".$thumbheight." \"$target_image\"");
dbReconnect();
dbQuery("UPDATE content SET main_thumb = 1 WHERE record_num = '$rrow[id]'");
}
}
dbReconnect();
dbQuery("UPDATE content SET approved = 2, encoded_date = NOW(), movie_width = '$movie_width', movie_height = '$movie_height', fps = '$movie_fps' WHERE record_num = '$rrow[id]'");
//check and insert CRC
if($row['orig_filename']) {
$origFileCRC = md5_file("$content_path/".$row['orig_filename']);
$newFileCRC = md5_file("$video_path/$subdir".$row['filename']);
dbReconnect();
dbQuery("INSERT INTO content_crc32 (checksum, content) VALUES ('$origFileCRC','$rrow[id]')");
if($newFileCRC != $origFileCRC) {
dbQuery("INSERT INTO content_crc32 (checksum, content) VALUES ('$newFileCRC','$rrow[id]')");
}
}
//add to appropriate categories.
if($row[keywords]) {
$parray = explode(',',$row[keywords]);
foreach($parray as $p) {
$p = trim($p);
$tresult = dbQuery("SELECT record_num FROM niches WHERE name LIKE '$p' OR (csv_match != '' AND csv_match LIKE '%$p%')",false);
if(count($tresult) > 0) {
$trow = $tresult[0];
dbQuery("INSERT INTO content_niches (niche, content) VALUES ($trow[record_num], $rrow[id])");
unset($trow, $tresult);
}
}
}
//make webvtt
echo "Making VTT..\n.";
$i = $row[filename];
$first = $row[filename][0];
$second = $row[filename][1];
$third = $row[filename][2];
$forth = $row[filename][3];
$fifth = $row[filename][4];
$subdir = $first.'/'.$second.'/'.$third.'/'.$forth.'/'.$fifth.'/';
$target_movie = $video_path."/".$subdir.$i;
$rrow[id] = uniqid();
$dirname = str_replace('.flv','',$row[orig_filename]);
$target_image = "$thumb_path/$subdir$dirname/";
echo "[".date('Y-m-d H:i:s')."] Starting timeline thumb generation...\n";
shell_exec("$ffmpeg_path -i \""."$video_path/$subdir".$row[filename]."\" -vcodec mjpeg -vf fps=1/5 -s 200x112 \"$target_image/vtt_%03d.jpg\"");
echo "[".date('Y-m-d H:i:s')."] ...done\n";
if($jpegoptim_path) {
echo "[".date('Y-m-d H:i:s')."] Starting timeline thumb optimization...\n";
shell_exec("find \"$target_image/\" -type f -name \"*.jpg\" -exec $jpegoptim_path $jpegoptim_command {} \;");
echo "[".date('Y-m-d H:i:s')."] ...done\n";
}
//transfer to ceph storage and remove if needed.
if($ceph_enabled) {
unset($vttThumbs);
$prepareThumbnails = scandir($target_image);
//remove . and ..
unset($prepareThumbnails[0]);
unset($prepareThumbnails[1]);
foreach($prepareThumbnails as $p) {
$vttThumbs[] = $target_image.$p;
}
foreach($vttThumbs as $v) {
if(cephUploadObject($v,str_replace("$basepath/media/","",$v))) {
echo "[".date('Y-m-d H:i:s')."] transfered $v to cloud storage.\n";
if($ceph_delete_local) {
@unlink($v);
}
}
}
}
//delete source
unlink("$content_path/".$row['orig_filename']);
if($deleteHotlinkedVideos && $row['hotlinked']) {
unlink("$video_path/$subdir".$row['filename']);
unlink("$video_path/$subdir".$row['mobile']);
}
//transfer to ceph storage and remove if needed.
if($ceph_enabled) {
$finalVideoPath = "$video_path/$subdir".$row[filename];
$finalTrailerPath = "$video_path/$subdir".$trailer;
$finalMobilePath = "$video_path/$subdir".$mobFile;
if(file_exists($finalVideoPath)) {
if(cephUploadObject($finalVideoPath,str_replace("$basepath/media/","",$finalVideoPath))) {
echo "[".date('Y-m-d H:i:s')."] transfered $finalVideoPath to cloud storage.\n";
if($ceph_delete_local) {
@unlink($finalVideoPath);
}
} else {
exit("Unable to upload $row[filename] to storage cloud!");
}
}
if($encodeTrailer) {
if(file_exists($finalTrailerPath)) {
if(cephUploadObject($finalTrailerPath,str_replace("$basepath/media/","",$finalTrailerPath))) {
echo "[".date('Y-m-d H:i:s')."] transfered $finalTrailerPath to cloud storage.\n";
if($ceph_delete_local) {
@unlink($finalTrailerPath);
}
} else {
exit("Unable to upload $finalTrailerPath to storage cloud!");
}
}
}
if($encodeMobile) {
if(file_exists($finalMobilePath)) {
if(cephUploadObject($finalMobilePath,str_replace("$basepath/media/","",$finalMobilePath))) {
echo "[".date('Y-m-d H:i:s')."] transfered $finalMobilePath to cloud storage.\n";
if($ceph_delete_local) {
@unlink($finalMobilePath);
}
} else {
exit("Unable to upload $finalMobilePath to storage cloud!");
}
}
}
}
//eof
dbQuery("UPDATE status SET encoder_done = encoder_done + 1");
echo "Success!\n\n";
$counter++;
}
} else {
dbReconnect();
echo "Failure!\n\n";
}
}
echo "\n----------------------\nEncoding Complete - $counter videos converted";
dbReconnect();
dbQuery("UPDATE status SET encoder_running = 0, encoder_total = 0, encoder_done = 0");