修剪音频文件(.wav,.MP3) [英] Trim an Audio File(.wav,.mp3)

查看:170
本文介绍了修剪音频文件(.wav,.MP3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行有关微调使用用户的音频文件的软件指定例如标记,如果播放音频文件1分钟,用户需要从20秒修剪该文件到40秒的并保存它有一个新的文件。代码示例将不胜感激。

I am implementing a software related to trimming a audio file using users specified markers for example if a audio file plays for 1 minute and user wants to trim that file from 20 second to 40 second and save it has a new file. Code samples will be appreciated.

先谢谢了。

推荐答案

感谢所有为你的答复,但我得到了来自马克·希思n音讯解决方案。这里的样本
希望它可以帮助:)

Thanks all for your reply but i got the solution from Mark Heath NAudio. Here's the sample Hope it helps :)

public static class WavFileUtils
{
    public static void TrimWavFile(string inPath, string outPath, TimeSpan cutFromStart, TimeSpan cutFromEnd)
    {
        using (WaveFileReader reader = new WaveFileReader(inPath))
        {
            using (WaveFileWriter writer = new WaveFileWriter(outPath, reader.WaveFormat))
            {
                int bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000;

                int startPos = (int)cutFromStart.TotalMilliseconds * bytesPerMillisecond;
                startPos = startPos - startPos % reader.WaveFormat.BlockAlign;

                int endBytes = (int)cutFromEnd.TotalMilliseconds * bytesPerMillisecond;
                endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;
                int endPos = (int)reader.Length - endBytes; 

                TrimWavFile(reader, writer, startPos, endPos);
            }
        }
    }

    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.WriteData(buffer, 0, bytesRead);
                }
            }
        }
    }
}

这篇关于修剪音频文件(.wav,.MP3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆