码迷,mamicode.com
首页 > 微信 > 详细

基于百度云实现微信服务号内文字语音互转STT TTS

时间:2018-04-23 14:22:07      阅读:1839      评论:0      收藏:0      [点我收藏+]

标签:amr   mp3   lse   action   rom   cli   效果图   send   ack   

先上效果图:

技术分享图片

使用百度语音识别和语音合成两个接口实现

1.语音识别STT

用户微信输入,调用百度api,得到识别文本。

2.语音合成TTS

用户输入问题,调用百度api,得到mps音频,使用开源转码工具ffmpeg,转成微信音频格式amr

代码

使用基于.net的NorWeChat开源微信框架

        [HttpPost]
        [ActionName("Callback")]
        public ActionResult Post(string msg_signature, string timestamp, string nonce)
        {
            try
            {
                string obj = wx.GetMsgStr(Request.InputStream, msg_signature, timestamp, nonce);
                ReceiveMsg obj = ReceiveMsg.GetMsgObj(msg.Text);
                if (obj != null)
                {
                    WeChat wx = WXClient.GetWXAPI();
                    var resStr = string.Empty;
                    switch (obj.MsgType)
                    {
                        case "text":
                            RceMsgText ttobj = (RceMsgText)obj;
                            TextToVoice.Send(ttobj);

                            break;
                        case "voice":
                            RceMsgVoice rmvobj = (RceMsgVoice)obj;
                            VoiceToText.Send(rmvobj);
                            break;
                    }
            }
            catch (Exception ex)
            {
                fileLog.Error(ex.Message);
                fileLog.Error(ex.StackTrace);
                fileLog.Error(ex.ToString());
            }          
            return Content(null);
        }

/// <summary>
/// 处理发送的消息
/// </summary>
/// <param name="obj">发送消息</param>
public static void Send(RceMsgText obj)
{
// 设置APPID/AK/SK
var APP_ID = "你的xxx";
var API_KEY = "你的xxx";
var SECRET_KEY = "你的xxx";
//1.调用百度语音合成接口,生成音频文件mp3
var client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY);
// 可选参数
var option = new Dictionary<string, object>()
{
{"spd", 5}, // 语速
{"vol", 7}, // 音量
{"per", 1} // 发音人,4:情感度丫丫童声
};


string name = DateTime.Now.ToString("yyyyMMddhhmmssttt");


var result = client.Synthesis(obj.Content, option);


if (result.ErrorCode == 0) // 或 result.Success
{
File.WriteAllBytes(@"D:\amr\ffmpeg\mp3\" + name + ".mp3", result.Data);
//mp3转amr
//ffmpeg -i 5.mp3 -ar 8000 -ab 12.2k -ac 1 6.amr
VideoConvertFlv(@"D:\amr\ffmpeg\mp3\" + name + ".mp3", @"D:\amr\ffmpeg\amr\" + name + ".amr");
//amr上传到微信临时素材
WeChat wc = new WeChat(WeChatParameter.CorpID, WeChatParameter.Secret, WeChatParameter.AgentID);
UploaMedia media = wc.mediaTemp(wc.GetAccessTokenVal(), "voice", @"D:\amr\ffmpeg\amr\" + name + ".amr");
if (media != null && media.errcode == 0)
{
//发送临时素材
string res = wc.SendVoice(wc.GetAccessTokenVal(), obj.FromUserName, "", "", obj.AgentID, media.media_id, "0");
}
}
}


public static void VideoConvertFlv(string FromName, string ExportName)
{


string ffmpeg = @"D:\amr\ffmpeg\ffmpeg.exe";
//FromName = "";
//ExportName = "";
string Command = " -i " + FromName + " -ar 8000 -ab 12.2k -ac 1 " + ExportName; //Flv格式
//string Command = " -i \"test.wmv\" -y -ab 32 -ar 22050 -b 800000 -s 320*240 \"2.flv\"";


//string Command = "E:\\FFmpeg\\ffmpeg.exe -i E:\\ClibDemo\\VideoPath\\admin\\a.wmv -y -ab 56 -ar 22050 -b 500 -r 15 -s 320*240 " ExportName;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = ffmpeg;
p.StartInfo.Arguments = Command;
p.StartInfo.WorkingDirectory = @"D:\amr\ffmpeg\";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;
//开始执行
p.Start();
p.BeginErrorReadLine();
p.WaitForExit();
p.Close();
p.Dispose();
}

 

public static void Send(RceMsgVoice obj)
{
string returnText = string.Empty;

//组装下载链接
string downloadurl = string.Format("https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", wx.GetAccessTokenVal(), obj.MediaId);

System.Net.WebClient _WebClient = new System.Net.WebClient();

_WebClient.DownloadFile(downloadurl, @"D:\amr\" + obj.MediaId + ".amr");

// 设置APPID/AK/SK
var APP_ID = "11059149";
var API_KEY = "0i6s4Z7Rj4HVwYSi324mXhN1";
var SECRET_KEY = "InmF5wDHuQl7gyLbMc7hNuyiM3rWt5Vf";

var client = new Baidu.Aip.Speech.Asr(API_KEY, SECRET_KEY);

 

var data = File.ReadAllBytes(@"D:\amr\" + obj.MediaId + ".amr");
JObject mObj = client.Recognize(data, "amr", 8000);

int id = Convert.ToInt32(mObj["err_no"]);
string err_msg = mObj["err_msg"].ToString();
string result = string.Empty;
if (mObj["result"] != null)
{
JToken arr = mObj["result"];//获取Json里的数组 {arr:[{yy:1,zz:2},{yy:3,zz:4}]}

foreach (JToken baseJ in arr)//遍历数组
{
result = baseJ.Value<string>();
}
}

if (mObj != null && id == 0)
{
returnText = result;
}
else
{
returnText = err_msg;
}


//发送消息
wx.SendText(wx.GetAccessTokenVal(), obj.FromUserName, "", "", obj.AgentID, returnText, "0");
}

 

 

基于百度云实现微信服务号内文字语音互转STT TTS

标签:amr   mp3   lse   action   rom   cli   效果图   send   ack   

原文地址:https://www.cnblogs.com/yilanyang/p/8918754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!