1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import java.io.*; import javax.sound.sampled.*;
public class AudioFileProcessor {
public static void CutAudio(String sourceFileName, String destinationFileName, int start, int end) { AudioInputStream inputStream = null; AudioInputStream shortenedStream = null; try { File file = new File(sourceFileName); AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file); AudioFormat format = fileFormat.getFormat(); inputStream = AudioSystem.getAudioInputStream(file); float bytesPerSecond = format.getFrameSize() * format.getFrameRate()/1000; inputStream.skip((long)(start * bytesPerSecond)); long framesOfAudioToCopy =(long)( (end-start) * format.getFrameRate()/1000); shortenedStream = new AudioInputStream(inputStream, format, framesOfAudioToCopy); File destinationFile = new File(destinationFileName); AudioSystem.write(shortenedStream, fileFormat.getType(), destinationFile); } catch (Exception e) { System.out.println(e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { System.out.println(e); } }
if (shortenedStream != null) { try { shortenedStream.close(); } catch (Exception e) { System.out.println(e); } } } }
}
|