在Windows和Linux上使用php在mp3中讲话(在线文本转语音) [英] eSpeak to mp3 in php on both windows and linux (online text-to-speech)

查看:62
本文介绍了在Windows和Linux上使用php在mp3中讲话(在线文本转语音)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Web应用程序中实现简单的文本转语音脚本,该脚本可以根据给定的文本动态生成mp3.

I want to implement simple text-to-speech script in my web application that would dynamically generate mp3's out of given texts.

它必须同时运行:

  • 我在Windows上的本地WAMP服务器
  • 和我的在线linux服务器

eSpeak 不提供最高质量的声音,但至少提供强大的语言支持、简单的实现并且它是免费的.因此,经过一番挖掘,我意识到没有太多将其集成到php中的示例.我得出结论,StackOverflow应该包含一个php文本语音脚本的简单实现,该脚本可以生成带有eSpeak和lame的mp3.

eSpeak doesn't offer the highest quality in sound but at least a strong support in languages, simple implementation and also it's free. So after a little digging i realized there are not much examples of integrating it into php. I concluded StackOverflow should contain a simple implementation of a php text to speech script that generates mp3 with eSpeak and lame.

推荐答案

首先,我们需要设置espeak和lame的路径.确保同时安装了两者.就我而言,它看起来像这样:

First we need to setup path to espeak and lame. Make sure you have installed both. In my case it looks like this:

我想,有人可能会觉得这很有用.我正在使用以下代码在本地Windows Wamp服务器和在线Linux服务器中生成命令:

I tought, someone might find this useful. I'm using this code to generate my command in local windows wamp server and online linux server:

// APPLICATION PATHS AND CONFIG
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    //This is a server using Windows!
    define('ESPEAK', '..\application\libraries\espeak-win\command_line\espeak');
    define('LAME', '..\application\libraries\espeak-win\command_line\lame');
} 
else {
    //This is a server not using Windows!
    define('ESPEAK', '/usr/bin/espeak');
    define('LAME', '/usr/bin/lame');
}

然后,编写您自己的要执行的命令.我使用了%s点,以后将其替换为所需的值.可以在此处找到espeak命令列表.

Then, write your own command to be executed. I used %s spots to be replaced later with desired values. List of espeak commands can be found here.

如果您不需要mp3转换并且对.wav文件感到满意,只需删除 | 之后的部分(包括此字符)并替换参数-stdout 和这两个参数 -w wanted_file_path .在这种情况下,请确保稍后正确设置%s变量.

In case you don't need mp3 conversion and you are satisfied with .wav files, just remove the part after | (including this character) and replace argument --stdout with this two args -w desired_file_path. In that case make sure to correctly set %s variables later on.

define('COMMAND', ESPEAK.' --stdout -v %s+m3 -p 60 -a 75 -s 130 "%s" | '.LAME.' --preset voice -q 9 --vbr-new - %s');

然后执行如下脚本:

$lang_voice = 'en';
$input_text = 'some input text to read';
$file_path = 'voice-cache/output.mp3'
$exe_path = sprintf(COMMAND, $lang_voice, $input_text, $file_path); // fills %s spots
exec($exe_path);

最后一步,仅输出生成的文件:

As a last step, just output generated file:

header('Content-Type: audio/mpeg');
header('Content-Length: '.filesize($file_path));
readfile($file_path);

这篇关于在Windows和Linux上使用php在mp3中讲话(在线文本转语音)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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