ffmpeg php 进度,php – ffmpeg视频编码进度条

javascript应该告诉php开始转换[1]然后做[2] …

[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++;

// THIS MUST BE THE PATH OF THE .txt FILE SPECIFIED IN [1] :

var logfile = 'path/to/output.txt';

/* (example requires dojo) */

request.post(logfile).then( function(content){

// AJAX success

var duration = 0, time = 0, progress = 0;

var result = {};

// get duration of source

var matches = (content) ? content.match(/Duration: (.*?), start:/) : [];

if( matches.length>0 ){

var rawDuration = matches[1];

// convert rawDuration from 00:00:00.00 to seconds.

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;

// get the time

matches = content.match(/time=(.*?) bitrate/g);

console.log( matches );

if( matches.length>0 ){

var rawTime = matches.pop();

// needed if there is more than one match

if (lang.isArray(rawTime)){

rawTime = rawTime.pop().replace('time=','').replace(' bitrate','');

} else {

rawTime = rawTime.replace('time=','').replace(' bitrate','');

}

// convert rawTime from 00:00:00.00 to seconds.

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;

//calculate the progress

progress = Math.round((time/duration) * 100);

}

result.status = 200;

result.duration = duration;

result.current = time;

result.progress = progress;

console.log(result);

/* UPDATE YOUR PROGRESSBAR HERE with above values ... */

if(progress==0 && i>20){

// TODO err - giving up after 8 sec. no progress - handle progress errors here

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) {

// TODO - err - ffmpeg is not executable ...

console.log('{"status":-400, "error":"ffmpeg : Permission denied, either for ffmpeg or upload location ..." }');

}

},

function(err){

// AJAX error

if(i<20){

// retry

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值