是否可以使用CSCore-图书馆剪切音频文件?例如,我想在第二个20开始启动一个mp3,在第二个50中停止它,我想生成一个新的mp3文件,这样就可以了。
发布于 2014-05-11 11:36:26
有两种方法可以剪切mp3文件。
第一种方法的缺点是它比方法2更复杂,而且不能精确地切割mp3。这意味着,您仅限于mp3帧的大小。
第二种方法就是你要找的东西。但是有一个大问题:MP3编码只支持Windows 8,这意味着您不能在Windows、Vista或Windows 7中使用这种方法。
->我建议你使用任何第三方的组件,如lame,ffmpeg,.
不管怎样..。方法2的一个例子:
private static void Main(string[] args)
{
TimeSpan startTimeSpan = TimeSpan.FromSeconds(20);
TimeSpan endTimeSpan = TimeSpan.FromSeconds(50);
using (IWaveSource source = CodecFactory.Instance.GetCodec(@"C:\Temp\test.mp3"))
using (MediaFoundationEncoder mediaFoundationEncoder =
MediaFoundationEncoder.CreateWMAEncoder(source.WaveFormat, @"C:\Temp\dest0.mp3"))
{
AddTimeSpan(source, mediaFoundationEncoder, startTimeSpan, endTimeSpan);
}
}
private static void AddTimeSpan(IWaveSource source, MediaFoundationEncoder mediaFoundationEncoder, TimeSpan startTimeSpan, TimeSpan endTimeSpan)
{
source.SetPosition(startTimeSpan);
int read = 0;
long bytesToEncode = source.GetBytes(endTimeSpan - startTimeSpan);
var buffer = new byte[source.WaveFormat.BytesPerSecond];
while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
int bytesToWrite = (int)Math.Min(read, bytesToEncode);
mediaFoundationEncoder.Write(buffer, 0, bytesToWrite);
bytesToEncode -= bytesToWrite;
}
}https://stackoverflow.com/questions/23591715
复制相似问题