gpt4 book ai didi

php - 使用 PHP 从 FFMPEG 读取实时输出

转载 作者:行者123 更新时间:2023-12-04 23:24:45 25 4
gpt4 key购买 nike

我正在处理的问题是从 ffmpeg 命令中获取 shell 输出,同时它正在执行并使用 php 将其写入 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如何从正在写入的临时文件中获取内容的答案,而不是直接从进程中获取内容。

最佳答案

ffmpeg 将其状态写入 stderr不是 stdouthttp://php.net/manual/en/function.popen.php示例 2

<?php
error_reporting(E_ALL);

/* Add redirection so we can get stderr. */
$handle = popen('/path/to/executable 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
?>

关于php - 使用 PHP 从 FFMPEG 读取实时输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52569823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com