Файл с кодировкой POST base64 в API речи Google с использованием perl

У меня возникают трудности с кодированием аудиофайла FLAC в base64 при публикации строки json в API речи Google. Я заметил несколько \n в ответе от Google, не уверен, что base64 недостаточно хорош, или, возможно, я не полностью понимаю, как создавать такие строки и делать их достаточно json-y, чтобы удовлетворить Google. Я склоняюсь к кодировке utf-8, но моя последняя попытка дальнейшей обработки отправляемой информации оставила меня с горами других сообщений об ошибках от perl. Любые указатели вообще были бы большим подспорьем! (даже если указатель должен отказаться от выполнения этого самостоятельно и заплатить плату за поддержку в Google)

Error message:
    {
      "error": {
        "code": 400,
        "message": "Invalid value at 'audio.content' (TYPE_BYTES), Base64 decoding failed for "ZkxhQwAAACIQABAAAAlJABQpAfQA8AABS+DDBqlWu7Ba27gz/koR4+04AwAAJAAAAAAAAAAAAAAA\nAAAAAAAQAAAAAAAAATAAAAAAAAABQ5kQAAQAACggAAAAcmVmZXJlbmNlIGxpYkZMQUMgMS4zLjAg\nMjAxMzA1MjYAAAAAgQA...

Мой код:

#!/usr/bin/env perl
# Render speech to text using the google cloud speech engine.
#
# Kruft Industries Sept. 2016
#
#
# Intended to replace work by the following(not sure where this is hosted): GNU General Public License Version 2 Copyright (C) 2011 - 2012, Lefteris Zafiris 
# <[email protected]>
#
#
# The script takes as input flac files at 8kHz and returns the following values: status : Return status. 0 means success, non zero values indicating different 
# errors.
#
# Outputs a voice transcription that satisfies the input of sendmailmp3 for freepbx authored by the above Zafiris I am by no means an expert with the perl 
# language, Please forgive any blaring ugliness :)

use utf8;
use MIME::Base64;
use strict;
use warnings; 
use LWP::UserAgent; 

if (!@ARGV || $ARGV[0] eq '-h' || $ARGV[0] eq '--help') {
    print "Speech recognition using google cloud speech api.\n\n";
    print "Usage: $0 [FILES]\n\n";
    exit;
}
my $url = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=API KEY HERE"; 

my @file_list = @ARGV; foreach my $file 
(@file_list) {
    print "Opening $file\n";
    open(my $fh, "<", "$file") or die "Cant read file: $!";
    my $audio = do { local $/; <$fh> };
    close($fh);

my $flac = encode_base64url($audio);

my $json = '{"config":{"encoding":"FLAC","sample_rate":8000,"language_code":"en-US"},"audio":{"content":"' . $flac . '"}}';

my $req = HTTP::Request->new( 'POST', $url );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );

my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);

print $response->as_string; #debug output google's reply headers and message
last if (!$response->is_success);

print $response->content; #debug output the full transcript
    my $doodle = $response->content;
    $doodle =~ s/.*\"transcript\"://g;
$doodle =~ s/}\],.*//g;
$doodle =~ s/^{\"result\":\[\]}/{\"result\":/g;
$doodle =~ s/\R//g;
$doodle =~ s/\*/_/g;
    print $doodle;


}
      sub encode_base64url{
         my($data) = @_;
         return 0 unless $data;
         $data = encode_base64($data);
         $data =~ tr#\-_#+/#;
         return($data);
      }
exit;

person Kruft Industries    schedule 21.09.2016    source источник
comment
Требуется ли Google URL-версия Base64? Ваша версия encode_base64url работает не так, как MIME::Base64::encode_base64url, проверено в эхо-коде от Google, в котором есть '/', '+' и символы новой строки. На самом деле ваша версия выдает тот же результат, что и encode_base64. Версия URL также должна удалить '='.   -  person Christopher Oicles    schedule 22.09.2016


Ответы (1)


Вот исправленный скрипт. Это должно помочь многим, я мог бы обновить заголовок и описание, чтобы предотвратить дублирование вопросов! Спасибо, что указали мне правильное направление, Кристофер Ойклс!

#!/usr/bin/env perl
# Render speech to text using the google cloud speech engine.
#
# Kruft Industries Sept. 2016
#
#
# Intended to replace work by the following(not sure where this is hosted): GNU General Public License Version 2 Copyright (C) 2011 - 2012, Lefteris Zafiris 
# <[email protected]>
#
#
# The script takes as input flac files at 8kHz and returns the following values: status : Return status. 0 means success, non zero values indicating different 
# errors.
#
# Outputs a voice transcription that satisfies the input of sendmailmp3 for freepbx authored by the above Zafiris I am by no means an expert with the perl 
# language, Please forgive any blaring ugliness :)

use utf8;
use MIME::Base64;
use strict;
use warnings; 
use LWP::UserAgent; 

if (!@ARGV || $ARGV[0] eq '-h' || $ARGV[0] eq '--help') {
print "Speech recognition using google cloud speech api.\n\n";
print "Usage: $0 [FILES]\n\n";
exit;
}
my $url = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=API KEY GOES HERE"; 

my @file_list = @ARGV; foreach my $file 
(@file_list) {
print "Opening $file\n";
open(my $fh, "<", "$file") or die "Cant read file: $!";
my $audio = do { local $/; <$fh> };
close($fh);

my $flac = encode_base64url($audio);

my $json = '{"config":{"encoding":"FLAC","sample_rate":8000,"language_code":"en-US"},"audio":{"content":"' . $flac . '"}}';

my $req = HTTP::Request->new( 'POST', $url );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );

my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);

#print $response->as_string; #debug output google's reply headers and message
last if (!$response->is_success);

#print $response->content; #debug output the full transcript
    my $doodle = $response->content;
    $doodle =~ s/.*\"transcript\"://g;
$doodle =~ s/}\],.*//g;
$doodle =~ s/^{\"result\":\[\]}/{\"result\":/g;
$doodle =~ s/\R//g;
$doodle =~ s/\*/_/g;
    print $doodle;


}
      sub encode_base64url{
         my($data) = @_;
         return 0 unless $data;
         $data = encode_base64($data);
         $data =~ s/\+/-/g;
         $data =~ s/\//_/g;
         $data =~ s/\=//g;
         $data =~ s/\n//g;
         return($data);
      }
exit;
person Kruft Industries    schedule 22.09.2016
comment
Я столкнулся с той же проблемой. Должны ли мы удалить + , / , = и \n, чтобы Google мог их расшифровать? Я удалил его, но я все еще не могу расшифровать. - person hamdanjz4; 30.04.2017
comment
Да, это то, что делает материал $doodle. Если у вас все еще есть проблемы, это может быть что-то еще. - person Kruft Industries; 10.05.2017