Guest User

speech

a guest
Jan 26th, 2012
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.46 KB | None | 0 0
  1. #!/usr/bin/perl
  2. ########################################################################
  3. #
  4. # $Id: google-voice-search.pl,v 1.1 2011-04-13 06:06:39 gosha Exp $
  5. #
  6. # Copyright (c) 2011 Okunev Igor <igor[at]prv.mts-nn.ru>
  7. #
  8. ########################################################################
  9.  
  10. require LWP::UserAgent;
  11.  
  12. my ( $file, ) = @ARGV;
  13.  
  14. my ( $url, $use_converter );
  15.  
  16. unless ( $file ) {
  17.     die "Usage: $0 file.flac lang\n\twhere lang is ru or en\n";
  18. }
  19.     $url = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US";
  20.  
  21.  
  22. #
  23. # to FLAC
  24. #
  25. unless ( $file =~ /\.flac$/i ) {
  26.     system "ffmpeg -i $file -ar 16000 $file.flac";
  27.     if ( $? ) {
  28.         die "Can't convert file $file to $file.flac: $?\n";
  29.     } else {
  30.         $file .= '.flac';
  31.         $use_converter = 1;
  32.     }
  33. }
  34.  
  35. my $file_info = `file $file`;
  36.  
  37. if ( $file_info =~ /FLAC audio.*\s(\d+)\s*kHz/ ) {
  38.     $file_info = $1.'000';
  39. } else {
  40.     unlink $file if $use_converter;
  41.     die "Incorrect FLAC file: $file_info\n";
  42. }
  43.  
  44. open( FILE, "<$file" ) || die "Can't open input file[$file]: $!\n";
  45. undef $/; my $audio = <FILE>; $/ = "\n";
  46. close(FILE);
  47.  
  48. unlink $file if $use_converter;
  49.  
  50. my $ua = LWP::UserAgent->new( debug => 1 );
  51.  
  52. my $response = $ua->post($url, Content_Type => "audio/x-flac; rate=$file_info", Content => $audio);
  53.  
  54. if ( $response->is_success ) {
  55.     print $response->content;
  56. } else {
  57.     use Data::Dumper;
  58.     delete $response->{'_request'}->{'_content'};
  59.     print STDERR Dumper($response);
  60.  
  61.    
  62. }
Advertisement
Add Comment
Please, Sign In to add comment