如果我们在不同格式之间进行转换,我认为播放音频文件的总持续时间应该没有差异。
例如,如果我记录总持续时间为2秒的波形文件,则其大小现在为20.3 MB。现在我使用ffmpeg最新版本将此波形文件转换为mp3文件,它的大小变为1.35 mb。现在我使用下面的代码获得相同转换MP3文件的持续时间。
public static String getDurationWithMp3Spi(File file)
throws UnsupportedAudioFileException, IOException, Exception {
AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
System.out.println(" File for duration MP3 " + file.getAbsolutePath());
if (fileFormat instanceof TAudioFileFormat) {
Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
String key = "duration";
Long microseconds = (Long) properties.get(key);
int mili = (int) (microseconds / 1000);
int sec = (mili / 1000) % 60;
int min = (mili / 1000) / 60;
String mp3Len = null;
String mins = null;
String secs = null;
if (min == 0) {
mins = "00";
}
if (min < 10) {
mins = "0" + min;
}else{
mins = "" + min;
}
if (sec == 0) {
secs = "00";
}
if (sec < 10) {
secs = "0" + sec;
}else{
secs = ""+secs;
}
mp3Len = mins + ":" + secs;
System.out.println("time = " + min + ":" + sec);
return mp3Len;
} else {
throw new UnsupportedAudioFileException();
}
}
如果我在Windows媒体播放器或任何其他播放器中看到它将显示与原始波浪相同的持续时间,但是当我从这种方法中获得时它与原件不同,并且差异非常大。
如果音频文件从WAVE转换为MP3,反之亦然,音频文件的持续时间是否有差异?
请帮忙。上面的代码使用MP3SPI插件。
上述方法进行转换并获得持续时间。
for (Iterator<FileItem> fileIter = fileList.iterator(); fileIter
.hasNext();) {
FileItem fileItem = fileIter.next();
// write file to disk to specified path
if (!fileItem.isFormField()) {
String fileName = fileItem.getName();
System.out.println(" file Name " + fileName);
// save file to desired destination
waveFileSavePath = processFolderAppendee(waveFileSavePath,
fileName);
File waveFile = new File(waveFileSavePath);
fileItem.write(waveFile);
Thread.sleep(100);
// do conversion
String mp3FileName = fileName.replace("wav", "mp3");
mp3Path = mp3Path + "/" + mp3FileName;
convertToMP3(servletContext, waveFileSavePath, mp3Path);
Thread.sleep(100);
// prepare data(s)
GuestMessagesForm guestMessageForm = prepareGuestMessageData(
accountId, waveFileSavePath, mp3Path);
PlayListMessagesForm playListMessageForm = preparePlayListMessageData(accountId);
// save data(s)
//此方法调用持续时间 saveGuestMessage(guestMessageForm); savePlayListMessage(playListMessageForm);
} else {
// do nothing
}
waveFileSavePath = servletContext.getRealPath(recordDir);
}