Hi.
PHP Code:
//CHECK POINT 1\\
//mail("lubna35@yahoo.com", "PHPmotion - Check Point 1", "Loaded OK", "From: Server <lubna35@yahoo.com>");
include_once ("classes/config.php");
@error_reporting(0);
//LOG THAT THIS PAGE WAS LOADED (debugging for CLI)
if ($log_encoder == "yes") {
//check if file exists
$file_contents = "\n\n\n\n" . 'PHPmotion Convertor.php debug' . "\n" .
'CLI for convertor OK' . "\n" . $config["date_format"] . "\n" .
'================================================================================' .
"\n";
$log_file = "logs/logfile.rtf";
if (@file_exists($log_file)) {//append to log file
$fo = @fopen($log_file, 'a');
@fwrite($fo, $file_contents);
@fclose($fo);
}
else {
$fo = @fopen($log_file, 'w');//else create new log
@fwrite($fo, $file_contents);
@fclose($fo);
}
}
/////////////////////////////
//get all file paths for file
/////////////////////////////
$ffmpeg_debug = $_GET['debug'];//incase we are loading this as a url dor debugging
$base_path = installation_paths();
$ffmpeg_path = $config["path_to_ffmpeg"];
//////////////////////////////
//PROCESS OR VIDEOS ONE BY ONE
//////////////////////////////
$sql = "SELECT * FROM videos where approved ='pending_conversion'";
$query = @mysql_query($sql);
while ($result = @mysql_fetch_array($query)) {
$raw_video = $result['video_id'];
//set current video to "converting"
$sql1 = "UPDATE videos SET approved = 'converting' WHERE video_id = '$raw_video'";
@mysql_query($sql1);
$raw_video_path = $base_path . '/uploads/avi/' . $raw_video;//full path of raw video file
list($file_name_no_extension, $extension) = @split('\.', $raw_video);//spliting the raw video file name to get just the unique name
$avi_file = $base_path . '/uploads/avi/' . $file_name_no_extension . '.avi';
$new_flv = $base_path . "/uploads/" . $file_name_no_extension . ".flv";
/////////////////////////////////////////////////////////////
// STEP 1 //
// encode video to flv //
/////////////////////////////////////////////////////////////
if ($extension != 'flv'){
//the following can be changed (vbitrate, vop scale, SRATE)
$path_to_mencoder = $config["path_to_mencoder"];
$mencoder_cmd = "$path_to_mencoder $raw_video_path -o $new_flv -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=800:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -vf scale=450:400 -srate 22050";
@exec("$mencoder_cmd 2>&1", $output);
//If no flv was created. Attempt to convert with -vop swicth and not -vf
if(!file_exists($new_flv)){
$mencoder_cmd = "$path_to_mencoder $raw_video_path -o $new_flv -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=800:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -vop scale=450:400 -srate 22050";
@exec("$mencoder_cmd 2>&1", $output);
}
//If no flv was created. Attempt to convert with no -lavcopts i_certify_etc_etc
if(!file_exists($new_flv)){
$mencoder_cmd = "$path_to_mencoder $raw_video_path -o $new_flv -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=800:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -vf scale=450:400 -srate 22050";
@exec("$mencoder_cmd 2>&1", $output);
}
//debugging
$debug_1 = $mencoder_cmd . "\n";//file line of debug
foreach ($output as $outputline) {
$debug_1 = $debug_1 . $outputline . "\n";
if ($debugmodex == 1) {//no debug mode
echo ("$outputline<br>");
}
}
//LOG THAT STEP 1 was ok
if ($log_encoder == "yes") {
//check if file exists
$file_contents = 'PHPmotion debug' . "\n" . $mencoder_cmd . "\n" .
'Command was executed.See rest of log for output details' . "\n" .
'================================================================================' .
"\n";
$log_file = "logs/logfile.rtf";
if (@file_exists($log_file)) {//append to log file
$fo = @fopen($log_file, 'a');
@fwrite($fo, $file_contents);
@fclose($fo);
}
else {
$fo = @fopen($log_file, 'w');//else create new log
@fwrite($fo, $file_contents);
@fclose($fo);
}
}
}else{
//just move the flv file
@copy($raw_video_path, $new_flv);
}
//CHECK POINT 2\\
//mail("you@yourdomain.com", "PHPmotion - Check Point 2", "Video - Flash : $mencoder_cmd", "From: Server <name@email.com>");
/////////////////////////////////////////////////////////////
// STEP 2 //
// FLVTOOL2 INJECTION //
/////////////////////////////////////////////////////////////
$path_to_flv = $config["path_to_flvtool2"];
$flv_cmd = "$path_to_flv -U $new_flv";
@exec("$flv_cmd 2>&1", $output);
//debugging
$debug_2 = $flv_cmd . "\n";//file line of debug
foreach ($output as $outputline) {
$debug_2 = $debug_2 . $outputline . "\n";
if ($debugmodex == 1) {//no debug mode
echo ("$outputline<br>");
}
}
//LOG THAT STEP 2 was ok
if ($log_encoder == "yes") {
//check if file exists
$file_contents = 'PHPmotion debug' . "\n" . $flv_cmd . "\n" .
'Command was executed.See rest of log for output details' . "\n" .
'================================================================================' .
"\n";
$log_file = "logs/logfile.rtf";
if (@file_exists($log_file)) {//append to log file
$fo = @fopen($log_file, 'a');
@fwrite($fo, $file_contents);
@fclose($fo);
}
else {
$fo = @fopen($log_file, 'w');//else create new log
@fwrite($fo, $file_contents);
@fclose($fo);
}
}
//CHECK POINT 3\\
//mail("you@yourdomain.com", "PHPmotion - Check Point 3", "FLVTOOL2 : $flv_cmd", "From: Server <name@email.com>");
/////////////////////////////////////////////////////////////
// STEP 3 //
// ffmpeg-php get video duration //
/////////////////////////////////////////////////////////////
//Original FFMPEG-PHP option for getting video length
///////////////////////////////////////////////////////////////////
/*
$video_info = @new ffmpeg_movie($new_flv);//duration of new flv file.
$sec = @$video_info->getDuration();// Gets the duration in secs.
*/
//-------------------------------------------------------------------------------end of original ffmpeg-php
//Alternative to flvtool2 if getting errors or if no flvtool exists (comment out if failing or to use ffpmeg-php)
///////////////////////////////////////////////////////////////////
//Try and read the time from the output files
$shell_output = $debug_1 . $debug_2;//get as much sheel out put as possible to run search for duration
if (@preg_match('/Video stream:.*bytes..(.*?).sec/', $shell_output, $regs)) {
$sec = $regs[1];
$duration = sec2hms($sec);//covert to 00:00:00 i.e. hrs:min:sec
}
else {
$sec = "";
}
//check if duration has been picked up...if not try second method (ffmpeg -i video.flv)
if ($sec == "" || !is_numeric($sec)) {
$check_duration = $ffmpeg_path . ' -i ' . $new_flv;
$durationfile = "";
@exec("$check_duration 2>&1", $durationoutput);
foreach ($durationoutput as $outputline) {
$durationfile = $durationfile . $outputline . "\n";
}
if (preg_match('/uration:.(.*?)\./', $durationfile, $regs)) {
$duration = $regs[1];//duration already in 00:00:00 format
$sec = date("s", strtotime($duration));//change back to seconds for use in getting middle of video
}
else {
$sec = 2;
$duration = sec2hms($sec);//covert to 00:00:00 i.e. hrs:min:sec
}
}
//get the middle of the movie (time; 00:00:00 format) for thumbnail
$sec2 = $sec / 2;
$sec2 = @round($sec2);
$thumb_position = sec2hms($sec2);
//LOG THAT STEP 3 was ok
if ($log_encoder == "yes") {
//check if file exists
$file_contents = 'PHPmotion debug' . "\n" .
'FFMPEG-PHP - check - Video Duration = ' . $duration . "\n" .
'================================================================================' .
"\n";
$log_file = "logs/logfile.rtf";
if (@file_exists($log_file)) {//append to log file
$fo = @fopen($log_file, 'a');
@fwrite($fo, $file_contents);
@fclose($fo);
}
else {
$fo = @fopen($log_file, 'w');//else create new log
@fwrite($fo, $file_contents);
@fclose($fo);
}
}
//CHECK POINT 4\\
//mail("you@yourdomain.com", "PHPmotion - Check Point 4", "FFMPEG-PHP : $duration", "From: Server <name@email.com>");
/////////////////////////////////////////////////////////////
// STEP 4 //
// Create thumnail image //
/////////////////////////////////////////////////////////////
$output_file = $base_path . '/uploads/thumbs/' . $file_name_no_extension .
'.jpg';
//IMPORTANT: if your thumbs ar enot being created, try change "mjpeg" below to "image2"
$ffmpeg_cmd2 = "$config[path_to_ffmpeg] -i $new_flv -ss $thumb_position -t 00:00:01 -s 120x90 -r 1 -f mjpeg $output_file";
//execute and record output to variable
@exec("$ffmpeg_cmd2 2>&1", $output);
//debugging
$debug_3 = $ffmpeg_cmd2 . "\n";//file line of debug
foreach ($output as $outputline) {
$debug_3 = $debug_3 . $outputline . "\n";
if ($debugmodex == 1) {//no debug mode
echo ("$outputline<br>");
}
}
//LOG THAT STEP 4 was ok
if ($log_encoder == "yes") {
//check if file exists
$file_contents = 'PHPmotion debug' . "\n" . $ffmpeg_cmd2 . "\n" .
'Command was executed.See rest of log for output details' . "\n" .
'================================================================================' .
"\n\n\n";
$log_file = "logs/logfile.rtf";
if (@file_exists($log_file)) {//append to log file
$fo = @fopen($log_file, 'a');
@fwrite($fo, $file_contents);
@fclose($fo);
}
else {
$fo = @fopen($log_file, 'w');//else create new log
@fwrite($fo, $file_contents);
@fclose($fo);
}
}
//////////////////////////////////////////////////////////////
//check if image thumb has been made
//This tries to create thumb using different format "image2"
//////////////////////////////////////////////////////////////
if (@!file_exists($output_file)) {
$ffmpeg_cmd2 = "$config[path_to_ffmpeg] -i $new_flv -ss $thumb_position -t 00:00:01 -s 120x90 -r 1 -f image2 $output_file";
//execute and record output to variable
@exec("$ffmpeg_cmd2 2>&1", $output);
//debugging
$debug_3 = $ffmpeg_cmd2 . "\n";//file line of debug
foreach ($output as $outputline) {
$debug_3 = $debug_3 . $outputline . "\n";
if ($debugmodex == 1) {//no debug mode
echo ("$outputline<br>");
}
}
//LOG THAT STEP 4 was ok
if ($log_encoder == "yes") {
//check if file exists
$file_contents = 'PHPmotion debug - Image type 2' . "\n" . $ffmpeg_cmd2 . "\n" .
'Command was executed.See rest of log for output details' . "\n" .
'================================================================================' .
"\n\n\n";
$log_file = "logs/logfile.rtf";
if (@file_exists($log_file)) {//append to log file
$fo = @fopen($log_file, 'a');
@fwrite($fo, $file_contents);
@fclose($fo);
}
else {
$fo = @fopen($log_file, 'w');//else create new log
@fwrite($fo, $file_contents);
@fclose($fo);
}
}
}
//CHECK POINT 5\\
//mail("you@yourdomain.com", "PHPmotion - Check Point 5", "THUMBNAIL : $ffmpeg_cmd2", "From: Server <name@email.com>");
/////////////////////////////////////////////////////////////
// STEP 6 //
// UDATE DATABASE DETAILS //
/////////////////////////////////////////////////////////////
//--------------new code 28 october 2007 --------reconnect mysql ---------
$sql_test = 'SELECT * FROM admin';
if (!mysql_query($sql_test)) {
@mysql_close();
@include_once ("classes/config.inc.php");
@include_once ("classes/config.php");
@mysql_connect($hostname,$dbusername,$dbpassword,true);
@mysql_select_db($dbname);
}
//--------------new code 28 october 2007 --------reconnect mysql end -----
//RESET THE VIDEO FROM "pending_conversion" to just "pending" (for admin approval etc)
$sql = "UPDATE videos SET approved='pending' WHERE video_id = '$raw_video'";
@mysql_query($sql);
//RESET THE VIDEO ID TO REMOVE file extenstion
$sql = "UPDATE videos SET video_id='$file_name_no_extension' WHERE video_id = '$raw_video'";
@mysql_query($sql);
//Update dabase with new duration information
/////////////////////////////////////////////
$file_name_no_extension = $file_name_no_extension;
$sql = "UPDATE videos SET video_length='$duration' WHERE video_id = '$file_name_no_extension'";
@mysql_query($sql);
//Check if video need pre approval by admin, if not update from pending to "yes"
////////////////////////////////////////////////////////////////////////////////
if ($config["auto_approve_videos"] == "yes") {
$sql = "UPDATE videos SET approved='yes' WHERE video_id = '$file_name_no_extension'";
@mysql_query($sql);
}
//@mysql_close();
//////////////////////////////////////////////////////////////////
//WRITE OUTPUT TO LOGFILE - HELP WITH DEBUGGING (logs/logfile.rtf)
//////////////////////////////////////////////////////////////////
if ($log_encoder == "yes") {
$file_contents = 'Date: ' . $config["date_format"] . "\n" . 'STEP 1 - OUTPUT' .
"\n" . $debug_1 . "\n" . 'STEP 2 - OUTPUT' . "\n" . $debug_2 . "\n" .
'STEP 4 - OUTPUT' . "\n" . $debug_3;//adding all output
//check if file exists
$log_file = "logs/logfile.rtf";
if (@file_exists($log_file)) {//append to log file
$fo = @fopen($log_file, 'a');
@fwrite($fo, $file_contents);
@fclose($fo);
}
else {
$fo = @fopen($log_file, 'w');//else create new log
@fwrite($fo, $file_contents);
@fclose($fo);
}
}
//delete original file and converted temp avi file
///////////////////////////////////////////////////
$original_file = $raw_video_path;
if ($config["delete_original"] == 'yes') {
if (@file_exists("$new_flv") && @file_exists("$raw_video_path")) {
if ($new_flv != $raw_video_path) {
@unlink($raw_video_path);
}
}
}
if ($config["delete_avi"] == 'yes') {
if (@file_exists("$new_flv") && @file_exists("$avi_file")) {
@unlink($avi_file);
}
}
//CHECK POINT 6\\
//mail("you@yourdomain.com", "PHPmotion - Check Point 6", "SCRIPT LOADED TO THE END", "From: Server <you@yourdomain.com>");
@mysql_close();
}//end while
?>