/// <summary> /// 解析音频文件,获取时间戳,截取音频,语音转文字 /// </summary> /// <param name="sound_path"></param> /// <param name="word_path"></param> /// <param name="language"></param> /// <param name="splitTime"></param> public void GetTimeSpan(string sound_path, string word_path, string language, double splitTime = 1.5) { //通过NAudio读取音频文件流 AudioFileReader inputStream = null; string sound_path_mp3 = ""; try { if (sound_path.ToLower().EndsWith(".mp3")) { sound_path_mp3 = sound_path; //通过NAudio将文件转换为WAV格式,返回新的文件路径 sound_path = NAudioHelper.GetWavPath(sound_path); } inputStream = new AudioFileReader(sound_path); //WAV文件读取 WAVReader reader = new WAVReader(); //reader.GetTimeSpan(sound_path, out voicePoint, out voiceMilliSecond, splitTime); reader.GetTimeSpan(sound_path, out voicePoint, out musicContents, splitTime); //voicePoint是为了截取小音频 //voicePoint是双数的原因是有开始时间就有一个结束时间 //最后一次的时间要加上(结束时,没有停顿时间) if (voicePoint.Count % 2 != 0) { voicePoint.Add(inputStream.TotalTime); } LogHelper.Info("时间戳获取:成功"); //string totalTime=FfmpegHelper.getMediaDuration(sound_path); int _name = 1;//命名 //一个时间点对应一段音频对应一段文字 LogHelper.Info("语音转文字:开始"); for (int i = 0; i < voicePoint.Count; i += 2) { var para = new BaiduRead() { voice_time = musicContents.Select(o => o.voiceMilliSecond).ToList()[i / 2], start = voicePoint[i], end = voicePoint[i + 1], sound_path = sound_path, file_name = _name, language = language, splitTime = splitTime }; Task task_max = Task.Factory.StartNew(() => { GetBaiduSpeech(para); }); Task.WaitAll(task_max); //GetBaiduSpeech(para); ++_name; } //word原文 originalText = NPOIHelper.ExcuteWordText(word_path); //word原文的集合 results = GetTextContrast(musicContents, originalText, language); } catch (Exception ex) { LogHelper.Error(ex.Message); } #region 除文件 finally { inputStream.Close(); inputStream.Dispose(); isFinish = true; //删除MP3文件 if (Util.isNotNull(sound_path_mp3) && File.Exists(sound_path_mp3)) { File.Delete(sound_path_mp3); string mp3_folder = Path.GetDirectoryName(sound_path_mp3); if (Directory.Exists(mp3_folder) && Directory.GetFiles(mp3_folder).Length == 0) { Directory.Delete(mp3_folder); } } //删除word文件 if (File.Exists(word_path)) { File.Delete(word_path); //删除word文件目录 string word_folder = Path.GetDirectoryName(word_path); if (Directory.Exists(word_folder) && Directory.GetFiles(word_folder).Length == 0) { Directory.Delete(word_folder); } } //删除文件夹,删除原音频文件 if (File.Exists(sound_path)) { File.Delete(sound_path); //删除原音频目录 string sound_folder = Path.GetDirectoryName(sound_path); if (Directory.Exists(sound_folder) && Directory.GetFiles(sound_folder).Length == 0) { Directory.Delete(sound_folder); } } } #endregion }
/// <summary> /// 解析音频文件,获取时间戳,截取音频,语音转文字 /// </summary> /// <param name="sound_path"></param> /// <param name="word_path"></param> /// <param name="language"></param> /// <param name="splitTime"></param> public void GetTimeSpan(string sound_path, string word_path, string language, double splitTime = 1.5) { //通过NAudio读取音频文件流 AudioFileReader inputStream = null; string sound_path_mp3 = ""; try { if (sound_path.ToLower().EndsWith(".mp3")) { sound_path_mp3 = sound_path; //通过NAudio将文件转换为WAV格式,返回新的文件路径 sound_path = NAudioHelper.GetWavPath(sound_path); } inputStream = new AudioFileReader(sound_path); //WAV文件读取 WAVReader reader = new WAVReader(); reader.GetTimeSpan(sound_path, out voicePoint, out voiceMilliSecond, splitTime); //voicePoint是为了截取小音频 //voicePoint是双数的原因是有开始时间就有一个结束时间 //最后一次的时间要加上(结束时,没有停顿时间) if (voicePoint.Count % 2 != 0) { voicePoint.Add(inputStream.TotalTime); } LogHelper.Info("时间戳获取:成功"); //string totalTime=FfmpegHelper.getMediaDuration(sound_path); int _name = 1;//命名 //一个时间点对应一段音频对应一段文字 LogHelper.Info("语音转文字:开始"); for (int i = 0; i < voicePoint.Count; i += 2) { GetBaiduSpeech(voicePoint[i], voicePoint[i + 1], sound_path, _name, language, splitTime); ++_name; } //word原文 if (Path.GetExtension(word_path).Contains("doc") || Path.GetExtension(word_path).Contains("docx")) { originalText = NPOIHelper.ExcuteWordText(word_path); //word原文的集合 results = GetTextContrast(voiceFiles.ToArray(), voiceMilliSecond.ToArray(), baidu_text.ToArray(), originalText, language); } else if (Path.GetExtension(word_path).Contains("txt") || Path.GetExtension(word_path).Contains("lrc")) { string _originalText = Util.ReadTxt(word_path); originalText = string.Join("", _originalText.Split('|')); //word原文的集合 results = GetTextContrast(voiceFiles.ToArray(), voiceMilliSecond.ToArray(), baidu_text.ToArray(), _originalText, language); } } catch (Exception ex) { LogHelper.Error(ex.Message); } finally { inputStream.Close(); inputStream.Dispose(); isFinish = true; ////删除MP3文件 //if (Util.isNotNull(sound_path_mp3) && File.Exists(sound_path_mp3)) //{ // File.Delete(sound_path_mp3); // string mp3_folder = Path.GetDirectoryName(sound_path_mp3); // if (Directory.Exists(mp3_folder) && Directory.GetFiles(mp3_folder).Length == 0) // { // Directory.Delete(mp3_folder); // } //} ////删除word文件 //if (File.Exists(word_path)) //{ // File.Delete(word_path); // //删除word文件目录 // string word_folder = Path.GetDirectoryName(word_path); // if (Directory.Exists(word_folder) && Directory.GetFiles(word_folder).Length == 0) // { // Directory.Delete(word_folder); // } //} ////删除文件夹,删除原音频文件 //if (File.Exists(sound_path)) //{ // File.Delete(sound_path); // //删除原音频目录 // string sound_folder = Path.GetDirectoryName(sound_path); // if (Directory.Exists(sound_folder) && Directory.GetFiles(sound_folder).Length == 0) // { // Directory.Delete(sound_folder); // } //} } }
/// <summary> /// 解析音频文件,获取时间戳,截取音频,语音转文字 /// </summary> /// <param name="sound_path"></param> /// <param name="word_path"></param> /// <param name="language"></param> /// <param name="splitTime"></param> public void GetTimeSpan(string sound_path, string word_path, string language, double splitTime = 1.5) { //通过NAudio读取音频文件流 string sound_path_mp3 = ""; try { if (sound_path.ToLower().EndsWith(".mp3") || sound_path.ToLower().EndsWith(".wav")) { sound_path_mp3 = sound_path; //使用ffmpeg将MP3转换为 单声道 16000Hz 16bit的wav string folder = Path.GetDirectoryName(sound_path); string wav_sound_path = folder + "/" + "_" + Path.GetFileNameWithoutExtension(sound_path) + ".wav"; if (FfmpegHelper.ChangeFileType(sound_path, wav_sound_path)) { sound_path = wav_sound_path; } else { LogHelper.Error("使用ffmpeg,MP3转wav文件失败!"); return; } } string totalTimeStr = FfmpegHelper.getMediaDuration(sound_path); TimeSpan totalTime = TimeSpan.Parse(totalTimeStr); //WAV文件读取 WAVReader reader = new WAVReader(); reader.GetTimeSpan(sound_path, out voicePoint, out voiceMilliSecond, splitTime); int channel = reader.channel; int rate = reader.rate; int bit = reader.bit; //voicePoint是为了截取小音频 //voicePoint是双数的原因是有开始时间就有一个结束时间 //最后一次的时间要加上(结束时,没有停顿时间) if (voicePoint.Count % 2 != 0) { voicePoint.Add(totalTime); } int _name = 1;//命名 //一个时间点对应一段音频对应一段文字 for (int i = 0; i < voicePoint.Count; i += 2) { GetBaiduSpeech(voicePoint[i], voicePoint[i + 1], sound_path, _name, language, splitTime, totalTime, channel, rate, bit); ++_name; } //word原文 originalText = NPOIHelper.ExcuteWordText(word_path); //word原文的集合 results = GetTextContrast(voiceFiles.ToArray(), voiceMilliSecond.ToArray(), baidu_text.ToArray(), originalText, language); } catch (Exception ex) { LogHelper.Error(ex.Message); } finally { isFinish = true; //删除MP3文件 if (Util.isNotNull(sound_path_mp3) && File.Exists(sound_path_mp3)) { File.Delete(sound_path_mp3); string mp3_folder = Path.GetDirectoryName(sound_path_mp3); if (Directory.Exists(mp3_folder) && Directory.GetFiles(mp3_folder).Length == 0) { Directory.Delete(mp3_folder); } } //删除word文件 if (File.Exists(word_path)) { File.Delete(word_path); //删除word文件目录 string word_folder = Path.GetDirectoryName(word_path); if (Directory.Exists(word_folder) && Directory.GetFiles(word_folder).Length == 0) { Directory.Delete(word_folder); } } //删除文件夹,删除原音频文件 if (File.Exists(sound_path)) { File.Delete(sound_path); //删除原音频目录 string sound_folder = Path.GetDirectoryName(sound_path); if (Directory.Exists(sound_folder) && Directory.GetFiles(sound_folder).Length == 0) { Directory.Delete(sound_folder); } } } }