tags:

views:

51

answers:

1

I want to let the screen only show the output of the continually updating time information while system calling ffmpeg.exe.

I've come up with the following script:

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

I know the script is buggy. But my current question is why the time_info subroutine does not work simultaneously with the ffmpeg subroutine? It seems to start running only when the ffmpeg subroutine finishes. And when the ffmpeg subroutine finishes, the time_info subroutine will give me something like the following:

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

Here, 79.46 is about the duration of the video.

Any pointers? Thanks like always :)

Update:

Thanks to @daxim for sending me on the right track. Now using pump from IPC:Run, I've come up with the following script that is still buggy but that can basically do what I need, i.e., suppress the output from ffmpeg and shows a progress bar of the video converstion.

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);
    }
+1  A: 

You see this because capture fills the variables only after the block is done. If you want to read the child output piecewise, use pump from IPC::Run.

daxim
Thanks @daxim, I'll give it a try.
Mike
@daxim, I've looked at usage example of the ipc::run module but pump does not seem to be able to output only the desired content. Looks like it incrementally outputs everything. Is there anything I can change in the following code to output selectively? Thanks. The script: my @cmd = qw(ffmpeg -i source_video.flv -vcodec wmv2 -acodec wmav2 output_video.wmv);my $h = start \@cmd, \$in, \$out, \$err, timeout( 10 );pump $h;
Mike
Filter output as in your question above so only the desired content is left.
daxim
@daxim, thanks again for sending me on the right track. I've figured out how to use pump from IPC:Run and this module seems to be what I needed in the first place. Thanks :)
Mike