我想让屏幕只显示系统调用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的泵:运行,我已经提出了以下脚本仍然有问题,但基本上可以做我需要的,即,抑制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);
}