mizzyさんの技を盗むの巻

MEncoder::Command

package MEncoder::Command;

use strict;
use base qw(Class::Accessor::Fast Class::ErrorHandler);
use IPC::Run qw(start);

our $VERSION = '0.01';

__PACKAGE__->mk_accessors qw(
    input_file output_file mencoder options
);

my %option = (
    video_codec => '-ovc',
    audio_codec => '-oac',
);

sub new {
    my $class = shift;
    my $self = {
        mencoder    => shift || 'mencoder',
        options     => { audio_codec => [], video_codec => [] },
        input_file  => '',
        output_file => '',
    };
    bless $self, $class;
}

sub encode_options {
    my ($self, %args) = @_;
    for (keys %args) {
        next unless (exists $option{$_});
        push @{ $self->options->{$_} },
            (! @{ $self->options->{$_} } ? $option{$_} : qw{}),
            @{ $args{$_} };
    }
}

sub video_options { shift->encode_options(video_codec => [ @_ ]) }
sub audio_options { shift->encode_options(audio_codec => [ @_ ]) }

sub execute {
    my $self = shift;
    my ($in, $out, $err);
    my $h = eval {
        start [
            $self->mencoder,
            $self->input_file,
            @{ $self->options->{video_codec} },
            @{ $self->options->{audio_codec} },
            '-o', $self->output_file,
        ], \$in, \$out, \$err;
    };
    if ($@) {
        $self->error($@);
        return;
    }
    else {
        finish $h or do {
            $self->error($err);
            return;
        };
    }
    return 1;
}

*exec = \&execute;

1;

__END__

=head1 SYNOPSIS

    use MEncoder::Command;

    my $mencoder = MEncoder::Command->new;

    $mencoder->input_file('./test.amc');

    $mencoder->output_file('./test.avi');

    $mencoder->encode_options(
        video_codec => [qw/ lavc /],
        audio_codec => [qw/ mp3lame -lameopts preset=standard /],
    );

    # or
    # $mencoder->video_options('lavc');
    # $mencoder->audio_options(qw/ mp3lame -lameopts preset=standard /);

    $mencoder->exec;

=cut

mizzyさんのコードまるパクリな上に、微妙にインターフェース変えてあって、、厄介なこと、この上なし。

はい、次行ってみよー。