【问题标题】:How do I change the screen output from ffmpeg.exe in Perl?如何在 Perl 中更改 ffmpeg.exe 的屏幕输出?
【发布时间】:2011-04-18 18:12:10
【问题描述】:

我想让屏幕只显示系统调用ffmpeg.exe时不断更新的时间信息的输出。

我想出了以下脚本:

use Capture::Tiny qw/capture/;
use threads;
use threads::shared;

my $stderr :shared;
my $thread1 = threads->create(\&ffmpeg);
my $threads = threads->create(\&time_info,$thread1);
$threads->join();

sub ffmpeg {
    ($stdout, $stderr) = capture {
    system "ffmpeg -i source_video.flv -vcodec wmv2 -acodec wmav2 output_video.wmv";
    };
}

sub time_info {
    while(1){
        $|=1;
        $stderr =~ m{time=(\d+\.\d+)}msg;
        print $1,"\n";
        sleep(1);
    }
}

我知道脚本有问题。但我目前的问题是为什么 time_info 子程序不能与 ffmpeg 子程序同时工作?它似乎只有在 ffmpeg 子例程完成时才开始运行。当 ffmpeg 子例程完成时,time_info 子例程会给我如下信息:

3.28
7.56
11.64
15.80
20.88
25.76
30.84
35.88
40.76
45.80
50.88
55.88
60.88
65.88
71.08
76.32
79.46
3.28
7.56

这里,79.46 大约是视频的时长。

任何指针?一如既往地感谢:)

更新:

感谢@daxim 让我走上正轨。现在使用 IPC:Run 中的泵,我想出了以下脚本,它仍然有问题,但基本上可以满足我的需要,即抑制 ffmpeg 的输出并显示视频转换的进度条。

use strict;
use warnings;
use IPC::Run qw(start pump);
use Term::ProgressBar;

my @cmd = qw(ffmpeg -i source_video.flv -vcodec wmv2 -acodec wmav2 output_video.wmv);

my ($in, $out, $err);

my $harness = start \@cmd, \$in, \$out, \$err;

#Captures the duration of the video...
#Converts hh:mm:ss format to seconds only
pump $harness until ($err =~ m{time=(\d+\.\d+)}msg);
$err =~ m{Duration: (\d+:\d+:\d+\.\d+)}ms;
my $duration = $1;
my ($h, $m, $s) = split /:/, $duration;
$duration = $h * 3600 + $m * 60 + $s;


my $progress = Term::ProgressBar->new ({count => $duration});

#Builds an infinite loop...
#Stops at intervals to print progress information
while(1){
    pump $harness until ($err =~ m{time=(\d+\.\d+)}msg);
    my $so_far = $1;
    $progress->update ($so_far);
    last if ( $duration - $so_far <= 0.5);
    }

【问题讨论】:

    标签: perl ffmpeg


    【解决方案1】:

    您会看到这一点,因为 capture 仅在块完成后填充变量。如果要分段读取子输出,请使用pump from IPC::Run

    【讨论】:

    • 谢谢@daxim,我试试看。
    • 我查看了 ipc::run 模块的使用示例,但 pump 似乎无法仅输出所需的内容。看起来它逐渐输出所有内容。我可以在以下代码中更改什么以选择性地输出吗?谢谢。脚本:我的@cmd = qw(ffmpeg -i source_video.flv -vcodec wmv2 -acodec wmav2 output_video.wmv);我的 $h = 开始 \@cmd,\$in,\$out,\$err,超时(10);泵 $h;
    • 按照上面的问题过滤输出,因此只剩下所需的内容。
    • 再次感谢您让我走上正轨。我已经弄清楚如何使用 IPC:Run 中的泵,这个模块似乎是我首先需要的。谢谢:)
    猜你喜欢
    • 2011-01-15
    • 2012-03-10
    • 1970-01-01
    • 2016-06-09
    • 2021-03-27
    • 2013-09-08
    • 2012-05-28
    • 2021-06-01
    • 1970-01-01
    相关资源
    最近更新 更多