C#?基于NAudio實(shí)現(xiàn)對(duì)Wav音頻文件剪切(限PCM格式)
前言
C#基于NAudio工具對(duì)Wav音頻文件進(jìn)行剪切,將一個(gè)音頻文件剪切成多個(gè)音頻文件
注:調(diào)用方法前需要導(dǎo)入NAudio.dll或者在NuGet程序管理器搜索NAudio并安裝
本文是按時(shí)間剪切
實(shí)現(xiàn)代碼
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工具對(duì)Wav音頻文件剪切(限PCM格式) /// </summary> /// <param name="inPath">目標(biāo)文件</param> /// <param name="outPath">輸出文件</param> /// <param name="cutFromStart">開(kāi)始時(shí)間</param> /// <param name="cutFromEnd">結(jié)束時(shí)間</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; //判斷結(jié)束位置是否越界 endPos = endPos > fileLength ? fileLength : endPos; TrimWavFile(reader, writer, startPos, endPos); } } } /// <summary> /// 重新合并wav文件 /// </summary> /// <param name="reader">讀取流</param> /// <param name="writer">寫(xiě)入流</param> /// <param name="startPos">開(kāi)始流</param> /// <param name="endPos">結(jié)束流</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); } } } } } }
調(diào)用:
string filePath = "D:\\wav\\test.wav";//需要切割的文件路徑 int cutTimeSpan = 20;//切割的時(shí)間片段時(shí)間(秒) FileInfo fi = new FileInfo(filePath); //獲取錄音文件時(shí)長(zhǎng)(秒) int fileTime = (int)Util.Cover(Util.GetVoiceTime(filePath)) / 1000; //計(jì)算文件需要切割多少等份 decimal fileNum = Math.Ceiling((decimal)fileTime / cutTimeSpan); int i = 0; while (i < fileNum) { string nowTime = Util.GetTimeStamp();//當(dāng)前時(shí)間戳 //切割后保存的文件絕對(duì)地址 var outputPath = System.IO.Path.Combine(fi.Directory.FullName, string.Format("{0}_{1}{2}", fi.Name.Replace(fi.Extension, ""), nowTime, fi.Extension)); //切割的開(kāi)始時(shí)間 TimeSpan cutFromStart = TimeSpan.FromSeconds(i * cutTimeSpan); //切割的結(jié)束時(shí)間 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> /// 獲取時(shí)間戳 /// </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> /// 返回音頻時(shí)長(zhǎng) /// </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;//返回音頻時(shí)長(zhǎng) return SongTime; } /// <summary> /// 時(shí)間格式轉(zhuǎn)毫秒值 /// </summary> /// <param name="time">時(shí)間字符串</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; } } }
效果圖
到此這篇關(guān)于C# 基于NAudio實(shí)現(xiàn)對(duì)Wav音頻文件剪切(限PCM格式)的文章就介紹到這了,更多相關(guān)C# NAudio Wav音頻文件剪切內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Winform開(kāi)發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式
這篇文章介紹了Winform開(kāi)發(fā)中使用下拉列表展示字典數(shù)據(jù)的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09c#中directory 和directoryinfo的使用小結(jié)
當(dāng)使用C#處理目錄時(shí),可以使用?System.IO?命名空間中的?Directory?和?DirectoryInfo?類來(lái)執(zhí)行各種目錄操作,本文主要介紹了c#中directory 和directoryinfo的使用小結(jié),感興趣的可以了解一下2024-02-02unity shader實(shí)現(xiàn)玻璃折射效果
這篇文章主要為大家詳細(xì)介紹了unity shader實(shí)現(xiàn)玻璃折射效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#使用StreamReader和StreamWriter類讀寫(xiě)操作文件
這篇文章介紹了C#使用StreamReader和StreamWriter類讀寫(xiě)操作文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C#實(shí)現(xiàn)帶消息數(shù)的App圖標(biāo)
這篇文章主要介紹了如何使用C#實(shí)現(xiàn)帶消息數(shù)的App圖標(biāo)的方法,并附上全部源碼,分享給大家,有需要的小伙伴可以參考下。2015-12-12