开发者

Trim an Audio File(.wav,.mp3) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.

Closed 8 years ago.

Improve this question

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 samp开发者_运维技巧les will be appreciated.

Thanks in advance.


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);
                }
            }
        }
    }
}


ffmpeg -ss 00:00:30.0 -t 00:00:10.0 -i input.mp3 -acodec copy output.mp3

This can be achieved using Ffmpeg . So download ffmpeg for windows and Start FFmpeg as a Process .

PS : -ss Pun Offset -t Length ,on the Example will trim the Audio File from 30 Sec to 40 Sec (10 Seconds Length)


Using Sox to Extract Subparts of a File

The trim effect copies the portion of the input starting at start and ending at start plus length to the output. Both parameters may be specified either as numbers of samples, consisting of an integer followed by the letter s, e.g. "8700s" or a time value. Time values are of the form ((hh:)mm:)ss(.fs). A bare integer is therefore a time value in seconds.

For example, suppose that you have a recording 1 hour long and wish to cut it into two halves. The following two commands will leave the first half in Half1.wav and the second half in Half2.wav.

sox Input.wav  Half1.wav trim 0 30:00
sox Input.wav  Half2.wav trim 30:00 30:00

The original file is unaffected, so once you have confirmed that the two output files contain what they should, you may delete the original if you wish to.

Sox is extensible, and has (installable) support for many formats including wav,flac,mp3 etc


Try to change your code:

byte[] buffer = new byte[1024];

To

byte[] buffer = new byte[endPosition-startPosition];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜