2016-09-21 53 views
1

我有一個FLAC音頻文件編碼到base64時,發佈一個json字符串到谷歌語音api有困難。我在Google的回覆中注意到了一些\ n的問題,不確定是否base64不夠好,或者我沒有完全掌握如何構建這樣的字符串,並使它們足夠滿足谷歌。我傾向於使用utf-8編碼,但是我最後一次嘗試在進一步處理要發送的信息時給我留下了來自perl的其他錯誤消息。任何指針都將非常有幫助! (即使指針放棄對我自己這樣做並支付支持費用,以谷歌)POST base64編碼的文件到谷歌語音api使用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 
# <[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 ([email protected] || $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; 
+0

Google是否要求URL版本的Base64?您的'encode_base64url'版本與'MIME :: Base64 :: encode_base64url'的工作方式不同,它在Google的回顯代碼中進行了驗證,該代碼中包含'/'','+''和換行符。實際上,你的版本只產生與'encode_base64'相同的輸出。 URL版本也應該刪除''=''。 –

回答

1

這裏是修正後的腳本。這應該會有所幫助,我可能會更新標題和說明以防止重複的問題!謝謝你指出我正確的方向,克里斯托弗Oicles!

#!/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 ([email protected] || $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; 
+0

我遇到了同樣的問題。我們是否應該刪除'+','/','='和'\ n'以便Google解碼?我已將其刪除,但仍無法解碼。 – hamdanjz4

+0

是的,這就是$塗鴉的東西在做什麼。如果你仍然有問題,它可能是別的東西。 –