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 ;
2019-05-14 11:27:54 +02:00
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 ;
2019-05-27 13:33:51 +02:00
my $ mp3 ;
2019-05-25 10:11:09 +02:00
2019-01-16 12:21:51 +01:00
GetOptions ( "help" = > \ $ help ,
2019-05-25 22:40:17 +02:00
"config=s" = > \ $ config ,
2019-05-29 17:49:43 +02:00
"mp3" = > \ $ mp3 ,
"verbose" = > \ $ verbose ,
2019-05-25 22:40:17 +02:00
"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
2019-05-27 13:33:51 +02:00
- - mp3 generate mp3 files
2019-01-16 12:21:51 +01:00
- - help show this message
- - verbose increase verbosity
- - dryrun print commands without executing them
EOS
2019-05-25 22:40:17 +02:00
;
2019-01-16 12:21:51 +01:00
}
sub read_config {
my ( $ filename ) = @ _ ;
my $ json_text = do { #read all the file in one shot
2019-05-25 22:40:17 +02:00
open ( my $ json_fh , '<:encoding(UTF-8)' , $ filename )
or die ( "Can't open \$filename\": $!\n" ) ;
2019-01-16 12:21:51 +01:00
local $/ ;
2019-05-25 22:40:17 +02:00
<$json_fh>
} ;
2019-01-16 12:21:51 +01:00
my $ json = JSON - > new ;
my $ data = $ json - > decode ( $ json_text ) ;
return $ data ;
}
sub process {
2019-05-27 13:33:51 +02:00
my ( $ config , $ mp3 , $ verbose , $ dryrun ) = @ _ ;
2019-01-16 12:21:51 +01:00
my $ data = read_config ( $ config ) ;
my $ short_date = $ data - > { short_date } ;
2019-05-25 22:40:17 +02:00
my $ year = substr $ short_date , 0 , 4 ;
2019-01-16 12:21:51 +01:00
my $ long_date = $ data - > { long_date } ;
my $ source_name = "libre-a-vous-$short_date" ;
my $ title = "Libre à vous ! du $long_date sur Cause Commune" ;
2021-11-16 11:27:02 +01:00
my $ short_description = $ data - > { short_description } ;
2019-01-16 12:21:51 +01:00
my $ ffmpeg_bin = $ data - > { ffmpeg_bin } ;
2019-05-27 13:33:51 +02:00
2019-05-29 17:49:43 +02:00
my $ option_mp3_meta_data_script ;
my @ formats_files ;
2021-11-16 11:27:02 +01:00
$ short_description =~ /^#(\d+)\s+-/ ;
my $ no_emission = $ 1 ;
2019-05-29 17:49:43 +02:00
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 } } ) {
2019-01-16 12:21:51 +01:00
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 ;
}
2019-05-21 15:02:37 +02:00
return 0 ;
2019-05-21 14:27:39 +02:00
}
2019-01-16 12:21:51 +01:00
}
2019-05-25 22:40:17 +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-05-27 13:33:51 +02:00
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\"" ;
2019-01-16 12:21:51 +01:00
if ( $ dryrun ) {
print "$command\n" ;
} else {
2019-05-21 14:39:15 +02:00
my @ ret = `$command` ;
if ( $? ) {
2019-05-25 10:11:09 +02:00
print "Error while setting metadata in $short_chapter_name\n" ;
2019-05-21 14:39:15 +02:00
if ( $ verbose ) {
print Dumper @ ret ;
print Dumper $ data ;
}
2019-05-21 15:02:37 +02:00
return 0 ;
2019-05-21 14:39:15 +02:00
}
}
2019-01-16 12:21:51 +01:00
2019-05-25 22:40:17 +02:00
# 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 ;
}
}
2019-05-25 10:11:09 +02:00
# renaming to target OGG and MP3
2019-05-29 17:49:43 +02:00
foreach my $ format ( @ formats_files ) {
2019-05-25 22:40:17 +02:00
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
2019-06-30 14:32:41 +02:00
my $ command = "sha1sum $target_name > $target_name.sha1" ;
2019-05-25 22:40:17 +02:00
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 ;
}
}
2021-09-27 09:14:05 +02:00
# generate .size
2021-10-05 10:27:25 +02:00
my $ command = "du -b $target_name |awk '{print \$1}' > $target_name.size" ;
2021-09-27 09:14:05 +02:00
if ( $ dryrun ) {
print "$command\n" ;
} else {
my @ ret = `$command` ;
if ( $? ) {
print "Error while generation size of $target_name\n" ;
if ( $ verbose ) {
print Dumper @ ret ;
print Dumper $ data ;
}
return 0 ;
}
}
# generate duration
# my $command = "ffprobe $target_name -show_entries format=duration -sexagesimal 2> /dev/null |grep duration |sed '1,\$s/duration=//' > $target_name.duration";
my $ command = "mediainfo --Inform='General;%Duration/String3%' $target_name > $target_name.duration" ;
if ( $ dryrun ) {
print "$command\n" ;
} else {
my @ ret = `$command` ;
if ( $? ) {
print "Error while generation duration of $target_name\n" ;
if ( $ verbose ) {
print Dumper @ ret ;
print Dumper $ data ;
}
return 0 ;
}
}
2019-05-25 22:40:17 +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" ;
2019-06-13 15:36:55 +02:00
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\"" ;
2019-01-16 12:21:51 +01:00
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 ;
}
2019-05-21 15:02:37 +02:00
return 0 ;
2019-01-16 12:21:51 +01:00
}
}
2019-05-21 14:39:15 +02:00
2019-05-25 22:40:17 +02:00
# renaming to target OGG and MP3
2019-05-29 17:49:43 +02:00
foreach my $ format ( @ formats_files ) {
2019-05-25 22:40:17 +02:00
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
2019-06-30 14:32:41 +02:00
my $ command = "sha1sum $source_name.$format > $source_name.$format.sha1" ;
2019-05-25 22:40:17 +02:00
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 ;
}
}
2021-10-05 08:36:45 +02:00
# duration
my $ command = "mediainfo --Inform=\"Audio;%Duration/String3%\" $source_name.$format > $source_name.$format.duration" ;
if ( $ dryrun ) {
print "$command\n" ;
} else {
my @ ret = `$command` ;
if ( $? ) {
print "Error while generation duration of $source_name.$format\n" ;
if ( $ verbose ) {
print Dumper @ ret ;
print Dumper $ data ;
}
return 0 ;
}
}
# size
2021-10-05 10:27:25 +02:00
my $ command = "du -b $source_name.$format |awk '{print \$1}' > $source_name.$format.size" ;
2021-10-05 08:36:45 +02:00
if ( $ dryrun ) {
print "$command\n" ;
} else {
my @ ret = `$command` ;
if ( $? ) {
print "Error while generation size of $source_name.$format\n" ;
if ( $ verbose ) {
print Dumper @ ret ;
print Dumper $ data ;
}
return 0 ;
}
}
2019-05-25 22:40:17 +02:00
}
binmode ( STDOUT , ":utf8" ) ;
2019-06-19 09:31:09 +02:00
2021-11-16 11:27:02 +01:00
my $ url = "https://libreavous.org/" . $ no_emission ;
2019-05-27 13:33:51 +02:00
2021-11-17 15:42:15 +01:00
print "\nSe connecter sur le site web de l'émission sur l'article qui correspond à l'émission : https://libreavous.org/" . "$no_emission\n\n" ;
2021-11-16 11:27:02 +01:00
2021-11-17 15:42:15 +01:00
print "Cliquer sur le bouton « Article » qui est en bas dans la zone grisée\n\n" ;
2021-11-16 11:27:02 +01:00
print "Cliquer sur le bouton « Importer l'émission »\n\n" ;
print "Les requêtes effectuées sont affichées et normalement aucune erreur est affichée\n\n" ;
print "Aller sur la page https://www.libreavous.org/ecrire/?exec=admin_vider et cliquer sur « Vider le cache »\n\n" ;
print "Retourner sur la page de l'émission : $url\n\n" ;
print "Si l'import s'est passé correctement, le lecteur pour le podcast de l'émission apparaît ainsi que les boutons pour les podcasts individuels (dans « Sujet principal » et dans « Et aussi »)\n\n" ;
print "Aller sur https://www.libreavous.org. Les liens vers les podcasts doivent être présents\n\n" ;
print "Aller sur https://www.libreavous.org/rss. Les items des podcasts pour le flux RSS doivent être présents\n\n" ;
2019-05-29 17:49:43 +02:00
if ( ! $ mp3 ) {
print "MP3 files not generated, please use --mp3 option to generate them\n" ;
}
2019-05-21 14:39:15 +02:00
2019-01-16 12:21:51 +01:00
}
2019-05-27 13:33:51 +02:00
process ( $ config , $ mp3 , $ verbose , $ dryrun ) ;