1 # -*- coding: ascii -*-
2 ###########################################################################
3 # clive, command line video extraction utility.
5 # Copyright 2009 Toni Gundogdu.
7 # This file is part of clive.
9 # clive is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU General Public License as published by the Free
11 # Software Foundation, either version 3 of the License, or (at your option)
14 # clive is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 # You should have received a copy of the GNU General Public License along
20 # with this program. If not, see <http://www.gnu.org/licenses/>.
21 ###########################################################################
27 use base
'Class::Singleton';
29 use clive
::Error
qw(CLIVE_OK CLIVE_OPTARG CLIVE_SYSTEM);
33 $self->{exec_queue
} = [];
34 $self->{stream_flag
} = 0;
35 $self->{stream_pid
} = -1;
37 my $config = clive
::Config
->instance->config;
38 if ( $config->{exec} ) {
39 if ( $config->{exec} !~ /[;+]$/ ) {
40 clive
::Log
->instance->err( CLIVE_OPTARG
,
41 "--exec expression must be "
42 . "terminated by either ';' or '+'" );
47 if ( !$config->{exec} && $config->{exec_run
} ) {
48 clive
::Log
->instance->err( CLIVE_OPTARG
,
49 "--exec-run depends on --exec" );
57 my $config = clive
::Config
->instance->config;
58 if ( $config->{exec} && $config->{exec_run
} ) {
60 push( @
{ $self->{exec_queue
} }, $$props->filename );
63 return $self->{exec_queue
};
67 my $config = clive
::Config
->instance->config;
68 return if !$config->{exec_run
};
71 if ( $config->{exec} =~ /;$/ ) { # Semi
72 foreach ( @
{ $self->{exec_queue
} } ) {
73 my $cmd = $config->{exec};
80 my $cmd = sprintf( "%s ", $config->{exec} );
83 $cmd .= sprintf( '"%s" ', $_ ) foreach ( @
{ $self->{exec_queue
} } );
91 waitpid( $self->{stream_pid
}, 0 )
92 if $self->{stream_flag
};
94 $self->{stream_flag
} = 0;
95 $self->{stream_pid
} = -1;
99 my ( $self, $percent, $props ) = @_;
100 my $config = clive
::Config
->instance->config;
101 if ( $config->{stream
}
102 && $config->{stream_exec
}
103 && !$self->{stream_flag
} )
105 _forkStreamer
( $self, \
$config, $props )
106 if ( $percent >= $config->{stream
} );
111 my ( $self, $props ) = @_;
113 my $config = clive
::Config
->instance->config;
114 my $cmd = $config->{stream_exec
};
115 my $lnk = $$props->video_link;
117 $cmd =~ s/%i/"$lnk"/g;
119 my $log = clive
::Log
->instance;
120 $log->out("stream ...");
122 my $n = system($cmd);
125 $log->out("done.\n");
128 $log->errn( CLIVE_SYSTEM
, "failed to execute: `$!'" );
131 $log->errn( CLIVE_SYSTEM
, "child exited with: " . ( $n >> 8 ) );
136 my ( $self, $config, $props ) = @_;
138 $self->{stream_flag
} = 1;
141 clive
::Log
->instance->errn( CLIVE_SYSTEM
, "fork: $!" );
143 elsif ( $child == 0 ) {
144 my $cmd = $$config->{stream_exec
};
145 my $fname = $$props->filename;
146 $cmd =~ s/%i/"$fname"/g;
154 # Businessmen they, they drink my wine.