Perl:从fork获取多个STDOUT并刷新内容

时间:2013-01-19 21:09:44

标签: perl

我有一个脚本在linux机器上编码视频。它一次分叉4个进程,除命令外不向屏幕输出。我想取消打印命令,只需打印每个排队的文件,然后开始说“尚未开始”,从启动过程刷新STDOUT,完成后打印“Done:”之类的东西。我不知道从哪里开始。我想保留日志文件,所以我将使用tee。

use File::Find::Rule;
use File::Basename;
use Parallel::ForkManager;
# Defines modules to be used.

%fpslist = (    "23.976" => '24000/1001',
                "24.000" => '24',
                "29.970" => '30000/1001',
                "30.000" => '30',
                "59.940" => '60000/1001',
                "60.000" => '60' );
# Used for lookup in a later section.

@findlist = find( file => name => [ qw/ *.avi *.h264 *.m2v *.mkv *.ts *.vc1 / ], in   => '/mnt/ddrive/videotemp/bluray/' );
# Makes a list of all files of the .avi, .h264, .m2v, .mkv, and .vc1 extensions.

foreach ( @findlist ) {
    ( $file, undef, $ext ) = fileparse( $_, qr/\.[^.]*/ );
    $ext =~ s/.//;
    $realext{$file} = $ext; }
# Takes the list, splits the elements into a filename, path (not needed, ergo "undef"), and extension, and stores into a hash with the filename being the key.

open LIST,"<","/home/tim/movies.txt";
while ( <LIST> ) {
    chomp $_;
    @line = split( /\//, $_ );
    $line{@line[0]} = @line[1]; }
close LIST;
# Gets crop values for movies that need cropping. Each line is a name and then the crop value (<left,top,right,bottom>) separated by a forward slash.
# They are stored in a hash with the filename being the key.

foreach ( keys %realext ) {
    my $fps = `mediainfo --inform="Video;%FrameRate%" /mnt/ddrive/videotemp/bluray/"$_.$realext{$_}"`;
    chomp $fps;
    $array{$_} = ( $_ => { 'extension' => $realext{$_}, 'crop' => $line{$_}, 'fps' => $fpslist{$fps} } ); }
# Takes the first list of all files and gets the original FPS and stores the extensions, crop values, and FPS in a hash with the filename being the key.

my $pm = new Parallel::ForkManager(4); # Forks 4 threads.

foreach $key ( sort keys %array ) {
    $pm->start and next;
    if ( defined $array{$key}{"crop"} ) { $crop = ' --video-filter crop:'.$array{$key}{"crop"}; } else { $crop = ''; }
    $cmd = 'x264 --crf 17 --threads 4 --profile high --level 4.1 --preset veryslow --fps '.$array{$key}{"fps"}.' --demuxer ffms /mnt/ddrive/videotemp/bluray/"'.$key.'.'.$array{$key}{"extension"}.'"'.$crop.' --output /mnt/ddrive/videotemp/"'.$key.'.h264" >> /mnt/ddrive/videotemp/"'.$key.'.log" 2>&1';
    print "\n$cmd\n\n";
    exec($cmd);
    $pm->finish; }
# This is a loop for each key in the last hash created.
# Checks to see if there is a crop value defined, and if so, makes a command string from it.
# It then builds the final command and executes it taking up a thread. When it's through, it moves to new thread until no more keys are in queue.

$pm->wait_all_children; # Waits for all threads to be completed in the previous loop and closes the entire program what that is true.

0 个答案:

没有答案