perl - 如何在 Perl 中更改 ffmpeg.exe 的屏幕输出?

标签 perl ffmpeg

我想让屏幕只显示系统调用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);
    }

最佳答案

你看到这个是因为 capture仅在 block 完成后填充变量。如果要分段读取子输出,请使用 pump from IPC::Run .

关于perl - 如何在 Perl 中更改 ffmpeg.exe 的屏幕输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3519759/

相关文章:

perl one liner 搜索模式

android - 简洁的多行子串提取

java - 字符串操作与正则表达式

ubuntu - ffmpeg:视频编解码器 ansi 与 flv 不兼容

android - 视频动态壁纸安卓

ffmpeg - 如何使用 FFmpeg 将两个图像合二为一?

perl - 在 perl 中发送多部分邮件

Perl 客户端 socket->recv() Vs <> 当服务器套接字调用多次发送时

python - ffmpeg 将多张图片放入不同的帧中

php - 为什么使用 PHP 将 shell_exec 转换为变量不起作用?