lav-outils/podcasts/scripts/make-all-podcasts.pl

206 lines
5.3 KiB
Perl
Raw Normal View History

2019-01-16 12:21:51 +01:00
#!/usr/bin/perl
2019-05-21 15:45:10 +02:00
# Copyright (C) 2019 Quentin GIBEAUX <qgibeaux@april.org>
2019-05-21 15:39:18 +02:00
# Copyright (C) 2019 Frédéric COUCHET <fcouchet@april.org>
# Copyright (C) 2019 Christian Pierre MOMON <cmomon@april.org>
#
# This file is part of lav-outils from "April/Libre à vous !"
#
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2019-01-16 12:21:51 +01:00
use strict;
use Getopt::Long;
use JSON;
use Data::Dumper;
use utf8;
2019-01-16 12:21:51 +01:00
my ($help,$config,$verbose,$dryrun);
my $meta_data_script = "lav-outils/podcasts/scripts/make-metadata-image-podcast.sh";
my $verbose;
GetOptions ("help" => \$help,
2019-05-21 14:27:39 +02:00
"config=s" => \$config,
"verbose" => \$verbose,
"dryrun" => \$dryrun);
2019-01-16 12:21:51 +01:00
if($help) {
usage();
} elsif( not $config ) {
print " /!\\ Missing config arg\n\n";
usage();
}
sub usage {
print <<EOS
2019-05-21 14:27:39 +02:00
Exec commands to cut and process LAV! podcast with json file containing the timestamps and sections to be cut.
Needs JSON perl library (apt install libjson-perl)
2019-01-16 12:21:51 +01:00
$0 --config conf_file.json
--config conf_file.json the configuration file containing stuff
--help show this message
--verbose increase verbosity
--dryrun print commands without executing them
EOS
;
}
sub read_config {
my ($filename) = @_;
my $json_text = do { #read all the file in one shot
open(my $json_fh, '<:encoding(UTF-8)', $filename)
2019-01-16 12:21:51 +01:00
or die("Can't open \$filename\": $!\n");
local $/;
<$json_fh>
};
my $json = JSON->new;
my $data = $json->decode($json_text);
return $data;
}
sub process {
my ($config,$verbose,$dryrun)=@_;
my $data = read_config($config);
my $short_date = $data->{short_date};
my $long_date = $data->{long_date};
my $source_name = "libre-a-vous-$short_date";
my $title = "Libre à vous ! du $long_date sur Cause Commune";
my $ffmpeg_bin = $data->{ffmpeg_bin};
for my $chapter (values @{$data->{chapters}}) {
my $start = $chapter->{start_timestamp};
my $end = $chapter->{end_timestamp};
my $short_chapter_name = $chapter->{short_chapter_name};
2019-01-30 10:02:11 +01:00
my $chapter_title = $chapter->{chapter_title};
2019-01-16 12:21:51 +01:00
# cutting chapter
my $command = "$ffmpeg_bin -y -i $source_name.ogg -vn -acodec copy -ss \"$start\" -to \"$end\" $source_name-$short_chapter_name.ogg";
if($dryrun) {
print "$command\n";
} else {
my @ret = `$command`;
if($?) {
print "Error while cutting $short_chapter_name\n";
if($verbose) {
print Dumper @ret;
print Dumper $data;
}
return 0;
2019-05-21 14:27:39 +02:00
}
2019-01-16 12:21:51 +01:00
}
# putting metadata
my $url = "https://media.april.org/audio/radio-cause-commune/libre-a-vous/emissions/$short_date/$source_name-$short_chapter_name.ogg";
2019-01-30 10:02:11 +01:00
my $command = "$meta_data_script -s \"$source_name-$short_chapter_name.ogg\" -d \"output.ogg\" -u \"$url\" -t \"$title - Partie $chapter_title\" -p \"$ffmpeg_bin\"";
2019-01-16 12:21:51 +01:00
if($dryrun) {
print "$command\n";
} else {
my @ret = `$command`;
if($?) {
print "Error while setting metadata in $short_chapter_name\n";
if($verbose) {
print Dumper @ret;
print Dumper $data;
}
return 0;
2019-05-21 14:27:39 +02:00
}
2019-01-16 12:21:51 +01:00
}
2019-05-21 14:39:15 +02:00
# renaming to target
my $target_name = "$source_name-$short_chapter_name.ogg";
my $command = "mv output.ogg $target_name";
2019-01-16 12:21:51 +01:00
if($dryrun) {
print "$command\n";
} else {
my @ret = `$command`;
if($?) {
print "Error while renaming $short_chapter_name\n";
if($verbose) {
print Dumper @ret;
print Dumper $data;
}
return 0;
2019-05-21 14:27:39 +02:00
}
2019-01-16 12:21:51 +01:00
}
2019-05-21 14:39:15 +02:00
# hashing
my $command = "sha1sum $target_name > $target_name.sha1";
if($dryrun) {
print "$command\n";
} else {
my @ret = `$command`;
if($?) {
print "Error while hashing $target_name\n";
if($verbose) {
print Dumper @ret;
print Dumper $data;
}
return 0;
2019-05-21 14:39:15 +02:00
}
}
2019-01-16 12:21:51 +01:00
}
# putting metadata in main podcast
my $url = "https://media.april.org/audio/radio-cause-commune/libre-a-vous/emissions/$short_date/$source_name.ogg";
my $command = "$meta_data_script -s \"$source_name.ogg\" -d \"output.ogg\" -u \"$url\" -t \"$title\" -p \"$ffmpeg_bin\"";
if($dryrun) {
print "$command\n";
} else {
my @ret = `$command`;
if($?) {
print "Error while setting metadata in $source_name\n";
if($verbose) {
print Dumper @ret;
print Dumper $data;
}
return 0;
2019-01-16 12:21:51 +01:00
}
}
2019-05-21 14:39:15 +02:00
2019-05-21 14:55:58 +02:00
# renaming to target
2019-01-16 12:21:51 +01:00
my $command = "mv output.ogg $source_name.ogg";
if($dryrun) {
print "$command\n";
} else {
my @ret = `$command`;
if($?) {
print "Error while renaming $source_name\n";
if($verbose) {
print Dumper @ret;
print Dumper $data;
}
return 0;
2019-05-21 14:27:39 +02:00
}
2019-01-16 12:21:51 +01:00
}
2019-05-21 14:39:15 +02:00
# hashing
my $command = "sha1sum $source_name.ogg > $source_name.ogg.sha1";
if($dryrun) {
print "$command\n";
} else {
my @ret = `$command`;
if($?) {
print "Error while hashing $source_name.ogg\n";
if($verbose) {
print Dumper @ret;
print Dumper $data;
}
return 0;
2019-05-21 14:39:15 +02:00
}
}
2019-01-16 12:21:51 +01:00
}
process($config,$verbose,$dryrun);