Audio Conversion From One Format to Another Format in Java

Posted By : Md Imroz Alam | 30-Apr-2019

To convert an audio file from one format to another format. Here, we are going to use JAVA library which internally uses FFmpeg tool inside. To run and use this library, make sure we have given full permission to that FFmpeg executable file. FFmpeg file can be found into the temp folder of the respective operating system directory. JAVA library support Linux, window, and Mac operating system. It also supports both 32 and 64-bit operating system environment.

The JAVE library is Java wrapper on the FFmpeg tool. FFmpeg is a free software tool which can use audio, video and other media file conversion and streaming. Programmer can take benefit of JAVE2 to transcode audio and video files from a format to another. we can change the size of the media file by changing media bitrate. 

Required Java Version:- Java 1.8 


Maven dependency:-

 
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>2.4.6</version>
</dependency>

Audio conversion from one file format to another using jave library
 try{
    AudioAttributes audio = new AudioAttributes();
    audio.setCodec("libmp3lame");
    audio.setBitRate(64000);
    audio.setChannels(2);
    audio.setSamplingRate(44100);

    //Encoding attributes
    EncodingAttributes attrs = new EncodingAttributes();
    attrs.setFormat("mp3");
    attrs.setAudioAttributes(audio);

    //Encode
    Encoder encoder = new Encoder();
    encoder.encode(new MultimediaObject(sourceFile), targetFile, attrs);
    }catch (IllegalArgumentException | EncoderException ex){
       LOGGER.error("ffmpeg ecoderException : {}",ex.getMessage());
    }
    catch (Exception ex) {
                 LOGGER.error("execption occured at audio conversion: {}",ex.getMessage());
    }
    finally {
    }

Note:- We can change the size of the final file with defining set sampling rate and set bitrate. JAVE Library throws an exception EncoderException, IllegalArgumentException when there is any wrong parameter passed to their library. As for example, setCodec value is predefined. Make sure these values are passed according to FFmpeg tool and JAVE standard supported type.


References:- 

https://www.sauronsoftware.it/projects/jave/
https://docs.oracle.com/javase/tutorial/sound/converters.html
https://codereview.stackexchange.com/questions/20084/converting-audio-to-different-file-formats
https://www.oracle.com/technetwork/java/javase/documentation/transcode-178272.html
https://stackoverflow.com/questions/640119/java-to-transcode-and-manipulate-mp3-files
https://www.javacodegeeks.com/2011/02/xuggler-tutorial-transcoding-media.html

Thanks 
I hope, this will be helpful.

 

About Author

Author Image
Md Imroz Alam

Md. Imroz Alam is a bright Web App Developer, he has good knowledge of Java, J2SE, Jsp, Servlet, jdbc. His hobbies are watching movie, playing carom.

Request for Proposal

Name is required

Comment is required

Sending message..