speak.pl
perl
posted: Feb, 8th 2012 | jump to bottom
#!/usr/bin/perl #-------------------------------------------------- # Usage: # echo "Hello world" | ./speak.pl en speech.mp3 # cat file.txt | ./speak.pl en speech.mp3 # # # Prerequisites: # sudo apt-get install libwww-perl sox libsox-fmt-mp3 # # Compiling sox: # Older versions of sox package might not have support for mp3 codec, # so just download sox from http://sox.sourceforge.net/ # install packages libmp3lame-dev libmad0-dev # and compile sox #-------------------------------------------------- use LWP; use strict; exit; } my $language = $ARGV[0]; #"sk" my $all_mp3_out = $ARGV[1]; my $TMP_DIR = "$all_mp3_out.tmp"; my $RECAPTCHA_URL = "http://www.google.com/sorry/?continue=http%3A%2F%2Ftranslate.google.com%2Ftranslate_tts%3Ftl=en%26q=Your+identity+was+successfuly+confirmed."; my $RECAPTCHA_SLEEP_SECONDS = 5; my $SYSTEM_WEBBROWSER = "firefox"; my $silence_duration_paragraphs = 0.8; my $silence_duration_sentences = 0.2; my $silence_duration_comma = 0.1; my $silence_duration_brace = 0.1; my $silence_duration_semicolon = 0.2; my $silence_duration_words = 0.05; my @headers = ( 'Host' => 'translate.google.com', 'User-Agent' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.5) Gecko/20091109 Ubuntu/9.10 (karmic) Firefox/3.5.5', 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language' => 'en-us,en;q=0.5', 'Accept-Encoding' => 'gzip,deflate', 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Keep-Alive' => '300', 'Connection' => 'keep-alive', ); my $browser = LWP::UserAgent->new; my @all_mp3s = (); my $sentence_idx = 0; # For each input line while (my $line = <STDIN>) { # Check for empty lines - paragraphs separator if ($line =~ /^\s*$/) { } else { my $sentence = ""; # For each word for (my $i=0; $i<scalar(@words); $i++) { my $word = $words[$i]; $sentence .= " $word"; # add another word to the sentence my $say = 0; my $silence_duration = 0.0; # Remove the last word; $say = 1; $silence_duration = $silence_duration_words; $i --; # one word back } # If a separator was found $say = 1; $silence_duration = $silence_duration_sentences; } $say = 1; $silence_duration = $silence_duration_comma; } $say = 1; $silence_duration = $silence_duration_semicolon; } $say = 1; $silence_duration = $silence_duration_brace; } # If there are no more words $say = 1; $silence_duration = $silence_duration_words; } if ($say) { $sentence = ""; # start a new sentence } } } } JoinMp3s(\@all_mp3s, $all_mp3_out); sub JoinMp3s() { Exec("sox @{$mp3s_ref} $mp3_out"); } sub SilenceToMp3() { Exec("sox -n -r 22050 $mp3_out trim 0.0 $duration"); } sub SentenceToMp3() { $sentence =~ s/ /+/g; } #print "mp3_out: $mp3_out\n"; #print "http://translate.google.com/translate_tts?q=$sentence\n"; my $recaptcha_waiting = 0; my $resp; while (1) { $resp = $browser->get("http://translate.google.com/translate_tts?tl=$language&q=$sentence", @headers); # open my $fh, '<', "recaptcha_response.html" or die "error opening file: $!"; # $resp = do { local $/; <$fh> }; if ($resp->content =~ "<!DOCTYPE") { if (!$recaptcha_waiting) { $recaptcha_waiting = 1; ReCaptcha(); } } else { last; } PrintWaitingDot(); } } } sub PrintWaitingDot() { $|=1; } sub ReCaptcha() { } sub TrimSilence() { if ($mp3 eq "") { } my $mp3_out = $mp3; $mp3_out =~ s/\.mp3$/_trim.mp3/; Exec(" sox $mp3 -p silence 1 0.1 -40d \\ | sox -p -p reverse \\ | sox -p -p silence 1 0.1 -40d \\ | sox -p $mp3_out reverse "); } sub Exec() { # print "exec $cmd\n"; }
46 views




