MP3 Duration Java

孤街浪徒 提交于 2019-12-11 05:04:58

问题


Having an awful time trying to get the accurate time for a set of MP3s. I have these following properties that are generated using the MP3SPI 1.9.5 library.

//      mp3.crc=true
//      mp3.copyright=true
//      mp3.padding=false
//      mp3.channels=1
//      mp3.version.mpeg=2.5
//      mp3.length.bytes=6425480
//      mp3.framerate.fps=27.777779
//      mp3.framesize.bytes=140
//      duration=1606356000
//      mp3.version.layer=3
//      mp3.length.frames=44621
//      mp3.frequency.hz=8000
//      mp3.header.pos=0
//      mp3.bitrate.nominal.bps=16000
//      mp3.vbr.scale=0
//      mp3.version.encoding=MPEG2DOT5L3
//      mp3.mode=3
//      mp3.vbr=false
//      mp3.original=false

Now the file I am reading has a duration of 47:35 as reported by iTunes, and 48:50 using Mac Preview.

When I get the duration in Java using the library I get 26:46:

AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(f);
            Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
            String key = "duration";
            long duration = ((Long) properties.get("duration")) / 1000;
            {
                String frameBased = String.format("Duration Tag: %d hours, %d min, %d sec",
                        TimeUnit.MILLISECONDS.toHours(duration),
                        TimeUnit.MILLISECONDS.toMinutes(duration),
                        TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration))
                    );
                    System.out.println(frameBased);
            }

I'm not having much luck, so I was wondering if I'm doing something dumb, or if I can use the information within the MP3 tags to calculate my actual length? Given iTunes is reporting it correctly I assume I should be able to.


回答1:


Given iTunes is reporting it correctly I assume I should be able to.

One fail-safe way to determine the length of the track is to convert it into a standard AudioInputStream and then measure the AIS.

Or, as per the comment of @Kilian Foth:

Unfortunately, that is also the only fail-safe way.



来源:https://stackoverflow.com/questions/6776090/mp3-duration-java

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