Skip to content

Commit

Permalink
support des proxies audio
Browse files Browse the repository at this point in the history
  • Loading branch information
wazoox committed Nov 19, 2018
1 parent eeecefa commit d30d8b6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 28 deletions.
116 changes: 90 additions & 26 deletions script/pre-post-run/create-proxy.pl
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@
use Sys::CPU q/cpu_count/;

my $nb_cpu = cpu_count();
my $output_picture_dir = '/usr/share/storiqone/cache/pictures/proxy';
my $output_movie_dir = '/usr/share/storiqone/cache/movies/proxy';
my $output_picture_dir = '/usr/share/storiqone-www/cache/pictures/proxy';
my $output_movie_dir = '/usr/share/storiqone-www/cache/movies/proxy';
my $output_sound_dir = '/usr/share/storiqone-www/cache/sound/proxy';
my $config;

$nb_cpu = int( $nb_cpu * 3 / 4 ) if $nb_cpu >= 4;

if ( open my $fd, '<', '/etc/storiq/storiqone.conf' ) {
my $data = do { local $/; <$fd> };

my $config = decode_json $data;
$config = decode_json $data;

$nb_cpu = $config->{'proxy'}->{'nb cpu'}
if exists $config->{'proxy'}->{'nb cpu'};
$output_picture_dir = $config->{'proxy'}->{'picture path'}
if exists $config->{'proxy'}->{'picture path'};
$output_movie_dir = $config->{'proxy'}->{'movie path'}
if exists $config->{'proxy'}->{'movie path'};
$nb_cpu = $config->{'proxy'}{'nb cpu'}
if exists $config->{'proxy'}{'nb cpu'};
$output_picture_dir = $config->{'proxy'}{'picture'}{'path'}
if exists $config->{'proxy'}{'picture'}{'path'};
$output_movie_dir = $config->{'proxy'}{'movie'}{'path'}
if exists $config->{'proxy'}{'movie'}{'path'};
$output_sound_dir = $config->{'proxy'}{'sound'}{'path'}
if exists $config->{'proxy'}{'sound'}{'path'};

close $fd;
}
Expand Down Expand Up @@ -110,7 +114,53 @@
},
);

sub processAudio {
return
if ( defined $config->{'proxy'}{'sound'}{'enabled'}
and not $config->{'proxy'}{'sound'}{'enabled'} );
my ($input) = @_;

my $pid = fork();

return unless defined $pid;

if ( $pid == 0 ) {
nice(10);

my $format = $codec{'mp4'}{'audio'};

my @params = ( '-v', 'quiet', '-i', encode_utf8($input) );

if ( defined $config->{'proxy'}{'sound'}{'duration'} ) {
push @params, '-t', $config->{'proxy'}{'sound'}{'duration'};
}

push @params, '-acodec', $format->{codec};
push @params, '-b:a', $format->{bitrate};
push @params, @{ $format->{extra} }
if scalar( @{ $format->{extra} } ) > 0;

my $filename = md5_hex( encode( 'UTF-8', $input, '-vn' ) ) . '.m4a';
push @params, "$output_sound_dir/$filename";

# print "run: " . join(" ", $ffmpeg, @params) . "\n";
exec $ffmpeg, @params;

exit 1;
}

$nb_processes++;

if ( $nb_processes >= $nb_cpu ) {
wait();
$nb_processes--;
}
}

sub processImage {
return
if ( defined $config->{'proxy'}{'picture'}{'enabled'}
and not $config->{'proxy'}{'picture'}{'enabled'} );
my ($input) = @_;

my $pid = fork();
Expand All @@ -120,9 +170,13 @@ sub processImage {
if ( $pid == 0 ) {
nice(10);

my $image_size =
defined $config->{'proxy'}{'picture'}{'image size'}
? $config->{'proxy'}{'picture'}{'image size'}
: '320x240';
my $filename = $output_picture_dir . '/'
. md5_hex( encode( 'UTF-8', $input ) ) . '.jpg';
my @params = ( '-thumbnail', '320x240', $input, $filename );
. md5_hex( encode( 'UTF-8', $input ) ) . '.jpg';
my @params = ( '-thumbnail', $image_size, $input, $filename );

exec $convert, @params;

Expand All @@ -138,6 +192,9 @@ sub processImage {
}

sub processVideo {
return
if ( defined $config->{'proxy'}{'movie'}{'enabled'}
and not $config->{'proxy'}{'movie'}{'enabled'} );
my ( $input, $format_name ) = @_;

my $pid = fork();
Expand All @@ -147,25 +204,32 @@ sub processVideo {
if ( $pid == 0 ) {
nice(10);

my $video_size =
defined $config->{'proxy'}{'movie'}{'video size'}
? $config->{'proxy'}{'movie'}{'video size'}
: 'cif';
my $format = $codec{$format_name};

my @params =
( '-v', 'quiet', '-i', encode_utf8($input), '-t', '0:0:30' );
my @params = ( '-v', 'quiet', '-i', encode_utf8($input) );

if ( defined $config->{'proxy'}{'movie'}{'duration'} ) {
push @params, '-t', $config->{'proxy'}{'movie'}{'duration'};
}

push @params, '-vcodec', $format->{video}->{codec};
push @params, '-b:v', $format->{video}->{bitrate};
push @params, '-s', 'cif';
push @params, '-s', $video_size;
push @params, @{ $format->{video}->{extra} }
if scalar( @{ $format->{video}->{extra} } ) > 0;
if scalar( @{ $format->{video}->{extra} } ) > 0;

push @params, '-acodec', $format->{audio}->{codec};
push @params, '-b:a', $format->{audio}->{bitrate};
push @params, '-ac', '2', '-ar', '44100';
push @params, @{ $format->{audio}->{extra} }
if scalar( @{ $format->{audio}->{extra} } ) > 0;
if scalar( @{ $format->{audio}->{extra} } ) > 0;

my $filename =
md5_hex( encode( 'UTF-8', $input ) ) . '.' . $format_name;
md5_hex( encode( 'UTF-8', $input ) ) . '.' . $format_name;
push @params, "$output_movie_dir/$filename";

# print "run: " . join(" ", $ffmpeg, @params) . "\n";
Expand All @@ -185,11 +249,9 @@ sub processVideo {
my $archive;
if ( defined $data->{archive}->{main} ) {
$archive = $data->{archive}->{main};
}
elsif ( defined $data->{archive}->{source} ) {
} elsif ( defined $data->{archive}->{source} ) {
$archive = $data->{archive}->{source};
}
else {
} else {
my $sent = {
'finished' => JSON::PP::true,
'data' => {},
Expand Down Expand Up @@ -226,13 +288,15 @@ sub processVideo {

if ( $file->{"mime type"} =~ m#^image/#i ) {
processImage( $file->{path} );
}
else {
} else {
my $info = new Mediainfo( "filename" => $file->{path} );
next unless defined $info->{video_format};

processVideo( $file->{path}, 'mp4' );
processVideo( $file->{path}, 'ogv' );
if ( defined $info->{video_format} ) {
processVideo( $file->{path}, 'mp4' );
processVideo( $file->{path}, 'ogv' );
} elsif ( defined $info->{audio_format} ) {
processAudio( $file->{path} );
}
}

my $sent = {
Expand Down
18 changes: 16 additions & 2 deletions storiqone.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,21 @@
}
},
"proxy": {
"picture path": "/usr/share/storiqone/cache/pictures/proxy/",
"movie path": "/usr/share/storiqone/cache/movies/proxy/"
"movie": {
"path": "/usr/share/storiqone-www/cache/movies/proxy/",
"duration": "0:00:30",
"video size": "cif",
"enabled": true
},
"picture": {
"path": "/usr/share/storiqone-www/cache/pictures/proxy/",
"image size": "320x240",
"enabled": true
},
"sound": {
"path": "/usr/share/storiqone-www/cache/sound/proxy/",
"duration": "0:00:30",
"enabled": true
}
}
}

0 comments on commit d30d8b6

Please sign in to comment.