您好,登錄后才能下訂單哦!
這篇文章主要講解了“C#基于NAudio怎么實現對Wav音頻文件剪切”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“C#基于NAudio怎么實現對Wav音頻文件剪切”吧!
C#基于NAudio工具對Wav音頻文件進行剪切,將一個音頻文件剪切成多個音頻文件
注:調用方法前需要導入NAudio.dll或者在NuGet程序管理器搜索NAudio并安裝
本文是按時間剪切
using NAudio.Wave; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace XXX.util { public static class WavFileUtils { /// <summary> /// 基于NAudio工具對Wav音頻文件剪切(限PCM格式) /// </summary> /// <param name="inPath">目標文件</param> /// <param name="outPath">輸出文件</param> /// <param name="cutFromStart">開始時間</param> /// <param name="cutFromEnd">結束時間</param> public static void TrimWavFile(string inPath, string outPath, TimeSpan cutFromStart, TimeSpan cutFromEnd) { using (WaveFileReader reader = new WaveFileReader(inPath)) { int fileLength = (int)reader.Length;using (WaveFileWriter writer = new WaveFileWriter(outPath, reader.WaveFormat)) { float bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000f; int startPos = (int)Math.Round(cutFromStart.TotalMilliseconds * bytesPerMillisecond); startPos = startPos - startPos % reader.WaveFormat.BlockAlign; int endPos = (int)Math.Round(cutFromEnd.TotalMilliseconds * bytesPerMillisecond); endPos = endPos - endPos % reader.WaveFormat.BlockAlign; //判斷結束位置是否越界 endPos = endPos > fileLength ? fileLength : endPos; TrimWavFile(reader, writer, startPos, endPos); } } } /// <summary> /// 重新合并wav文件 /// </summary> /// <param name="reader">讀取流</param> /// <param name="writer">寫入流</param> /// <param name="startPos">開始流</param> /// <param name="endPos">結束流</param> private static void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPos, int endPos) { reader.Position = startPos; byte[] buffer = new byte[1024]; while (reader.Position < endPos) { int bytesRequired = (int)(endPos - reader.Position); if (bytesRequired > 0) { int bytesToRead = Math.Min(bytesRequired, buffer.Length); int bytesRead = reader.Read(buffer, 0, bytesToRead); if (bytesRead > 0) { writer.Write(buffer, 0, bytesRead); } } } } } }
調用:
string filePath = "D:\\wav\\test.wav";//需要切割的文件路徑 int cutTimeSpan = 20;//切割的時間片段時間(秒) FileInfo fi = new FileInfo(filePath); //獲取錄音文件時長(秒) int fileTime = (int)Util.Cover(Util.GetVoiceTime(filePath)) / 1000; //計算文件需要切割多少等份 decimal fileNum = Math.Ceiling((decimal)fileTime / cutTimeSpan); int i = 0; while (i < fileNum) { string nowTime = Util.GetTimeStamp();//當前時間戳 //切割后保存的文件絕對地址 var outputPath = System.IO.Path.Combine(fi.Directory.FullName, string.Format("{0}_{1}{2}", fi.Name.Replace(fi.Extension, ""), nowTime, fi.Extension)); //切割的開始時間 TimeSpan cutFromStart = TimeSpan.FromSeconds(i * cutTimeSpan); //切割的結束時間 TimeSpan cutFromEnd = cutFromStart + TimeSpan.FromSeconds(cutTimeSpan); //音頻切割 WavFileUtils.TrimWavFile(recordFile.FilePath, outputPath, cutFromStart, cutFromEnd); i++; }
Util 類:
using Shell32; using System; using System.Diagnostics; using System.IO; using System.Net; using System.Net.Sockets; using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; namespace XXX.util { class Util { /// <summary> /// 獲取時間戳 /// </summary> /// <returns></returns> public static string GetTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalMilliseconds).ToString(); } /// <summary> /// 返回音頻時長 /// </summary> /// <param name="SongPath">音頻文件路徑</param> /// <returns></returns> public static string GetVoiceTime(string SongPath) { string dirName = Path.GetDirectoryName(SongPath); string SongName = Path.GetFileName(SongPath); ShellClass sh = new ShellClass(); Folder dir = sh.NameSpace(dirName); FolderItem item = dir.ParseName(SongName); string SongTime = Regex.Match(dir.GetDetailsOf(item, -1), "\\d:\\d{2}:\\d{2}").Value;//返回音頻時長 return SongTime; } /// <summary> /// 時間格式轉毫秒值 /// </summary> /// <param name="time">時間字符串</param> /// <returns></returns> public static long Cover(string time) { string[] a = time.Split(':'); if (long.Parse(a[0]) == 0 && long.Parse(a[1]) == 0) { return long.Parse(a[2]) * 1000; } else if (long.Parse(a[0]) == 0 && long.Parse(a[1]) != 0) { return (long.Parse(a[1]) * 60 + long.Parse(a[2])) * 1000; } else if (long.Parse(a[0]) != 0 && long.Parse(a[1]) == 0) { return ((long.Parse(a[0]) * 60 * 60) + long.Parse(a[2])) * 1000; } else if (long.Parse(a[0]) != 0 && long.Parse(a[1]) != 0) { return (((long.Parse(a[0]) * 60) + long.Parse(a[1])) * 60) * 1000; } return 0; } } }
感謝各位的閱讀,以上就是“C#基于NAudio怎么實現對Wav音頻文件剪切”的內容了,經過本文的學習后,相信大家對C#基于NAudio怎么實現對Wav音頻文件剪切這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。