Jump to content

Recommended Posts

I have a cron job that runs every five minutes, it grabs all the videos in a file, and converts them to an FLV file. It uses FFMpeg to convert the videos, the problem is that the program can only do one file at a time. So, if I have five files waiting to convert, it will do them one at a time, which could take 30+ minutes. So... how could I grab the files, then convert them all at once?

 

The only way I could think would be to have the cron start up FFMpeg more than one time, and pass one file to each open FFMpeg instance... But how?

Link to comment
https://forums.phpfreaks.com/topic/129039-faster-cron-job-process/
Share on other sites

converting videos would take alot of processor + ram. if you run multiple instances you would have to share your resources between each one so u might not benefit from any speed up at all?

 

anyway, just a guess but you could write a shell script to run every minute or so which checks how many instances of FFMpeg u have running (find the command line for task manager and pipe results into ur script) against how many files you have waiting.

 

then if you have 4 instances running and 5 files waiting you could run your cron job and somehow find a way to know which files are already being processed and which ones need processing.

 

1. What type of file is that?

 

2. in php doing a "shell_exec" will wait for it to execute the first file, then when that one is done, it will execute the next... does this method do that?

 

3. my php does lots of extra things, such as gets the video height/width, the movie length in time such as 00:03:34, send an email, etc.  can I still do this?

my php does lots of extra things, such as gets the video height/width, the movie length in time such as 00:03:34, send an email, etc.  can I still do this?

 

Why would you be even using php to execute ffmpeg? Its a Linux command, use the Linux shell.

 

The above script is written in bash, the default shell on most (if not all) Linux distributions.

 

Are you saying you have cron execute a php script? If so, show me the script.

<?php
include '../../tzfiles.com/id3/getid3.php';
include '../../dudeel.com/incl/includes.php';
$fileTypes = array('mov','mpg','mpeg','avi','wmv','wma','mp4');
$f = implode(',',$fileTypes);

$gl = glob('../../flv.dudeel.com/wait/*.{'.$f.'}',GLOB_BRACE);
$bitr = 300;

function multiple_round($value, $to){
    $opt = $value / $to;
    $opt = round($opt,0);
    $opt = $opt * $to;
    return $opt;
} 



if(count($gl)>0){
foreach($gl as $f){
	$getID3 = new getID3;
	$fileInfo = $getID3->analyze($f);
	$fileName = fileNameNoExt($f);
	if(isset($fileInfo['video']['resolution_x']))
		$oResX = $fileInfo['video']['resolution_x'];
	else
		$oResX = 480;

	if(isset($fileInfo['video']['resolution_y']))
		$oResY = $fileInfo['video']['resolution_y'];
	else
		$oResY = 360;
// Get X Values
	$resX = 480;

// Get Y Values
	$resY = 360;

// Get a position in the video for an image
	if(isset($fileInfo['playtime_seconds'])){
		$playLen = $fileInfo['playtime_seconds'];
		$randFPlay = rand(0,$playLen);
		if($playLen > 59){
			$mins = floor($randFPlay/60);
		}else{
			$mins = 0;
		}
		$secs = floor($randFPlay%60);
		if ($secs<10) {
			$secs = "0".$secs;
		} 
		if ($mins<10) {
			$mins = "0".$mins;
		}
		$time = $mins.":".$secs;
	}else{
		$time = "00:03";
	}

	$ratio = round(($resX/$oResX)*$oResY);
	$r = ($resY - $ratio)/2;
	$padTop = multiple_round($r,2);
	$padBot = multiple_round($r,2);
	if($padTop < 0)
		$padTop = 0;
	if($padBot < 0)
		$padBot = 0;


// Convert to flv file
	shell_exec("/usr/bin/ffmpeg -i '$f' -qscale 12 -vcodec flv -f flv -r 29.97 -s {$resX}x{$resY} -aspect 4:3 -padtop {$padTop}px -padbottom {$padBot}px -b  {$bitr}k -g 160 -cmp 2  -subcmp 2  -mbd 2 -flags +aic+cbp+mv0+mv4+trell -ac 1 -ar 22050 -ab 56k '../../flv.dudeel.com/flv/$fileName.flv'");
// Create an image
	shell_exec("/usr/bin/ffmpeg -i '../../flv.dudeel.com/flv/$fileName.flv' -an -ss 00:{$time} -t 00:00:01 -r 1 -y -s 128x96 '{$fileName}%d.jpg'");
// Move the file
	rename("{$fileName}1.jpg","../../img.dudeel.com/video/{$fileName}.jpg");

	$fileSize = filesize('../../flv.dudeel.com/flv/'.$fileName.'.flv');
	if($fileSize > 0){
		mysql_query(sprintf("UPDATE search_video SET `converted` = 'yes', `fileSize` = '%s' WHERE `id` = '%s'",
				mysql_real_escape_string($fileSize),
				mysql_real_escape_string($fileName)));
		$sql = mysql_query(sprintf("SELECT * FROM users, search_video WHERE
			search_video.owner = users.id
			AND
			search_video.id = '%s'", mysql_real_escape_string($fileName)));
		if(mysql_num_rows($sql) > 0){
			$row = mysql_fetch_array($sql);
			include 'mail/convertedVideo.php';
		}
		unlink($f);
	}else{
		$sql = mysql_query(sprintf("SELECT * FROM users, search_video WHERE
			search_video.owner = users.id
			AND
			search_video.id = '%s'", mysql_real_escape_string($fileName)));
		if(mysql_num_rows($sql) > 0){
			$row = mysql_fetch_array($sql);
			include 'mail/convertedVideoBad.php';
		}
	}
}
}
?>

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.