How do I get a mp3 file's total time in Java?
The answers provided in How do I get a sound file’s total time in Java? work well for wav files, but not for mp3 files.
They are (given a file):
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long frames = audioInputStream.getFrameLength();
double durationInSeconds = (frames+0.0) / format.getFrameRate();
and:
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));
They give the same correct result for wav files, but wrong and different results for mp3 fil开发者_如何学运维es.
Any idea what do I have to do to get the mp3 file's duration?
Using MP3SPI:
private static void getDurationWithMp3Spi(File file) throws UnsupportedAudioFileException, IOException {
AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
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;
System.out.println("time = " + min + ":" + sec);
} else {
throw new UnsupportedAudioFileException();
}
}
Here is the way I get the total time of a file .mp3, I'm using the library is Jlayer 1.0.1
Header h = null;
FileInputStream file = null;
try {
file = new FileInputStream(filename);
} catch (FileNotFoundException ex) {
Logger.getLogger(MP3.class.getName()).log(Level.SEVERE, null, ex);
}
bitstream = new Bitstream(file);
try {
h = bitstream.readFrame();
} catch (BitstreamException ex) {
Logger.getLogger(MP3.class.getName()).log(Level.SEVERE, null, ex);
}
int size = h.calculate_framesize();
float ms_per_frame = h.ms_per_frame();
int maxSize = h.max_number_of_frames(10000);
float t = h.total_ms(size);
long tn = 0;
try {
tn = file.getChannel().size();
} catch (IOException ex) {
Logger.getLogger(MP3.class.getName()).log(Level.SEVERE, null, ex);
}
//System.out.println("Chanel: " + file.getChannel().size());
int min = h.min_number_of_frames(500);
return h.total_ms((int) tn)/1000;
Here is a detailed explanation of the MP3 File structure
http://www.autohotkey.com/forum/topic29420.html
Use jave-1.0.1.jar
library.
- It passed my test on wave file formats such as: wav-pcm8/16, wav-alaw, wav-ulaw, wav-gsm, wav-adpcm;
- It passed my test on some MP3 file formats: cbr vs vbr, stereo vs SingleChannel;
- It can support video formats, which I haven't tested on.
Code sample:
File source = new File("C:\\22.mp3");
Encoder encoder = new Encoder();
try {
MultimediaInfo mi = encoder.getInfo(source);
long ls = mi.getDuration();
System.out.println("duration(sec) = "+ ls/1000);
} catch (Exception e) {
e.printStackTrace();
}
I've tried Jlayer 1.0.1
, but it failed for some MP3 format. As for another libaray jaudiotagger-2.0.3.jar
, it works fine for MP3 formats, but it can only support wav-pcm8/16.
I'm old-fashioned in this, but I always simply get the specs for MP3, write a function that searches the right bits, and find it out.
If Winamp can determine this by reading the bitstream of an MP3 file, then so can I right?
And so can you, I believe in you mate!
精彩评论