#!/usr/bin/perl # Copyright (C) 2019 Quentin GIBEAUX # Copyright (C) 2019 Frédéric COUCHET # Copyright (C) 2019 Christian Pierre MOMON # # 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 . use strict; use Getopt::Long; use JSON; use Data::Dumper; use utf8; my ($help,$config,$verbose,$dryrun); my $meta_data_script = "lav-outils/podcasts/scripts/make-metadata-image-podcast.sh"; my $verbose; my $mp3; my $textwebpage="
    \n"; GetOptions ("help" => \$help, "config=s" => \$config, "mp3" => \$mp3, "verbose" => \$verbose, "dryrun" => \$dryrun); if($help) { usage(); } elsif( not $config ) { print " /!\\ Missing config arg\n\n"; usage(); } sub usage { print < }; my $json = JSON->new; my $data = $json->decode($json_text); return $data; } sub process { my ($config,$mp3,$verbose,$dryrun)=@_; my $data = read_config($config); my $short_date = $data->{short_date}; my $year = substr $short_date,0,4; 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 $short_description = $data->{short_description}; my $ffmpeg_bin = $data->{ffmpeg_bin}; my $option_mp3_meta_data_script; my @formats_files; if($mp3) { @formats_files = qw/ogg mp3/; $option_mp3_meta_data_script = "yes"; } else { @formats_files = qw/ogg/; $option_mp3_meta_data_script = "no"; } for my $chapter (values @{$data->{chapters}}) { my $start = $chapter->{start_timestamp}; my $end = $chapter->{end_timestamp}; my $short_chapter_name = $chapter->{short_chapter_name}; my $chapter_title = $chapter->{chapter_title}; # 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; } } # putting metadata my $url = "https://media.april.org/audio/radio-cause-commune/libre-a-vous/emissions/$short_date/$source_name-$short_chapter_name.ogg"; my $command = "$meta_data_script -s \"$source_name-$short_chapter_name.ogg\" -d \"output\" -u \"$url\" -t \"$title - Partie $chapter_title\" -p \"$ffmpeg_bin\" -y \"$year\" -m \"$option_mp3_meta_data_script\""; 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; } } # Update string for web site my $command = "mediainfo --Inform=\"Audio;%Duration/String%\" output.ogg"; if($dryrun) { print "$command\n"; } else { my @ret = `$command`; if($?) { print "Error while updating string for web site\n"; if($verbose) { print Dumper @ret; print Dumper $data; } return 0; } else { chomp($ret[0]); $ret[0] =~ s/s/secondes/ig; $ret[0] =~ s/min/minutes/ig; my $url2 = $url =~ s/\.ogg/\.mp3/r; $textwebpage = $textwebpage . "
  • $chapter_title (format OGG) (et format MP3) ($ret[0])
  • \n"; } } # renaming to target OGG and MP3 foreach my $format (@formats_files) { my $target_name = "$source_name-$short_chapter_name.$format"; my $command = "mv output.$format $target_name"; if($dryrun) { print "$command\n"; } else { my @ret = `$command`; if($?) { print "Error while renaming $short_chapter_name.$format\n"; if($verbose) { print Dumper @ret; print Dumper $data; } return 0; } } # 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; } } } } # 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\" -u \"$url\" -t \"$title - $short_description\" -p \"$ffmpeg_bin\" -y \"$year\" -m \"$option_mp3_meta_data_script\""; 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; } } # renaming to target OGG and MP3 foreach my $format (@formats_files) { my $command = "mv output.$format $source_name.$format"; if($dryrun) { print "$command\n"; } else { my @ret = `$command`; if($?) { print "Error while renaming $source_name.$format\n"; if($verbose) { print Dumper @ret; print Dumper $data; } return 0; } } # hashing my $command = "sha1sum $source_name.$format > $source_name.$format.sha1"; if($dryrun) { print "$command\n"; } else { my @ret = `$command`; if($?) { print "Error while hashing $source_name.$format\n"; if($verbose) { print Dumper @ret; print Dumper $data; } return 0; } } } $textwebpage = $textwebpage . "
\n\n"; binmode(STDOUT, ":utf8"); print "\nText for the web page of the radio program :\n\n$textwebpage\n"; if(! $mp3) { print "MP3 files not generated, please use --mp3 option to generate them\n"; } } process($config,$mp3,$verbose,$dryrun);