dtpk04526211 2016-02-21 08:17
浏览 87

PHP Ajax XMLHttpRequest popen实时进度

I'm trying to output live : the ffmpeg progress

It works great when I execute the php file alone : many lines with text appearing one after an other...

••• But when it's a XMLHttpRequest context, the output is 1 line empty

PHP working when not in AJAX situation :

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();
}

Does someone know why in AJAX it's blank and only 1 line ? how can this be fixed ?

Regards

  • 写回答

1条回答 默认 最新

  • duan0065626385 2016-02-21 22:29
    关注

    I have a "work around" temporary solution...

    //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 FILE

    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
    
    ?>
    

    It's working very well except for long videos...

    Is someone that (knows FFMEG well) know a better way to chec

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行