ffmpeg可以显示进度条吗? [英] Can ffmpeg show a progress bar?

查看:1130
本文介绍了ffmpeg可以显示进度条吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ffmpeg将.avi文件转换为.flv文件。由于转换文件需要很长时间才能显示进度条。有人可以指导我如何去做相同的。

I am converting a .avi file to .flv file using ffmpeg. As it takes a long time to convert a file I would like to display a progress bar. Can someone please guide me on how to go about the same.

我知道ffmpeg不得不输出一个文本文件的进度,我必须用ajax调用。但是如何使ffmpeg输出到文本文件的进度?

I know that ffmpeg somehow has to output the progress in a text file and I have to read it using ajax calls. But how do I get ffmpeg to output the progress to the text file?

非常感谢。

推荐答案

我已经玩了几天。那个ffmpegprogress的东西有所帮助,但是很难很好的使用我的设置,很难阅读代码。

I've been playing around with this for a few days. That "ffmpegprogress" thing helped, but it was very hard to get to work with my set up, and hard to read the code.

为了显示进度ffmpeg您需要执行以下操作:

In order to show the progress of ffmpeg you need to do the following:


  1. 从php运行ffmpeg命令,无需等待响应(对我来说,这是最难的告诉ffmpeg将它的输出从前端发送到一个文件

  2. (AJAX,Flash,无论如何)直接或者直接打到该文件,一个php文件,可以从ffmpeg的输出中提取进度。

这是我如何解决每个部分:

Here's how I solved each part:

1。
我从ffmpegprogress得到以下想法。这是他做的:一个PHP文件通过http套接字来调用另一个。第二个实际上运行exec,第一个文件刚刚挂起来。对我来说,他的实施太复杂了。他正在使用fsockopen。我喜欢CURL。所以这里是我做的:

1. I got the following idea from "ffmpegprogress". This is what he did: one PHP file calls another through an http socket. The 2nd one actually runs the "exec" and the first file just hangs up on it. For me his implementation was too complex. He was using "fsockopen". I like CURL. So here's what I did:

$url = "http://".$_SERVER["HTTP_HOST"]."/path/to/exec/exec.php";
curl_setopt($curlH, CURLOPT_URL, $url);
$postData = "&cmd=".urlencode($cmd);
$postData .= "&outFile=".urlencode("path/to/output.txt");
curl_setopt($curlH, CURLOPT_POST, TRUE);
curl_setopt($curlH, CURLOPT_POSTFIELDS, $postData);

curl_setopt($curlH, CURLOPT_RETURNTRANSFER, TRUE);

// # this is the key!
curl_setopt($curlH, CURLOPT_TIMEOUT, 1);
$result = curl_exec($curlH);

将CURLOPT_TIMEOUT设置为1表示它将等待1秒的响应。优选地,这将较低。还有CURLOPT_TIMEOUT_MS需要几毫秒,但对我来说并不奏效。

Setting CURLOPT_TIMEOUT to 1 means it will wait 1 second for a response. Preferably that would be lower. There is also the CURLOPT_TIMEOUT_MS which takes milliseconds, but it didn't work for me.

1秒钟后,CURL挂起,但是exec命令仍然运行。第一部分解决了。

After 1 second, CURL hangs up, but the exec command still runs. Part 1 solved.

BTW - 有几个人建议使用nohup命令。但这似乎对我来说似乎不起作用。

BTW - A few people were suggesting using the "nohup" command for this. But that didn't seem to work for me.

还有!在服务器上有一个可以直接在命令行上执行代码的php文件是一个明显的安全隐患。您应该有一个密码,或以某种方式对帖子数据进行编码。

*ALSO! Having a php file on your server that can execute code directly on the command line is an obvious security risk. You should have a password, or encode the post data in some way.

2。
上面的exec.php脚本还必须告诉ffmpeg输出到一个文件。这里是代码:

2. The "exec.php" script above must also tell ffmpeg to output to a file. Here's code for that:

exec("ffmpeg -i path/to/input.mov path/to/output.flv 1> path/to/output.txt 2>&1");

请注意1路径/ to / output.txt 2>& 1。我不是命令行专家,但从我可以告诉这一行说发送正常输出到这个文件,并发送错误到同一个地方。查看此网址了解更多信息: http://tldp.org/LDP/abs /html/io-redirection.html

Note the "1> path/to/output.txt 2>&1". I'm no command line expert, but from what I can tell this line says "send normal output to this file, AND send errors to the same place". Check out this url for more info: http://tldp.org/LDP/abs/html/io-redirection.html

3。
从前端调用一个php脚本给它output.txt文件的位置。然后,该php文件将从文本文件中拉出进度。这是我如何做的:

3. From the front end call a php script giving it the location of the output.txt file. That php file will then pull out the progress from the text file. Here's how I did that:

// # get duration of source
preg_match("/Duration: (.*?), start:/", $content, $matches);

$rawDuration = $matches[1];

// # rawDuration is in 00:00:00.00 format. This converts it to seconds.
$ar = array_reverse(explode(":", $rawDuration));
$duration = floatval($ar[0]);
if (!empty($ar[1])) $duration += intval($ar[1]) * 60;
if (!empty($ar[2])) $duration += intval($ar[2]) * 60 * 60;


// # get the current time
preg_match_all("/time=(.*?) bitrate/", $content, $matches); 

$last = array_pop($matches);
// # this is needed if there is more than one match
if (is_array($last)) {
    $last = array_pop($last);
}

$curTime = floatval($last);


// # finally, progress is easy
$progress = $curTime/$duration;

希望这有帮助。

这篇关于ffmpeg可以显示进度条吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆