Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
  • Flutter Apps
Menu
How to check the Аudio length from Java Spring Boot app

How to check the Аudio Length from Java App

Posted on December 9, 2024December 23, 2024 by Toma Velev

To check the Аudio Length in a Java Spring Boot application, you can use the following open source libraries or tools.

1. Using AudioFileFormat

THIS OPTION DID NOT WORK FOR ME – It throws IOException: mark/reset not supported. You can use the javax.sound.sampled.AudioFileFormat class to get the audio length.

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFileFormat;

public class AudioLengthChecker {
    public static void main(String[] args) throws Exception {
        // Load the audio file
        AudioInputStream ais = AudioSystem.getAudioInputStream(new FileInputStream("path_to_your_audio_file.wav"));

        // Get the audio format
        AudioFormat af = ais.getFormat();

        // Calculate the length in milliseconds
        int lengthInMs = (int) ais.getFrameLength() * af.getFrameSize();

        // Convert the length to seconds
        int lengthInSeconds = lengthInMs / 1000;

        System.out.println("Audio length: " + lengthInSeconds + " seconds");
    }
}

2. Using FFmpeg

You could use some library wrapper of Ffmpeg. It is a powerful tool for video, but audio also. Check out this stack overflow issue:

https://stackoverflow.com/questions/6239350/how-to-extract-duration-time-from-ffmpeg-output

I’ve used this tool before and you could read more here: https://programtom.com/dev/?s=Ffmpeg. I’ve also coded some wrapper apps: https://programtom.com/dev/product/video-converter-tool/

3 Using a third-party library jaudiotagger

I’ve used the jaudiotagger library. Here is the maven dependency:

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

And here is the working code:

try {
    AudioFile audioMetadata = AudioFileIO.read(new File(s));
    System.out.println("Audio Metadata {} " + audioMetadata.displayStructureAsPlainText());
    System.out.println(audioMetadata.getAudioHeader().getTrackLength());
    System.out.println(audioMetadata.getAudioHeader().getPreciseTrackLength());
    System.out.println(audioMetadata.getAudioHeader().getBitRate());
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException
         | InvalidAudioFrameException e) {
    throw new RuntimeException("Error while getting metadata for audio file. Error " + e.getLocalizedMessage());
}

Note that the above code snippets are just examples and may need to be modified based on your specific requirements.

  • What are ways to Optimize the backend endpoints in Spring Boot
  • Flutter image flickers
  • Could a Flutter App save a Flag even after uninstall
  • Could iOS flutter app logs be viewed while running in release mode – started after previous closed state
  • 6 Addictive Mobile Game Ideas Inspired by Flappy Bird’s Simplicity

Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (206)
  • Graphical User Interface (13)
  • Marketing (114)
  • Software Development (270)
  • Spring (43)
  • StartUp (21)
  • Uncategorized (4)
  • Uncategorized (15)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (83)
    • Flutter Apps (23)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • What are ways to Optimize the backend endpoints in Spring Boot
  • Flutter image flickers
  • Could a Flutter App save a Flag even after uninstall
  • Could iOS flutter app logs be viewed while running in release mode – started after previous closed state
  • 6 Addictive Mobile Game Ideas Inspired by Flappy Bird’s Simplicity

Post Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (206)
  • Graphical User Interface (13)
  • Marketing (114)
  • Software Development (270)
  • Spring (43)
  • StartUp (21)
  • Uncategorized (4)
  • Uncategorized (15)
  • Vaadin (14)