Trimming audio (mp3 or wav file)

限于喜欢 提交于 2019-12-08 02:25:28

问题


I have a quick question regarding the modification of an audio file. I can access the samples through the input stream(decoding etc), but I cannot figure out a way to manipulate the samples in that input stream(could be the case of multiple samples).
What I would like to achieve is to get a file Ex A.mp3, cut out some seconds out of it and finally store it to a new file.

By looking at the JavaSoundApi examples, I found that a file can be created in the following way:

    File fileOut = new File(someNewPathName);
    AudioFileFormat.Type fileType = fileFormat.getType();
    if (AudioSystem.isFileTypeSupported(fileType,audioInputStream)) {
        AudioSystem.write(audioInputStream, fileType, fileOut);
    }

回答1:


You just need to make the AudioInputStream lie about its size. To do that you can create your own AudioInputStream class and use delegation to the normal AudioInputStream you obtained from the sound API (skeleton only):

public class CuttingAudioInputStream extends AudioInputStream {

    private AudioInputStream delegate;

    public CuttingAudioInputStream(AudioInputStream delegate) {
        this.delegate = delegate;
    }

    /**
     * Lie about length of delegate
     */
    public long getFrameLength() {
        // simple demo, make the caller think the stream is 44100 samples long
        return Math.min(delegate.getFrameLength(), 44100); 
    }

    // Overrides for all other methods of AudioInputStream, 
    // left as excercise to the reader
}

You just need to implement all the other methods to adhere to the limits you want. You can also cut out parts in the middle, its just a little more complicated in the read() methods.

However, I feel obliged to point out that this approach may degrate the audio quality, if the source/destination is a lossy format (e.g. an MP3 is decoded and re-encoded). If the quality loss is not acceptable (should only matter if you process the same stream multiple times), you would be better of not decoding the files but apply the cutting process directly to the encoded data. Of course this is more complicated (you need to have a good understanding of each format you want to process) and there are restrictions to where you can cut imposed by the different formats.




回答2:


Your tutorial is for Java 1.5! There are more current versions.

On the page you cite, there is some sample code under the heading "Reading Sound Files". Note the area with the comments:

  // Here, do something useful with the audio data that's 
  // now in the audioBytes array...

This is within the while loop where the audioInputStream is being read. If you keep track of the frames as they pass through this area, you can calculate the elapsed time. The data that is loaded can be saved to a byte array. Or perhaps data within the desired frames can be the output of a class that reads the audioInputStream and implements TargetDataLine. If I recall correctly, a TargetDataLine can be made to work with AudioSystem.write(...

I don't know about directly editing compressed data or even a wav. Wav formats aren't uniform, don't have uniform headers. That's why I'd let Java read the stream, and use the data as it gets exposed that way.



来源:https://stackoverflow.com/questions/11775232/trimming-audio-mp3-or-wav-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!