gpt4 book ai didi

PHP Ajax XMLHttpRequest popen 实时进度

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

我正在尝试实时输出:ffmpeg 进度

当我单独执行 php 文件时效果很好:许多行一个接一个地出现文本......

••• 但是当它是一个 XMLHttpRequest 上下文时,输出是 1 行空

PHP 在非 AJAX 情况下工作:

ini_set("output_buffering", "0");
ob_implicit_flush(true);
$call_mp4 = ' __FFMPEG Command HERE__ ';
$proc = popen($call_mp4, 'r');
while (!feof($proc)) {
echo '['.date("i:s")."] ".fread($proc, 4096).'<br>';ob_flush();flush();
}

有人知道为什么在 AJAX 中它是空白的并且只有 1 行吗?
这怎么能解决?

问候

最佳答案

我有一个“变通”的临时解决方案......

//JS FILE
var formData = new FormData();
var fileSizeForAllUpload = 0;

var allAllowedExt_arr = __EXTENSIONS ALLOWED HERE__;

// Retrieve the FileList object from the referenced element ID (for multiple uploading files)
var ins = document.getElementById( jXHRinputID ).files.length;
for (var x = 0; x < ins; x++) {

fileName = ''; fileExtension = '';
fileName = document.getElementById( jXHRinputID ).files[x].name ;
fileSize = document.getElementById( jXHRinputID ).files[x].size ; jLog('• fileName -> ' + fileName + ' : fileSize -> ' + fileSize);
fileExtension = fileName.substr((fileName.lastIndexOf('.') + 1));
if ( !in_array( fileExtension , allAllowedExt_arr ) ){ fileExtension = 'notAllowed'; }

// if file too large...
if ( fileSize < 400000000 ) {
formData.append( jXHRinputID+"[]", document.getElementById( jXHRinputID ).files[x]);
fileSizeForAllUpload = fileSizeForAllUpload + fileSize;
}

if ( fileSize >= 400000000 ) { fileName = fileName + ' ' + _file_size_too_large ; }

$('#uploadingFilesNames_'+jXHRinputID).append( '• <img src="'+subdomain_icons+'/files/'+ fileExtension +'.png" width="42px" />' + fileName + '<br>' );

}

if ( fileSizeForAllUpload > 0 ) {
xhr = new XMLHttpRequest();
xhr.open('POST', '___WEB PATH OF THE PHP FILE___'+"?"+params, true);

xhr.upload.addEventListener("loadstart", function(e){
console.log('loadstart');
}, false);

// upload progress
xhr.upload.addEventListener("progress", function(e){
if(e.lengthComputable){
var percentage = Math.round((e.loaded * 100) / e.total);
$("#percentageCalc_"+jXHRinputID).html(percentage + '%');
$("#progressBar_"+jXHRinputID).val(percentage);
}

}, false);

xhr.upload.addEventListener("load", function(e){
console.log('transferCompleteFunction');
}, false);

xhr.onprogress = function(e) {
console.log('onprogress');
// ******** HERE : it's the actual line that output live from the echo stuff;ob_flush();flush();
$('#liveXHRoutput_'+jXHRinputID).html(e.currentTarget.responseText);
}

xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
console.log('Complete');
}
}
xhr.send(formData);
}

PHP文件

ini_set("output_buffering", "0");
ob_implicit_flush(true);

// ******* later in PHP file after move_uploaded_file

$newFile_log = "___path for the log file___";
$call_mp4 = " __FFMPEG Command HERE__ 1> $newFile_log 2>&1 ";
// start the ffmpeg convertion
$proc_mp4 = popen($call_mp4, 'r');

//wait for the log file to be created
while (!file_exists($newFile_log)) { sleep(1); }

while (file_exists($newFile_log)) {
echo '•';ob_flush();flush();sleep(1);
// Check for the word head in the log file
// in ffmpeg I found that the line containing the word global headers: is the end of convertion
if( shell_exec('grep -c "head" '.$newFile_log .' 2>&1') > 0 ) { break; }
}

//In FFMPEG the 1> $newFile_log is to create a loge file
//and the 2>&1 is needed to have the ouput inside the variable

?>

它工作得很好,除了长视频......

是否有人(非常了解 FFMEG)知道更好的检查方法

关于PHP Ajax XMLHttpRequest popen 实时进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35533861/

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