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)
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
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 ###########################################################################
26 use base
'Class::Singleton';
28 use clive
::Error
qw(CLIVE_OK CLIVE_OPTARG CLIVE_SYSTEM);
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 '+'" );
46 if ( !$config->{exec} && $config->{exec_run
} ) {
47 clive
::Log
->instance->err( CLIVE_OPTARG
,
48 "--exec-run depends on --exec" );
56 my $config = clive
::Config
->instance->config;
57 if ( $config->{exec} && $config->{exec_run
} ) {
59 push( @
{ $self->{exec_queue
} }, $$props->filename );
62 return $self->{exec_queue
};
66 my $config = clive
::Config
->instance->config;
67 return if !$config->{exec_run
};
70 if ( $config->{exec} =~ /;$/ ) { # Semi
71 foreach ( @
{ $self->{exec_queue
} } ) {
72 my $cmd = $config->{exec};
79 my $cmd = sprintf( "%s ", $config->{exec} );
82 $cmd .= sprintf( '"%s" ', $_ ) foreach ( @
{ $self->{exec_queue
} } );
90 waitpid( $self->{stream_pid
}, 0 )
91 if $self->{stream_flag
};
93 $self->{stream_flag
} = 0;
94 $self->{stream_pid
} = -1;
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
} );
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);
124 $log->out("done.\n");
127 $log->errn( CLIVE_SYSTEM
, "failed to execute: `$!'" );
130 $log->errn( CLIVE_SYSTEM
, "child exited with: " . ( $n >> 8 ) );
135 my ( $self, $config, $props ) = @_;
137 $self->{stream_flag
} = 1;
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;
153 # Businessmen they, they drink my wine.