【发布时间】:2018-09-29 15:27:09
【问题描述】:
我正在处理的问题是在执行 ffmpeg 命令时从它获取 shell 输出,并使用 php.ini 将其写入 html 页面。
经过一些研究,我在这里发现了一个非常相似的请求:Update page content from live PHP and Python output using Ajax,这似乎很完美,但它根本不起作用。
基本思想是使用 AJAX 请求来调用 PHP 脚本,该脚本应该执行命令并从进程中回显实时读取的内容,注意使用 this.readyState==3 (否则 JS 脚本只有在完成时才会收到响应)
对于 PHP 部分,我尝试使用上面答案中的代码,(显然适应了我的需要):
function liveExecuteCommand($command){
while (@ ob_end_flush()); // end all output buffers if any
$proc = popen($command, 'r');
$live_output = "";
$complete_output = "";
while (!feof($proc))
{
$live_output = fread($proc, 4096);
$complete_output = $complete_output . $live_output;
echo "<pre>$live_output</pre>";
@ flush();
}
pclose($proc);
}
对于我使用的 AJAX 部分
function getLiveStream(){
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (this.readyState == 3) {
document.getElementById("result").innerHTML = this.responseText;
}
};
var url = 'process/getlive';
ajax.open('GET', url,true);
ajax.send();
}
遗憾的是,这不起作用。
正在执行的命令是这样的:'ffmpeg.exe -i "C:/Users/BACKUP/Desktop/xampp/htdocs/testarea/test.mp4" -map 0:0 -map 0:1 -c:v libx264 -preset fast -crf 26 -c:a libmp3lame -ar 24000 -q:a 5 "C:\Users\BACKUP\Desktop\xampp\htdocs\testarea\output/test.mkv"',我对其进行了测试并且可以正常工作。
当我在其中运行 html 页面和 ajax 脚本时,ffmpeg 命令甚至没有运行,因为我在任务管理器中进行了检查。它只是返回一个空白文本。
当我自己运行 php 脚本时,命令运行,文件被转换但它根本不回显任何东西。
经过更多研究,我还发现了这个页面,它似乎是为了这个确切目的而制作的:https://github.com/4poc/php-live-transcode/blob/master/stream.php
相关部分在最后,之前的代码用于处理特定于 ffmpeg 的选项。但它也没有奏效,结果完全相同。
现在我正在考虑简单地将输出写入文件并动态读取它,但我真的很想知道它们都不适合我的原因。
编辑:PHP Execute shell command asynchronously and retrieve live output 回答如何从正在写入的临时文件中获取内容,而不是直接从进程中获取内容。
【问题讨论】:
标签: php ffmpeg pipe output live