Check audio file duration in java
Asked Answered
N

2

1

I made a FileChooser program that gets the file path of .wav files but right now I want to add another condition. I only want to get the file path of wav files that has a maximum of 5 minutes. How do I do this?

Nigh answered 9/11, 2015 at 13:21 Comment(0)
R
5

if you are using JAudioTagger , you can simply get the accurate duration in second like below.

File target = new File("E:\\sample.wav");
AudioFile af = AudioFileIO.read(target);
AudioHeader ah = af.getAudioHeader();
System.out.println( ah.getTrackLength());

Here is a fully working sample code

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.AudioHeader;

import java.io.File;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * Created by RAGINROSE on 9/20/2019.
 */

public class AudioTest {
    public static void main(String[] args) {

            try {

                SimpleDateFormat timeInFormat = new SimpleDateFormat("ss", Locale.UK);
                SimpleDateFormat timeOutFormat = new SimpleDateFormat("mm:ss", Locale.UK);
                SimpleDateFormat timeOutOverAnHourFormat = new SimpleDateFormat("kk:mm:ss", Locale.UK);
                String duratin;

                File target = new File("E:\\sample.wav");
                AudioFile f = AudioFileIO.read(target);
                AudioHeader ah = f.getAudioHeader();

                long trackLength = (long)ah.getTrackLength();

                Date timeIn;
                synchronized(timeInFormat) {
                    timeIn = timeInFormat.parse(String.valueOf(trackLength));
                }
                if(trackLength < 3600L) {
                    synchronized(timeOutFormat) {
                        duratin =  timeOutFormat.format(timeIn);
                    }
                } else {
                    synchronized(timeOutOverAnHourFormat) {
                        duratin = timeOutOverAnHourFormat.format(timeIn);
                    }
                }

                System.out.println("Duration in Integer : " + trackLength);
                System.out.println("Duration in String : " +duratin);

            } catch (Exception e) {
                e.printStackTrace();
            }


    }
}

Don't Forget to add dependency

<dependency>
    <groupId>net.jthink</groupId>
    <artifactId>jaudiotagger</artifactId>
    <version>2.2.3</version>
</dependency>

The supported formats are

ogg, mp3, flac, mp4, m4a, m4p, wma, wav, ra, rm, m4b and aif

Hope this will be helpful for someone.

Rosalynrosalynd answered 20/9, 2019 at 12:35 Comment(0)
L
1

You could do it by knowing the file size in MB and then knowing the bit-rate of the file. Follow me on this MP3 example:

MP3 Filesize: 3.89MBytes
Bitrate: 128Kbits

Convert Mbytes to Kbits:

3.89Mbytes * 1024 = 3983Kbytes
3989Kbytes * 8 = 31866Kbits

Convert Kbits to Length:

31866Kbits / 128Kbits per second = 249 seconds
249 seconds / 60 seconds = 4.19 Minutes

Note that it is still not accurate representation of the duration and you will have to account for some variance in the calculation.

Lekishalela answered 9/11, 2015 at 13:28 Comment(4)
Isn't there a shorter way? like getduration() or something?Nigh
No, but you can create your own method getDuration() with the math I have provided you with.Lekishalela
What I mean is yes of course I can get the filesize and bitrate of audios by audioinputstream but what I want is to get the filesize and bitrate of an audio with just the file's path. But how?Nigh
Doesn't work for variable bit rate encoded MP3 files :-( I tried jaudiotagger from the answer above. It worked nicely :-)Lewanna

© 2022 - 2024 — McMap. All rights reserved.