利用Cloudflare Workers实现免费搭建微软文字转语音服务

2023-09-01T16:42:00

说明:目前网络上,文字转语音的软件众多,有免费的,也有收费的,但这些都依赖于他们的服务器需要维护,今天分享的教程,无需服务器,只需要将代码上传到Cloudflare Workers中,即可实现文字转语音服务。

项目介绍
此项目是基于微软azure的文本转语音服务,通过调用微软azureapi接口,将文本转换为语音,然后下载到本地。

教程开始

1. 访问 [button color="warning" icon="glyphicon glyphicon-cloud" url="https://www.cloudflare.com"]Cloudflare[/button]

2. 点击Workers 和 Pages,选择 创建应用程序 如下图

3. 点击创建Workers

4. 设置项目名称 注:名称会当作域名前缀

5. 复制代码 粘贴到如图所示位置 访问生成的域名即可

[scode type="yellow"]由于众所周知的原因,自动分配的域名基本无法访问,请继续往下查看教程![/scode]

[hide]

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request))
  })
  
  
  function generateUUID() {
    let uuid = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
        let r = Math.random() * 16 | 0,
            v = c === 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
    return uuid;
  }
  
  const API_URL = "https://southeastasia.api.speech.microsoft.com/accfreetrial/texttospeech/acc/v3.0-beta1/vcg/speak";
  const DEFAULT_HEADERS = {
    authority: "southeastasia.api.speech.microsoft.com",
    accept: "*/*",
    "accept-language": "zh-CN,zh;q=0.9",
    customvoiceconnectionid: generateUUID(),
    origin: "https://speech.microsoft.com",
    "sec-ch-ua":
        '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36",
    "content-type": "application/json",
  };
  
  const speechApi = async (ssml) => {
    const data = JSON.stringify({
        ssml,
        ttsAudioFormat: "audio-24khz-160kbitrate-mono-mp3",
        offsetInPlainText: 0,
        properties: {
            SpeakTriggerSource: "AccTuningPagePlayButton",
        },
    });
  
    try {
        const response = await fetch(API_URL, {
            method: "POST",
            responseType: "arraybuffer",
            headers: DEFAULT_HEADERS,
            body: data
        });
  
        if (!response.ok) {
            throw new Error(`Request failed with status ${response.status}`);
        }
  
        return response.arrayBuffer();
    } catch (error) {
        console.error("Error during API request:", error);
        throw error;
    }
  };
  
  const handleRequest = async (request) => {
    // 解析请求 URL
    const url = new URL(request.url);
  
    const clientIP = request.headers.get("CF-Connecting-IP")
  
    if (url.pathname == "/") {
      const html = await fetch("https://raw.githubusercontent.com/duansix/cftts_temple/main/index.html")
  
      const page =await html.text()  
        return new Response(page, {
            headers: {
                "content-type": "text/html;charset=UTF-8",
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Credentials": "true",
                "Access-Control-Allow-Headers": "*",
                "Access-Control-Allow-Methods": "*",
                "ip": `Access cloudflare's ip:${clientIP}`
            },
        })
    } else if (url.pathname == "/audio") {
        // 解析查询参数
        const params = new URLSearchParams(url.search);
        // 获取查询参数中的文本
        const text = params.get("text");
        // 获取查询参数中的语速
        const rate = params.get("rate");
        // 获取查询参数中的音高
        const pitch = params.get("pitch");
        // 获取查询参数中的音色
        const voice = params.get("voice");
        // 获取查询参数中的音色风格
        const voiceStyle = params.get("voiceStyle");
        const ssml = `<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="en-US">
    <voice name="${voice}">
    <mstts:express-as style="${voiceStyle}">
        <prosody rate="${rate}%" pitch="${pitch}%">
        ${text}
       </prosody>
        </mstts:express-as>
    </voice>
    </speak>`;
  
        const audio = await speechApi(ssml);
  
        return new Response(audio, {
            headers: {
                "Content-Type": "audio/mpeg",
                "Content-Disposition": `attachment; filename=audio.mp3`,
            },
        });
    }else{
      return new Response("page", {
        headers: {
            "content-type": "text/html;charset=UTF-8",
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Credentials": "true",
            "Access-Control-Allow-Headers": "*",
            "Access-Control-Allow-Methods": "*",
            "ip": `Access cloudflare's ip:${clientIP}`
        },
    })
    }
  
  }

[/hide]

反代域名 实现访问

1.点击左上角项目名称 返回 项目首页

2.点击 触发器 添加自定义域(添加的域名需要是你已经解析到Cloudflare才可以)

3.现在就可以实现访问自己的域名 使用文字转语音服务了

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »