使用Google翻译在PHP中进行文本到语音转换 [英] Text-to-speech in PHP with Google Translate

查看:185
本文介绍了使用Google翻译在PHP中进行文本到语音转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将转换为语音 ..



直到现在我试过了:

 <?php 
$ text =您好,这是Google的语音api测试;

//使用MD5散列生成的MP3文件的名称
$ file = md5($ text);

//将MP3文件以.mp3扩展名保存在此文件夹中
$ file =audio /。 $ file。。mp3;
if($ file){
echocreated;
} else {
echonot created;
}

//如果MP3文件存在,请不要创建新的请求
if(!file_exists($ file)){
$ mp3 = file_get_contents(
'http://translate.google.com/translate_tts?q='。$ text);
回声hello;
file_put_contents($ file,$ mp3);
} else {
echohii;
}
?>

在我的html文件中:

 < audio controls =controlsautoplay =autoplay> 
< source src =<?php echo $ file;?> type =audio / mp3/>
< / audio>

我在输出中创建了hello和音频播放器。但没有文件播放,也没有创建文件夹中?

解决方案


  1. 是您尝试访问的网址的问题。它被打破 !你应该先尝试一下。
    我在FF控制台上找到的新网址为:



    http://translate.google.com/translate_tts?ie=UTF -8& q = Hello& tl = en& total = 1& idx = 0& textlen = 5& prev = input $ b

    EM>你好。你会发现你必须在 textlen 中指定语言和文本的长度,尽管它对所有我尝试过的句子都有效,而不改变这个变量。


  2. 另一个问题是你必须urlencode()你的文本,否则你将会有一个带有重音和标点符号的错误。
    因此下载MP3的行变成:

    $ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ FR;
    $ mp3 = file_get_contents(
    'http://translate.google.com/translate_tts?ie=UTF-8&q='。urlencode($ text)。'& tl ='。$ lang& total = 1& idx = 0& textlen = 5& prev = input');


所以完整的代码如下所示:

 <?php 

$ text =Bonjour,comment allez vous?;
//是法语是一种美丽的语言。
$ lang =fr;

//使用MD5散列生成的MP3文件名
//添加了一些东西以防止错误,如果你想用两种不同的语言写出同样的句子
$ file = md5($ lang。? .urlencode($文本));

//将MP3文件保存到扩展名为.mp3的文件夹中
$ file =audio /。 $文件。 .MP3\" ;


//检查文件夹是否存在,如果没有创建,则验证CHMOD
if(!is_dir(audio /))
mkdir(audio / );
else
if(substr(sprintf('%o',fileperms('audio /')),-4)!=0777)
chmod(audio /,0777 );


//如果MP3文件存在,不要创建新的请求
if(!file_exists($ file))
{
//下载内容
$ mp3 = file_get_contents(
'http://translate.google.com/translate_tts?ie=UTF-8&q='。urlencode($ text)'& tl ='。$ lang '&安培;总= 1&安培; IDX = 0&安培;在textlen = 5&安培;一个先前=输入');
file_put_contents($ file,$ mp3);
}

?>


I am trying to convert words to speech ..

Untill now I have tried this:

<?php
 $text = "Hello this is a test for voice api of google";

// Name of the MP3 file generated using the MD5 hash
   $file  = md5($text);

// Save the MP3 file in this folder with the .mp3 extension 
   $file = "audio/" . $file .".mp3";
   if($file) {
     echo "created";
   } else {
     echo "not created";
   }

// If the MP3 file exists, do not create a new request
   if (!file_exists($file)) {
     $mp3 = file_get_contents(
        'http://translate.google.com/translate_tts?q=' . $text);
     echo "hello";
     file_put_contents($file, $mp3);
   } else {
     echo "hii";
   }
?>

In my html file :

<audio controls="controls" autoplay="autoplay">
  <source src="<?php echo $file; ?>" type="audio/mp3" />
</audio>

I am getting created hello and an audio player in output. But no file is played and neither it is created in the folder?

解决方案

  1. There is a problem with the url you try to access. It is broken ! You should have tried first. The new URL, that I found on the FF console is :

    http://translate.google.com/translate_tts?ie=UTF-8&q=Hello&tl=en&total=1&idx=0&textlen=5&prev=input

    For the single word Hello. And you see that you have to specify the language, and the length of your text in textlen, even though it did work for all the sentences I tried without changing this var.

  2. Another problem is that you have to urlencode() your text, or you will have a bug with accents and punctuation. So the line to download the MP3 becomes :

    // Language of the sentence
    $lang = "fr";
    $mp3 = file_get_contents(
    'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
    

So the complete code looks like :

<?php

    $text = "Bonjour, comment allez vous ?";
    // Yes French is a beautiful language.
    $lang = "fr";

    // MP3 filename generated using MD5 hash
    // Added things to prevent bug if you want same sentence in two different languages
    $file = md5($lang."?".urlencode($text));

    // Save MP3 file in folder with .mp3 extension 
    $file = "audio/" . $file . ".mp3";


    // Check folder exists, if not create it, else verify CHMOD
    if (!is_dir("audio/"))
        mkdir("audio/");
    else
        if (substr(sprintf('%o', fileperms('audio/')), -4) != "0777")
            chmod("audio/", 0777);


    // If MP3 file exists do not create new request
    if (!file_exists($file))
    {
        // Download content
        $mp3 = file_get_contents(
        'http://translate.google.com/translate_tts?ie=UTF-8&q='. urlencode($text) .'&tl='. $lang .'&total=1&idx=0&textlen=5&prev=input');
        file_put_contents($file, $mp3);
    }

?>

这篇关于使用Google翻译在PHP中进行文本到语音转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆