我一直在玩这个几天。 这个“ffmpegprogress”的东西有帮助,但是很难和我一起工作,而且很难阅读代码。
为了显示ffmpeg的进度,您需要执行以下操作:
从php运行ffmpeg命令,而不用等待响应(对我来说,这是最难的部分)
告诉ffmpeg将其输出发送到一个文件
从前端(AJAX,Flash,不pipe)直接打这个文件还是一个可以从ffmpeg的输出中取出进度的php文件。
以下是我解决每个部分的方法:
我从“ffmpegprogress”得到了以下想法。 这是他做的:一个PHP文件通过一个http套接字调用另一个。 第二个实际运行“exec”,第一个文件挂在上面。 对我来说,他的实施太复杂了。 他正在使用“fsockopen”。 我喜欢CURL。 所以这就是我所做的:
$url = "http://".$_SERVER["HTTP_HOST"]."/path/to/exec/exec.php"; curl_setopt($curlH, CURLOPT_URL, $url); $postData = "&cmd=".urlencode($cmd); $postData .= "&outFile=".urlencode("path/to/output.txt"); curl_setopt($curlH, CURLOPT_POST, TRUE); curl_setopt($curlH, CURLOPT_POSTFIELDS, $postData); curl_setopt($curlH, CURLOPT_RETURNTRANSFER, TRUE); // # this is the key! curl_setopt($curlH, CURLOPT_TIMEOUT, 1); $result = curl_exec($curlH);
将CURLOPT_TIMEOUT设置为1意味着它将等待1秒钟的响应。 最好是会更低。 还有CURLOPT_TIMEOUT_MS需要几毫秒,但它不适合我。
1秒后,CURL挂起,但exec命令仍然运行。 第1部分解决了。
顺便说一句 – 有人build议使用“nohup”命令。 但是这似乎不适合我。
*也! 在你的服务器上有一个可以直接在命令行上执行代码的php文件是一个明显的安全风险。 你应该有一个密码,或以某种方式编码后数据。
2.上面的“exec.php”脚本还必须告诉ffmpeg输出到一个文件。 这里是代码:
exec("ffmpeg -i path/to/input.mov path/to/output.flv 1> path/to/output.txt 2>&1");
请注意“1> path / to / output.txt 2>&1”。 我不是命令行的专家,但从我可以告诉这行说“发送正常输出到这个文件,并发送错误到同一个地方”。 看看这个URL的更多信息: http : //tldp.org/LDP/abs/html/io-redirection.html
3.从前端调用一个php脚本给output.txt文件的位置。 该PHP文件将从文本文件中提取出进度。 以下是我如何做到这一点:
// # get duration of source preg_match("/Duration: (.*?), start:/", $content, $matches); $rawDuration = $matches[1]; // # rawDuration is in 00:00:00.00 format. This converts it to seconds. $ar = array_reverse(explode(":", $rawDuration)); $duration = floatval($ar[0]); if (!empty($ar[1])) $duration += intval($ar[1]) * 60; if (!empty($ar[2])) $duration += intval($ar[2]) * 60 * 60; // # get the current time preg_match_all("/time=(.*?) bitrate/", $content, $matches); $last = array_pop($matches); // # this is needed if there is more than one match if (is_array($last)) { $last = array_pop($last); } $curTime = floatval($last); // # finally, progress is easy $progress = $curTime/$duration;
希望这有助于某人。