import ws.schild.jave.*;

public class AudioCutter {

public static void main(String[] args) throws Exception {
// 原始音频文件路径
String sourceAudio = "source.mp3";
// 截取后的音频文件路径
String targetAudio = "target.mp3";
// 开始时间(单位:毫秒)
long startTime = 5000;
// 结束时间(单位:毫秒)
long endTime = 10000;
// 创建音频编码属性对象
AudioAttributes audioAttr = new AudioAttributes();
EncodingAttributes encodingAttr = new EncodingAttributes();
encodingAttr.setAudioAttributes(audioAttr);
// 创建媒体对象
MultimediaObject multimediaObject = new MultimediaObject(new java.io.File(sourceAudio));
// 创建截取器
AudioStream audioStream = multimediaObject.getAudio();
audioStream.setStartTime(new Timecode(startTime, audioStream.getTimeBase()));
audioStream.setDuration(new Timecode(endTime - startTime, audioStream.getTimeBase()));
// 转换音频格式
Encoder encoder = new Encoder();
encoder.encode(new MultimediaObject(audioStream), new java.io.File(targetAudio), encodingAttr);
System.out.println("音频截取完成");
}
}

posted @ 2023-07-22 15:59  ENU  阅读(8)  评论(0编辑  收藏  举报