I\xc2\xb4m 尝试将音频文件分成一些部分。
\n\n事实是:我有一个字节数组,我想将 wav 文件分割成一些随机块(例如 3 个)。
\n\n当然,我知道我可以\xc2\xb4t做这样的事情。但有人知道如何做到这一点吗?
\n\nbyte[] result = stream.ToArray();\nbyte[] testing = new byte[44];\n\nfor (int ix = 0; ix < testing.Length; ix++)\n{\n testing[ix] = result[ix];\n}\n\nSystem.IO.File.WriteAllBytes("yourfilepath_" + System.Guid.NewGuid() + ".wav", testing);\nRun Code Online (Sandbox Code Playgroud)\n\n我想用 C# 构建这个解决方案,但我听说有一个名为 Sox 的库,我可以像这样用沉默间隙来分割:
\n\nsox in.wav out.wav silence 1 0.5 1% 1 5.0 1% : newfile : restart\nRun Code Online (Sandbox Code Playgroud)\n\n但每次运行此命令时,只生成一个文件。(音频文件持续 5 秒,每个分割文件必须有大约 1 秒的内容)。
\n\n做这个的最好方式是什么?
\n\n非常感谢!
\n编辑
与萨班斯法案:
string sox = @"C:\Program Files (x86)\sox-14-4-1\sox.exe";
string inputFile = @"D:\Brothers Vibe - Rainforest.mp3";
string outputDirectory = @"D:\splittest";
string outputPrefix = "split";
int[] segments = { 10, 15, 30 };
IEnumerable<string> enumerable = segments.Select(s => "trim 0 " + s.ToString(CultureInfo.InvariantCulture));
string @join = string.Join(" : newfile : ", enumerable);
string cmdline = string.Format("\"{0}\" \"{1}%1n.wav" + "\" {2}", inputFile,
Path.Combine(outputDirectory, outputPrefix), @join);
var processStartInfo = new ProcessStartInfo(sox, cmdline);
Process start = System.Diagnostics.Process.Start(processStartInfo);
Run Code Online (Sandbox Code Playgroud)
如果 SOX 抱怨 libmad(用于 MP3):复制旁边的 DLL,请参见此处
或者,您可以以相同的方式使用 FFMPEG:
ffmpeg -ss 0 -t 30 -i "Brothers Vibe - Rainforest.mp3" "Brothers Vibe - Rainforest.wav"
Run Code Online (Sandbox Code Playgroud)
(有关所有详细信息,请参阅文档)
您可以使用BASS.NET轻松做到这一点:
对于下面的代码,您传入:
该方法将检查文件是否足够长以容纳指定的段,如果是,则将文件剪切为具有相同采样率、通道、位深度的 WAV。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Un4seen.Bass;
using Un4seen.Bass.Misc;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (!Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
throw new InvalidOperationException("Couldn't initialize BASS");
string fileName = @"D:\Brothers Vibe - Rainforest.mp3";
var segments = new double[] {30, 15, 20};
string[] splitAudio = SplitAudio(fileName, segments, "output", @"D:\split");
}
private static string[] SplitAudio(string fileName, double[] segments, string prefix, string outputDirectory)
{
if (fileName == null) throw new ArgumentNullException("fileName");
if (segments == null) throw new ArgumentNullException("segments");
if (prefix == null) throw new ArgumentNullException("prefix");
if (outputDirectory == null) throw new ArgumentNullException("outputDirectory");
int i = Bass.BASS_StreamCreateFile(fileName, 0, 0,
BASSFlag.BASS_STREAM_PRESCAN | BASSFlag.BASS_STREAM_DECODE);
if (i == 0)
throw new InvalidOperationException("Couldn't create stream");
double sum = segments.Sum();
long length = Bass.BASS_ChannelGetLength(i);
double seconds = Bass.BASS_ChannelBytes2Seconds(i, length);
if (sum > seconds)
throw new ArgumentOutOfRangeException("segments", "Required segments exceed file duration");
BASS_CHANNELINFO info = Bass.BASS_ChannelGetInfo(i);
if (!Directory.Exists(outputDirectory)) Directory.CreateDirectory(outputDirectory);
int index = 0;
var list = new List<string>();
foreach (double segment in segments)
{
double d = segment;
long seconds2Bytes = Bass.BASS_ChannelSeconds2Bytes(i, d);
var buffer = new byte[seconds2Bytes];
int getData = Bass.BASS_ChannelGetData(i, buffer, buffer.Length);
string name = string.Format("{0}_{1}.wav", prefix, index);
string combine = Path.Combine(outputDirectory, name);
int bitsPerSample = info.Is8bit ? 8 : info.Is32bit ? 32 : 16;
var waveWriter = new WaveWriter(combine, info.chans, info.freq, bitsPerSample, true);
waveWriter.WriteNoConvert(buffer, buffer.Length);
waveWriter.Close();
list.Add(combine);
index++;
}
bool free = Bass.BASS_StreamFree(i);
return list.ToArray();
}
}
}
Run Code Online (Sandbox Code Playgroud)
去做
提取未优化,如果您关心内存使用情况,则应增强该功能以抓取段的一部分并将其逐步写入WaveWriter.
笔记
BASS.NET 有一个导航屏幕,但您可以在其网站上请求免费注册序列号。
注意,安装 BASS.NET 然后确保从 EXE 旁边的基础包中复制 bass.dll。此外,您几乎可以使用任何音频格式,请参阅他们的网站以获取格式插件以及如何加载它们(BASS_PluginLoad)。