我想知道是否有任何简单的方法可以在后台运行和ffmpeg进程,以便人们可以离开页面。如果你这样做,那么告诉ffmpeg进程是否正在运行,当它完成时,将数据库更新为“finished”。如果需要,我会发布我的代码。谢谢! 这是处理页面上的代码:
<?php
$name = $_FILES['upload_file']['name'];
$type = $_FILES['upload_file']['type'];
$size = $_FILES['upload_file']['size'];
$tmpname = $_FILES['upload_file']['tmp_name'];
if (!$title) {
$title = $name;
}
if (!$description) {
$description = "No description available.";
}
$string = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',10)),0,10);
$videoname = "$string.mp4";
$date = date("F d, Y"); // October 09, 2010
$srcFile = $tmpname;
mkdir("users/$usercode/uploads/$string");
mkdir("users/$usercode/uploads/$string/HD");
mkdir("users/$usercode/uploads/$string/regular");
mkdir("users/$usercode/uploads/$string/mobile_upload");
mkdir("users/$usercode/uploads/$string/thumbnails");
$destFile1 = "/users/$usercode/uploads/$string/HD/$string.mp4";
$destFile2 = "/users/$usercode/uploads/$string/regular/$string.mp4";
$destFile3 = "/users/$usercode/uploads/$string/mobile_upload/$string.mp4";
$ffmpegPath = "/usr/local/bin/ffmpeg";
$flvtool2Path = "/usr/bin/flvtool2";
$yamdiPath = "/usr/bin/yamdi";
$mp4boxPath = "/usr/local/bin/MP4Box";
// Create our FFMPEG-PHP class
$ffmpegObj = new ffmpeg_movie($srcFile);
// Save our needed variables
$srcWidth = $ffmpegObj->getFrameWidth();
$srcHeight = $ffmpegObj->getFrameHeight();
$srcFPS = $ffmpegObj->getFrameRate();
$srcAB = $ffmpegObj->getAudioBitRate();
$srcAR = $ffmpegObj->getAudioSampleRate();
$res = $srcWidth . "x" . $srcHeight;
// Call our convert using exec()
$iphone1 = getcwd().$destFile1;
$iphone2 = getcwd().$destFile2;
$iphone3 = getcwd().$destFile3;
$low = "854x480";
$local_img = "/users/$usercode/uploads/$string/thumbnails/$string.jpg";
$img = getcwd().$local_img;
$img = preg_replace('/ /','\ ',$img);
$thumb = "$string.jpg";
$cmd = "$ffmpegPath -i $tmpname 2>&1";
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
$interval = rand(0, $total);
}
$img = shell_exec("$ffmpegPath -ss $interval -i $tmpname 2>&1 -s 120x90 -f mjpeg -vframes 1 $img");
if ($srcWidth >= 1280 && $srcHeight >= 720) {
$out1 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre normal -ab $srcAB -ar $srcAR -b 5000k -r $srcFPS -s $res -acodec libfaac $iphone1");
$out2 = shell_exec("$mp4boxPath -inter 0.5 $iphone1");
$out3 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre normal -ab $srcAB -ar $srcAR -b 2000k -r $srcFPS -s $low -acodec libfaac $iphone2");
$out4 = shell_exec("$mp4boxPath -inter 0.5 $iphone2");
$out5 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre slow -ab 64k -ar 44100 -b 500k -r 30 -s $low -acodec libfaac $iphone3");
$out6 = shell_exec("$mp4boxPath -inter 0.5 $iphone3");
echo 1;
}
else {
$out3 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre normal -ab 64k -ar 44100 -b 500k -r 30 -s $low -acodec libfaac $iphone2");
$out4 = shell_exec("$mp4boxPath -inter 0.5 $iphone2");
$out5 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre slow -ab 64k -ar 44100 -b 500k -r 30 -s $low -acodec libfaac $iphone3");
$out6 = shell_exec("$mp4boxPath -inter 0.5 $iphone3");
echo 1;
}
function generate_random($number_of_characters)
{
$characters = array();
$randomchar = "";
$x = 0;
for($i = 48; $i < 123; $i++)
{
if(ctype_alnum(chr($i)))
{
$characters[$x] = chr($i);
$x++;
}
}
for($i = 0; $i < $number_of_characters; $i++)
{
$randomchar .= $characters[rand(0, count($characters) - 1)];
}
return $randomchar;
}
?>
答案 0 :(得分:1)
首先,您的代码非常容易受到SQL注入攻击和shell参数攻击。您需要将所有输入转义为SQL和shell命令!
我认为这是解决这个问题的标准方法:
exec('php ffmpeg_job_processor.php '.escapeshellarg($jobNumber).' > /dev/null &');
在后台运行该脚本脚本将按ID从数据库加载作业,执行所有处理,完成后将更新数据库“完成”。 / LI>
诀窍是在后台执行第二个PHP脚本,这样它就会释放你调用的PHP脚本,继续进行而不会阻塞。您必须将stdout
传递给/dev/null
(Linux框)并使用背景运算符&
。
这里有一些更好的信息:Run a ffmpeg process in the background