d***@dufan.cool
d***@dufan.cool
  • 发布:2024-05-18 08:25
  • 更新:2024-11-15 11:22
  • 阅读:429

抖音火山引擎语音合成代码

分类:uniCloud
// 抖音火山引擎语音合成  
function makerDouyinVoice(content) {  

    const host = "https://openspeech.bytedance.com/api/v1/tts"  
    const header = {  
        "Authorization": "Bearer;Uwn******************iw0V4"  // 换成自己的 token  
    }  

    const requestBody = {  
        "app": {  
            "appid": "123456", //换成自己的 抖音火山引擎账号 id  
            "token": "Uwn******************iw0V4", // 换成自己的 token  
            "cluster": "****", //换成自己的 cluster  
        },  
        "user": {  
            "uid": content.uid  
        },  
        "audio": {  
            "voice_type": "BV027_streaming",  // 角色   
            "encoding": "mp3",  
            "compression_rate": 1,  
            "rate": 24000,  
            "speed_ratio": 0.8,  // 语速  
            "volume_ratio": 1.0,  
            "pitch_ratio": 1.0,  
            "emotion": "happy",  
            "language": "en"  // 这里切换中英文  
        },  
        "request": {  
            "reqid": content.sentenceId,  
            "text": content.text,  
            "text_type": "plain",  
            "operation": "query",  
            "silence_duration": "125",  
            "with_frontend": "1",  
            "frontend_type": "unitTson",  
            "pure_english_opt": "1"  
        }  
    }  

    return uniCloud.httpclient.request(host, {  
        method: 'POST',  
        headers: header,  
        data: requestBody,  
        contentType: 'json',  
        dataType:'text', // 注意这个参数,要以字符串形式返回  
        timeout: 60000  
    })  
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// 云对象调用
async makeVoiceByDouyin(content) {
const voiceRes = await makerDouyinVoice(content)
console.log(voiceRes)
if (voiceRes.status == 200) {

        // 火山引擎返回的数据是字符串 要转换成json对象  
        const voiceData = JSON.parse(voiceRes.data)  
        if(voiceData.code == 3000){  
            // 火山引擎返回的音频数据 base64 要转换成 Buffer  
            const voiceBuffer = Buffer.from(voiceData.data,'base64')  
            // 上传到云存储  
            const res = await uniCloud.uploadFile({  
                cloudPath: `${content.sentenceId}.mp3`,  
                fileContent: voiceBuffer  
            })  
            return res  
        }  
        return {  
            errCode:1,  
            errMsg:'语音合成失败'  
        }  

    }  
    return {  
        errCode:1,  
        errMsg: '合成失败'  
    }  

}    
0 关注 分享

要回复文章请先登录注册

愤怒的洋芋蛋蛋

愤怒的洋芋蛋蛋

没太看明白,要云储存后才能调用播放吗?
2024-11-15 11:22