網站首頁 編程語言 正文
前言
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; } } }
效果圖
原文鏈接:https://www.cnblogs.com/qiantao/archive/2021/11/29/15618359.html
相關推薦
- 2022-07-08 PyHacker編寫指南引用Nmap模塊實現端口掃描器_python
- 2023-03-26 spring?boot整合redis中間件與熱部署實現代碼_Redis
- 2022-04-07 Swift實現簡單計算器項目_Swift
- 2022-10-17 使用docker部署django的詳細步驟_docker
- 2022-10-18 使用shell腳本快速登錄容器的實現步驟_linux shell
- 2022-05-20 MybatisCodeHelpPro生成持久層代碼
- 2022-07-23 C#文件路徑Path類介紹_C#教程
- 2023-02-06 Golang泛型實現類型轉換的方法實例_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支