使用Google Translate在PHP中进行文本转语音

时间:2013-02-06 07:46:10

标签: php html text-to-speech speech google-text-to-speech

我正在尝试将字词转换为语音 ..

直到现在我已经尝试过这个:

<?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";
   }
?>

在我的html文件中:

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

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

6 个答案:

答案 0 :(得分:11)

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

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

    单词 Hello 。而且你看到你必须在 textlen 中指定语言和文本的长度,即使它确实适用于我尝试的所有句子而不更改此变量。

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

    // 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');
    
  3. 所以完整的代码如下:

    <?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);
        }
    
    ?>
    

答案 1 :(得分:3)

改进版本:

// ~~~ Credits to kube ~~~

    $text = "Hello this is a test for voice api of google";
    $text = urlencode($text);
    $lang = urldecode("en");
    $file  = "audio/" . md5($text) .".mp3";
       if (!file_exists($file) || filesize($file) == 0) {
         $mp3 = file_get_contents('http://translate.google.com/translate_tts?ie=UTF-8&q='.$text.'&tl='.$lang.'&total=2&idx=0&textlen='.strlen($text).'&prev=input');
         if(file_put_contents($file, $mp3)){
            echo "Saved<br>";
         }else{
            echo "Wasn't able to save it !<br>";
         }
       } else {
         echo "Already exist<br>";
       }

答案 2 :(得分:3)

我找到了它:

https://translate.google.com.vn/translate_tts?ie=UTF-8&client=tw-ob&q=ANYTHING_TEXT&tl=YOUR_LANGUAGE_CODE

重要提示:client=tw-ob

YOUR_LANGUAGE_CODE可以是en,us,uk,vi等。

答案 3 :(得分:2)

您无法免费使用此服务。

  

是否有免费配额?   不,Google Translate API仅作为付费服务提供。有关更多详细信息,请参阅定价和支持。但是,我们会提供Google网站翻译小工具,该小工具将免费翻译您的网站。

检查translate API FAQ

有关您在Techcrunch

上可以找到的非正式使用方式的更多信息

答案 4 :(得分:1)

您也可以使用下面的简单代码。只需回显代码即可获得结果。在此代码中,无需保存文件或获取权限问题。

java.lang.NullPointerException
org.codehaus.jackson.xc.JaxbAnnotationIntrospector.findAnnotation(JaxbAnnotationIntrospector.java:860)
org.codehaus.jackson.xc.JaxbAnnotationIntrospector._typeResolverFromXmlElements(JaxbAnnotationIntrospector.java:318)
org.codehaus.jackson.xc.JaxbAnnotationIntrospector.findPropertyTypeResolver(JaxbAnnotationIntrospector.java:296)
org.codehaus.jackson.map.AnnotationIntrospector$Pair.findPropertyTypeResolver(AnnotationIntrospector.java:928)
org.codehaus.jackson.map.deser.BasicDeserializerFactory.findPropertyTypeDeserializer(BasicDeserializerFactory.java:502)
org.codehaus.jackson.map.deser.BasicDeserializerFactory.resolveType(BasicDeserializerFactory.java:692)
org.codehaus.jackson.map.deser.BasicDeserializerFactory.constructCreatorProperty(BasicDeserializerFactory.java:787)
org.codehaus.jackson.map.deser.BeanDeserializerFactory._addDeserializerConstructors(BeanDeserializerFactory.java:590)
org.codehaus.jackson.map.deser.BeanDeserializerFactory.findDeserializerCreators(BeanDeserializerFactory.java:524)
org.codehaus.jackson.map.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:393)
org.codehaus.jackson.map.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:370)
org.codehaus.jackson.map.deser.StdDeserializerProvider._createDeserializer(StdDeserializerProvider.java:359)
org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:281)
org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:261)
org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:119)
org.codehaus.jackson.map.deser.StdDeserializerProvider.findTypedValueDeserializer(StdDeserializerProvider.java:140)
org.codehaus.jackson.map.ObjectMapper._findRootDeserializer(ObjectMapper.java:2197)
org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2112)
org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1004)
org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:410)
com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:474)
com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123)
com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:203)
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

答案 5 :(得分:0)

您的文件未创建,因为您忘记创建它,使用下面的代码创建文件。

$file = "audio/".$file.".mp3";
$ourFileHandle = fopen($file, 'w') or die("can't open file");