| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # -*- cperl -*- | ||
| 3 | # PODNAME: awspollymp3.pl | ||
| 4 | # ABSTRACT: script converting textfile named $1 | ||
| 5 | # into file $2 | ||
| 6 | # with engine:voice $3 | ||
| 7 | # using AWS Polly | ||
| 8 | our $VERSION = '20261115.1249'; # VERSION | ||
| 9 | |||
| 10 | |||
| 11 | use v5.32; | ||
| 12 | use IO::File; | ||
| 13 | use IPC::Run; | ||
| 14 | |||
| 15 | # Check arguments | ||
| 16 | die( "Usage: $0 <input_text_file> <output_audio_file> <region:engine:voice>\n" ) | ||
| 17 | unless( 3 == @ARGV ); | ||
| 18 | |||
| 19 | my ( $textfilename, $audiofilename, $regionenginevoice ) = @ARGV; | ||
| 20 | |||
| 21 | # determine format from audiofilename | ||
| 22 | my $format = $audiofilename; | ||
| 23 | $format =~ s/.+\.([^\.]+)/$1/; | ||
| 24 | foreach ( $format ) { | ||
| 25 | /^mp3$/ and do { | ||
| 26 | $format = 'mp3'; | ||
| 27 | last; | ||
| 28 | }; | ||
| 29 | /^ogg$/ and do { | ||
| 30 | $format = 'ogg_vorbis'; | ||
| 31 | last; | ||
| 32 | }; | ||
| 33 | die( "Error: could not guess audio format based on output audiofilename\n" ); | ||
| 34 | } | ||
| 35 | |||
| 36 | # determine engine and voice | ||
| 37 | my ( $region, $engine, $voice ) = split( /:/, $regionenginevoice ); | ||
| 38 | die( "Invalid region, engine or voice format. Use <region:engine:voice>.\n" ) | ||
| 39 | |||
| 40 | unless( defined $region and defined $engine and defined $voice ); | ||
| 41 | |||
| 42 | # read text file | ||
| 43 | my $textfile = IO::File->new(); | ||
| 44 | $textfile->open( "<$textfilename" ) | ||
| 45 | or die( "Error: cannot open '$textfilename'\n" ); | ||
| 46 | my $text = do { local $/; <$textfile> }; | ||
| 47 | |||
| 48 | # Run polly, run! | ||
| 49 | my $command = [ | ||
| 50 | 'aws', | ||
| 51 | 'polly', | ||
| 52 | 'synthesize-speech', | ||
| 53 | '--output-format', | ||
| 54 | $format, | ||
| 55 | '--engine', | ||
| 56 | $engine, | ||
| 57 | '--voice-id', | ||
| 58 | $voice, | ||
| 59 | '--text', | ||
| 60 | $text, | ||
| 61 | # '--text-type', | ||
| 62 | # 'ssml', | ||
| 63 | $audiofilename | ||
| 64 | ]; | ||
| 65 | my $out; | ||
| 66 | IPC::Run::run( $command, | ||
| 67 | '>', \$out ); | ||
| 68 | exit($?); | ||
| 69 | |||
| 70 | __END__ |