1

json 文字列を Google Speech API に投稿するときに、FLAC オーディオ ファイルを base64 にエンコードするのに問題があります。Google からの返信にいくつかの \n があることに気付きました。base64 がうまく機能していないのか、それとも、そのような文字列を作成して Google を満足させるのに十分な json-y にする方法を完全に理解していないのかもしれません。私は utf-8 エンコーディングに傾倒していますが、送信される情報をさらに処理しようとした最後の試みでは、perl からの他のエラー メッセージが山ほど残っていました。どんなポインタでも大いに役立ちます!(ポインタが自分でこれを行うことをあきらめて、グーグルにサポート料金を支払うことであっても)

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 
# <zaf.000@gmail.com>
#
#
# 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;
4

1 に答える 1

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 
# <zaf.000@gmail.com>
#
#
# 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;
于 2016-09-22T23:10:52.790 に答える