Exec.pm: tweak --stream-pass message.
[clive.git] / lib / clive / Exec.pm
blob0403ba25288a1ca3221cc4e1c28982a0fef260f5
1 # -*- coding: ascii -*-
2 ###########################################################################
3 # clive, command line video extraction utility.
4 # Copyright 2007, 2008, 2009 Toni Gundogdu.
6 # This file is part of clive.
8 # clive is free software: you can redistribute it and/or modify it under
9 # the terms of the GNU General Public License as published by the Free
10 # Software Foundation, either version 3 of the License, or (at your option)
11 # any later version.
13 # clive is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 # details.
18 # You should have received a copy of the GNU General Public License along
19 # with this program. If not, see <http://www.gnu.org/licenses/>.
20 ###########################################################################
21 package clive::Exec;
23 use warnings;
24 use strict;
26 use base 'Class::Singleton';
28 use clive::Error qw(CLIVE_OK CLIVE_OPTARG CLIVE_SYSTEM);
30 sub init {
31 my $self = shift;
32 $self->{exec_queue} = [];
33 $self->{stream_flag} = 0;
34 $self->{stream_pid} = -1;
36 my $config = clive::Config->instance->config;
37 if ( $config->{exec} ) {
38 if ( $config->{exec} !~ /[;+]$/ ) {
39 clive::Log->instance->err( CLIVE_OPTARG,
40 "--exec expression must be "
41 . "terminated by either ';' or '+'" );
42 exit(CLIVE_OPTARG);
46 if ( !$config->{exec} && $config->{exec_run} ) {
47 clive::Log->instance->err( CLIVE_OPTARG,
48 "--exec-run depends on --exec" );
49 exit(CLIVE_OPTARG);
53 sub queue {
54 my $self = shift;
55 if (@_) {
56 my $config = clive::Config->instance->config;
57 if ( $config->{exec} && $config->{exec_run} ) {
58 my $props = shift;
59 push( @{ $self->{exec_queue} }, $$props->filename );
62 return $self->{exec_queue};
65 sub runExec {
66 my $config = clive::Config->instance->config;
67 return if !$config->{exec_run};
69 my $self = shift;
70 if ( $config->{exec} =~ /;$/ ) { # Semi
71 foreach ( @{ $self->{exec_queue} } ) {
72 my $cmd = $config->{exec};
73 $cmd =~ s/%i/"$_"/g;
74 $cmd =~ tr{;}//d;
75 system("$cmd");
78 else { # Plus
79 my $cmd = sprintf( "%s ", $config->{exec} );
80 $cmd =~ s/%i//g;
81 $cmd =~ tr{+}//d;
82 $cmd .= sprintf( '"%s" ', $_ ) foreach ( @{ $self->{exec_queue} } );
83 system("$cmd");
87 sub resetStream {
88 my $self = shift;
90 waitpid( $self->{stream_pid}, 0 )
91 if $self->{stream_flag};
93 $self->{stream_flag} = 0;
94 $self->{stream_pid} = -1;
97 sub runStream {
98 my ( $self, $percent, $props ) = @_;
99 my $config = clive::Config->instance->config;
100 if ( $config->{stream}
101 && $config->{stream_exec}
102 && !$self->{stream_flag} )
104 _forkStreamer( $self, \$config, $props )
105 if ( $percent >= $config->{stream} );
109 sub passStream {
110 my ( $self, $props ) = @_;
112 my $config = clive::Config->instance->config;
113 my $cmd = $config->{stream_exec};
114 my $lnk = $$props->video_link;
116 $cmd =~ s/%i/"$lnk"/g;
118 my $log = clive::Log->instance;
119 $log->out("stream ...");
121 my $n = system($cmd);
123 if ( $n == 0 ) {
124 $log->out("done.\n");
126 elsif ( $n == -1 ) {
127 $log->errn( CLIVE_SYSTEM, "failed to execute: `$!'" );
129 else {
130 $log->errn( CLIVE_SYSTEM, "child exited with: " . ( $n >> 8 ) );
134 sub _forkStreamer {
135 my ( $self, $config, $props ) = @_;
137 $self->{stream_flag} = 1;
138 my $child = fork;
139 if ( $child < 0 ) {
140 clive::Log->instance->errn( CLIVE_SYSTEM, "fork: $!" );
142 elsif ( $child == 0 ) {
143 my $cmd = $$config->{stream_exec};
144 my $fname = $$props->filename;
145 $cmd =~ s/%i/"$fname"/g;
146 system("$cmd");
147 exit(CLIVE_OK);
153 # Businessmen they, they drink my wine.