/// <summary> /// 截取音频后,对每个小音频获语音转文字,一个音频对应一段文字 /// </summary> /// <param name="startMilliSecond"></param> /// <param name="endMilliSecond"></param> /// <param name="reader"></param> /// <param name="sound_path"></param> /// <param name="_name"></param> /// <param name="language"></param> /// <param name="splitTime"></param> private void GetBaiduSpeech(TimeSpan startMilliSecond, TimeSpan endMilliSecond, string sound_path, int i, string language, double splitTime) { string newFile = ""; string _newFile = ""; string pcm_newFile = ""; bool need_delete = true; //将文件保存到新的文件夹(sound_path是原音频路径,newFolder是新的小音频路径,使用完成后将上传到服务器成功的音频删除) string newFolder = System.AppDomain.CurrentDomain.BaseDirectory + "NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/"; newFolder = newFolder.Replace("/", "\\"); AudioFileReader reader = new AudioFileReader(sound_path); AudioFileReader _reader = null;//超过60s音频截取小音频 try { #region 为截取音频做准备 //开始时间往前取startMilliSecond一半的偏移,结束时间往后取间隔时间的一半的偏移 if (i == 0) { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(startMilliSecond.TotalMilliseconds / 2); } else { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(splitTime / 2); } if (endMilliSecond < reader.TotalTime)//最后一次不用取偏移 { endMilliSecond = endMilliSecond + TimeSpan.FromMilliseconds(splitTime / 2); } TimeSpan span = endMilliSecond - startMilliSecond; if (span.TotalSeconds > 60)//超过60s,只取50秒 { span = TimeSpan.FromSeconds(50); } var trimed = reader.Skip(startMilliSecond).Take(endMilliSecond - startMilliSecond); #endregion string fileName = Path.GetFileNameWithoutExtension(sound_path) + "_" + i + Path.GetExtension(sound_path);//重命名文件 //重新存储到一个新的文件目录 if (!System.IO.Directory.Exists(newFolder)) { System.IO.Directory.CreateDirectory(newFolder); } //拼接后的文件路径 newFile = newFolder + fileName; //截取小音频文件 WaveFileWriter.CreateWaveFile16(newFile, trimed); //上传到文件服务器 string server_path = UploadFile.PostFile(newFile); if (Util.isNotNull(server_path)) { //上传成功 voiceFiles.Add(server_path); } else { need_delete = false; //上传失败,在服务器上的路径 voiceFiles.Add(Util.getServerPath() + "/NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/" + fileName); } //大于60s的需要再处理 if (span == TimeSpan.FromSeconds(50))//音频大于60s,只截取50s { //小音频 _reader = new AudioFileReader(sound_path); var _trimed = _reader.Skip(startMilliSecond).Take(span); //保存新的音频文件 string _fileName = "_" + Path.GetFileNameWithoutExtension(sound_path) + "_" + i + ".pcm";//重命名文件 _newFile = newFolder + _fileName; WaveFileWriter.CreateWaveFile16(_newFile, _trimed); //将音频转换为文字 //baidu_text.Add(BaiduSpeech.BaiduTranslateToText(_newFile, language, _trimed.WaveFormat.SampleRate.ToString())); baidu_text.Add(Rays.Utility.BadiAI.BaiduAI.BaiduTranslateToText(_newFile, language, trimed.WaveFormat.SampleRate.ToString())); } else { //将wav转换成pcm可以提高识别速度 pcm_newFile = NAudioHelper.GetPcmPath(newFile); //将音频转换为文字 //baidu_text.Add(BaiduSpeech.BaiduTranslateToText(pcm_newFile, language, trimed.WaveFormat.SampleRate.ToString())); baidu_text.Add(Rays.Utility.BadiAI.BaiduAI.BaiduTranslateToText(pcm_newFile, language, trimed.WaveFormat.SampleRate.ToString())); } } catch (Exception ex) { LogHelper.Error("GetBaiduSpeech:" + ex.Message); } finally { reader.Close(); reader.Dispose(); if (_reader != null) { _reader.Close(); _reader.Dispose(); } //删除文件 //if (File.Exists(_newFile)) //{ // File.Delete(_newFile); //} //if (need_delete) //{ // if (File.Exists(newFile)) // { // File.Delete(newFile); // } //} ////删除目录 //if (Directory.Exists(newFolder) && Directory.GetFiles(newFolder).Length == 0) //{ // Directory.Delete(newFolder); //} //if (File.Exists(pcm_newFile)) //{ // File.Delete(pcm_newFile); // string pcm_folder = Path.GetDirectoryName(pcm_newFile); // //删除目录 // if (Directory.Exists(pcm_folder) && Directory.GetFiles(pcm_folder).Length == 0) // { // Directory.Delete(pcm_folder); // } //} } }