如果JavaScript更新进度条,JavaScript可以直接执行步骤2:
[此示例需要 dojo]
1 php: 开始转换并将状态写入文本文件 - 示例语法:
exec("ffmpeg -i path/to/input.mov path/to/output.flv 1>path/to/output.txt 2>&1");
对于第二部分,我们只需要使用JavaScript来读取文件。
以下示例使用dojo.request进行AJAX,但您也可以使用jQuery或vanilla或其他库:
[2] js:从文件中获取进度:
var _progress = function(i){
i++;
var logfile = 'path/to/output.txt';
request.post(logfile).then( function(content){
var duration = 0, time = 0, progress = 0;
var result = {};
var matches = (content) ? content.match(/Duration: (.*?), start:/) : [];
if( matches.length>0 ){
var rawDuration = matches[1];
var ar = rawDuration.split(":").reverse();
duration = parseFloat(ar[0]);
if (ar[1]) duration += parseInt(ar[1]) * 60;
if (ar[2]) duration += parseInt(ar[2]) * 60 * 60;
matches = content.match(/time=(.*?) bitrate/g);
console.log( matches );
if( matches.length>0 ){
var rawTime = matches.pop();
if (lang.isArray(rawTime)){
rawTime = rawTime.pop().replace('time=','').replace(' bitrate','');
} else {
rawTime = rawTime.replace('time=','').replace(' bitrate','');
}
ar = rawTime.split(":").reverse();
time = parseFloat(ar[0]);
if (ar[1]) time += parseInt(ar[1]) * 60;
if (ar[2]) time += parseInt(ar[2]) * 60 * 60;
progress = Math.round((time/duration) * 100);
}
result.status = 200;
result.duration = duration;
result.current = time;
result.progress = progress;
console.log(result);
if(progress==0 && i>20){
console.log('{"status":-400, "error":"there is no progress while we tried to encode the video" }');
return;
} else if(progress<100){
setTimeout(function(){ _progress(i); }, 400);
}
} else if( content.indexOf('Permission denied') > -1) {
console.log('{"status":-400, "error":"ffmpeg : Permission denied, either for ffmpeg or upload location ..." }');
}
},
function(err){
if(i<20){
setTimeout(function(){ _progress(0); }, 400);
} else {
console.log('{"status":-400, "error":"there is no progress while we tried to encode the video" }');
console.log( err );
}
return;
});
}
setTimeout(function(){ _progress(0); }, 800);
[
方括号了吗?)数组$matches
。如果你传递一个数组,因为它是通过引用(&
)传递的,这个数组将被覆盖为结果。如果你放入一个尚不存在的变量,它将被创建并赋予该值。PHP的另一个怪癖,为什么返回值不能是我们想要的呢?哦,算了... - Jimbo