mydomain
No ADS
No ADS

Java JAVE Convert audio and video to mp3

  1. Library
  2. Examples
  3. Other Examples
  4. Reference
JAVE (Java Audio Video Encoder) is an open source Java library that allows converting audio or video to different formats. For example, it helps you convert audio or video to MP3 format.
Essentially, JAVE is a wrapper that calls FFMPEG commands to perform format conversion.
FFmpeg is the leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created. It supports the most obscure ancient formats up to the cutting edge. No matter if they were designed by some standards committee, the community or a corporation.
FFmpeg is highly portable, it can be compiled and run on most operating systems such as Linux, Mac OS X, Microsoft Windows, BSD, Solaris, etc.
  • Introduction to FFMpeg
In this article, I will show you how to use Java JAVE to convert any video or audio to MP3 format.

1. Library

No ADS
<!-- https://mvnrepository.com/artifact/ws.schild/jave-all-deps --> 
<dependency>
  <groupId>ws.schild</groupId>
  <artifactId>jave-all-deps</artifactId>
  <version>3.3.1</version>
</dependency> 


<!-- https://mvnrepository.com/artifact/ws.schild/jave-core --> 
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-core</artifactId>
    <version>3.3.1</version>
</dependency>   

<!-- Log library used by JAVE -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.9</version>
</dependency>

<!-- Log library used by JAVE -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.9</version>
</dependency>

2. Examples

libmp3lame is an MP3 library included in the FFmpeg project, used to convert any video or audio format to MP3 format.
No ADS
ConvertToMp3.java
package org.o7planning.java_14173_mp3;

import java.io.File;

import ws.schild.jave.Encoder;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes; 

// Convert any video (or audio) to MP3.
public class ConvertToMp3 {

	private static void convertToMP3(File source, File target) throws Exception {
		// Audio Attributes
		AudioAttributes audio = new AudioAttributes();
		audio.setCodec("libmp3lame");
		audio.setBitRate(128000);
		audio.setChannels(2);
		audio.setSamplingRate(44100);

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

		// Encode
		Encoder encoder = new Encoder();
		encoder.encode(new MultimediaObject(source), target, attrs);
	}

	public static void main(String[] args) throws Exception {
		File wavFile = new File("/Volumes/New/Test/wavFile.wav");
		File outputMp3File = new File("/Volumes/New/Test/output/outputFile.mp3");

		convertToMP3(wavFile, outputMp3File);
		System.out.println("Done!");
	}
}
Next is an advanced example, converting an MP4 video to MP3 with status information during conversion, this way you can observe the percentage of work that has been completed.
AdvancedExample.java
package org.o7planning.java_14173_mp3;

import java.io.File;

import ws.schild.jave.Encoder;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.progress.EncoderProgressListener;

public class AdvancedExample {

	private static void convertToMP3(File source, File target, //
			EncoderProgressListener listener) throws Exception {
		// Audio Attributes
		AudioAttributes audio = new AudioAttributes();
		audio.setCodec("libmp3lame");
		audio.setBitRate(128000);
		audio.setChannels(2);
		audio.setSamplingRate(44100);

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

		// Encode
		Encoder encoder = new Encoder();
		encoder.encode(new MultimediaObject(source), target, attrs, listener);
	}

	public static void main(String[] args) throws Exception {
		Runnable runnable = new Runnable() {
			public void run() {
				// Video File:
				File wavFile = new File("/Volumes/New/Test/sample-16m.mp4");

				File outputFile = new File("/Volumes/New/Test/output/output2.mp3");

				EncoderProgressListener listener = new ConvertProgressListener();
				try {
					convertToMP3(wavFile, outputFile, listener);
				} catch (Exception e) {
					System.err.println("Error: " + e);
				}
				System.out.println("Done!");
			}
		};
		Thread thread = new Thread(runnable);
		thread.start();
	}
}

class ConvertProgressListener implements EncoderProgressListener {

	public ConvertProgressListener() {
	}

	public void message(String m) {
		System.out.println("Message: " + m);
	}

	public void progress(int p) {
		// Find %100 progress
		double progress = p / 1000.00;
		System.out.println(progress);
	}

	public void sourceInfo(MultimediaInfo m) {
		int videoHeight = m.getVideo().getSize().getHeight();
		int videoWidth = m.getVideo().getSize().getWidth();
		System.out.println("Video Size: " + videoWidth + "/" + videoHeight);
		long duration = m.getDuration();
		System.out.println("Duration: " + duration + " milliseconds");
	}
}
Output:
Video Size: 1280/720
Duration: 997880 milliseconds
0.0
0.03
0.074
0.114
0.156
0.201
0.247
0.293
0.342
0.388
0.436
0.479
0.524
0.567
0.613
0.659
0.705
0.75
0.796
0.843
0.888
0.935
0.981
1.0
Done!

3. Other Examples

Through JAVE you can get supported Encoders and Decoders
No ADS
JAVESupportedInfo.java
package org.o7planning.java_14173_mp3;

import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;

public class JAVESupportedInfo {

	public static void showEncodersDecoders() throws EncoderException {
		Encoder encoder = new Encoder();
		// aac   aac_at   ac3   ac3_fixed   adpcm_adx   adpcm_argo ...
		String[] audioEncoders = encoder.getAudioEncoders();
		System.out.println("=== Audio Encoders: ====================\n");
		print(audioEncoders);
		// 8svx_exp   8svx_fib   aac   aac_fixed   aac_at   aac_latm ...
		String[] audioDecoders = encoder.getAudioDecoders();
		System.out.println("\n\n=== Audio Decoders: ====================\n");
		print(audioDecoders);

		// a64multi   a64multi5   alias_pix   amv   apng   asv1 ...
		String[] videoEncoders = encoder.getVideoEncoders();
		System.out.println("\n\n=== Video Encoders: ====================\n");
		print(videoEncoders);
		// 012v   4xm   8bps   aasc   agm   aic ...
		String[] videoDecoders = encoder.getVideoDecoders();
		System.out.println("\n\n=== VideoVideo Decoders: ====================\n");
		print(videoDecoders);
	}

	public static void showSupportedFormats() throws EncoderException {
		Encoder encoder = new Encoder();
		// 3g2   3gp   a64   ac3   adts   adx ...
		String[] supportedEncodingFormats = encoder.getSupportedEncodingFormats();
		System.out.println("\n\n=== Supported Encoding Formats: ====================\n");
		print(supportedEncodingFormats);
		// 3dostr   4xm   aa   aac   aax   ac3 ...  
		String[] supportedDecodingFormats = encoder.getSupportedDecodingFormats();
		System.out.println("\n\n=== Supported Decoding Formats: ====================\n");
		print(supportedDecodingFormats);
	}

	private static void print(String[] array) {
		int i = 0;
		for (String s : array) {
			i++;
			System.out.print(s + "   ");
			if (i % 6 == 0) {
				System.out.println();
			}
		}
	}

	public static void main(String[] args) throws EncoderException {
		showEncodersDecoders();
		showSupportedFormats();
	}
}
Output:
=== Audio Encoders: ====================

aac   aac_at   ac3   ac3_fixed   adpcm_adx   adpcm_argo   
g722   g726   g726le   adpcm_ima_alp   adpcm_ima_amv   adpcm_ima_apm   
adpcm_ima_qt   adpcm_ima_ssi   adpcm_ima_wav   adpcm_ms   adpcm_swf   adpcm_yamaha   
alac   alac_at   aptx   aptx_hd   comfortnoise   dca   
eac3   flac   g723_1   ilbc_at   mlp   mp2   
mp2fixed   libmp3lame   nellymoser   opus   libopus   pcm_alaw   
pcm_alaw_at   pcm_dvd   pcm_f32be   pcm_f32le   pcm_f64be   pcm_f64le   
pcm_mulaw   pcm_mulaw_at   pcm_s16be   pcm_s16be_planar   pcm_s16le   pcm_s16le_planar   
pcm_s24be   pcm_s24daud   pcm_s24le   pcm_s24le_planar   pcm_s32be   pcm_s32le   
pcm_s32le_planar   pcm_s64be   pcm_s64le   pcm_s8   pcm_s8_planar   pcm_u16be   
pcm_u16le   pcm_u24be   pcm_u24le   pcm_u32be   pcm_u32le   pcm_u8   
pcm_vidc   real_144   roq_dpcm   s302m   sbc   sonic   
sonicls   truehd   tta   vorbis   libvorbis   wavpack   
wmav1   wmav2   

=== Audio Decoders: ====================

8svx_exp   8svx_fib   aac   aac_fixed   aac_at   aac_latm   
ac3   ac3_fixed   ac3_at   acelp.kelvin   adpcm_4xm   adpcm_adx   
adpcm_afc   adpcm_agm   adpcm_aica   adpcm_argo   adpcm_ct   adpcm_dtk   
adpcm_ea   adpcm_ea_maxis_xa   adpcm_ea_r1   adpcm_ea_r2   adpcm_ea_r3   adpcm_ea_xas   
g722   g726   g726le   adpcm_ima_alp   adpcm_ima_amv   adpcm_ima_apc   
adpcm_ima_apm   adpcm_ima_cunning   adpcm_ima_dat4   adpcm_ima_dk3   adpcm_ima_dk4   adpcm_ima_ea_eacs   
adpcm_ima_ea_sead   adpcm_ima_iss   adpcm_ima_moflex   adpcm_ima_mtf   adpcm_ima_oki   adpcm_ima_qt   
adpcm_ima_qt_at   adpcm_ima_rad   adpcm_ima_smjpeg   adpcm_ima_ssi   adpcm_ima_wav   adpcm_ima_ws   
adpcm_ms   adpcm_mtaf   adpcm_psx   adpcm_sbpro_2   adpcm_sbpro_3   adpcm_sbpro_4   
adpcm_swf   adpcm_thp   adpcm_thp_le   adpcm_vima   adpcm_xa   adpcm_yamaha   
adpcm_zork   alac   alac_at   amrnb   amr_nb_at   amrwb   
ape   aptx   aptx_hd   atrac1   atrac3   atrac3al   
atrac3plus   atrac3plusal   atrac9   on2avc   binkaudio_dct   binkaudio_rdft   
bmv_audio   comfortnoise   cook   derf_dpcm   dolby_e   dsd_lsbf   
dsd_lsbf_planar   dsd_msbf   dsd_msbf_planar   dsicinaudio   dss_sp   dst   
dca   dvaudio   eac3   eac3_at   evrc   fastaudio   
flac   g723_1   g729   gremlin_dpcm   gsm   gsm_ms   
gsm_ms_at   hca   hcom   iac   ilbc   ilbc_at   
imc   interplay_dpcm   interplayacm   mace3   mace6   metasound   
mlp   mp1   mp1float   mp1_at   mp2   mp2float   
mp2_at   mp3float   mp3   mp3_at   mp3adufloat   mp3adu   
mp3on4float   mp3on4   als   mpc7   mpc8   nellymoser   
opus   libopus   paf_audio   pcm_alaw   pcm_alaw_at   pcm_bluray   
pcm_dvd   pcm_f16le   pcm_f24le   pcm_f32be   pcm_f32le   pcm_f64be   
pcm_f64le   pcm_lxf   pcm_mulaw   pcm_mulaw_at   pcm_s16be   pcm_s16be_planar   
pcm_s16le   pcm_s16le_planar   pcm_s24be   pcm_s24daud   pcm_s24le   pcm_s24le_planar   
pcm_s32be   pcm_s32le   pcm_s32le_planar   pcm_s64be   pcm_s64le   pcm_s8   
pcm_s8_planar   pcm_sga   pcm_u16be   pcm_u16le   pcm_u24be   pcm_u24le   
pcm_u32be   pcm_u32le   pcm_u8   pcm_vidc   qcelp   qdm2   
qdm2_at   qdmc   qdmc_at   real_144   real_288   ralf   
roq_dpcm   s302m   sbc   sdx2_dpcm   shorten   sipr   
siren   smackaud   sol_dpcm   sonic   tak   truehd   
truespeech   tta   twinvq   vmdaudio   vorbis   libvorbis   
wavesynth   wavpack   ws_snd1   wmalossless   wmapro   wmav1   
wmav2   wmavoice   xan_dpcm   xma1   xma2   

=== Video Encoders: ====================

a64multi   a64multi5   alias_pix   amv   apng   asv1   
asv2   libaom-av1   libsvtav1   avrp   avui   ayuv   
bmp   cfhd   cinepak   cljr   vc2   dnxhd   
dpx   dvvideo   exr   ffv1   ffvhuff   fits   
flashsv   flashsv2   flv   gif   h261   h263   
h263p   libx264   libx264rgb   h264_videotoolbox   hap   libx265   
hevc_videotoolbox   huffyuv   jpeg2000   libopenjpeg   jpegls   ljpeg   
magicyuv   mjpeg   mpeg1video   mpeg2video   mpeg4   msmpeg4v2   
msmpeg4   msvideo1   pam   pbm   pcx   pfm   
pgm   pgmyuv   png   ppm   prores   prores_aw   
prores_ks   qtrle   r10k   r210   rawvideo   roqvideo   
rpza   rv10   rv20   sgi   snow   speedhq   
sunrast   svq1   targa   libtheora   tiff   utvideo   
v210   v308   v408   v410   libvpx   libvpx-vp9   
libwebp_anim   libwebp   wmv1   wmv2   wrapped_avframe   xbm   
xface   xwd   y41p   yuv4   zlib   zmbv   


=== VideoVideo Decoders: ====================

012v   4xm   8bps   aasc   agm   aic   
alias_pix   amv   anm   ansi   apng   arbc   
argo   asv1   asv2   aura   aura2   libaom-av1   
av1   avrn   avrp   avs   avui   ayuv   
bethsoftvid   bfi   binkvideo   bintext   bitpacked   bmp   
bmv_video   brender_pix   c93   cavs   cdgraphics   cdtoons   
cdxl   cfhd   cinepak   clearvideo   cljr   cllc   
eacmv   cpia   cri   camstudio   cyuv   dds   
dfa   dirac   dnxhd   dpx   dsicinvideo   dvvideo   
dxa   dxtory   dxv   escape124   escape130   exr   
ffv1   ffvhuff   fic   fits   flashsv   flashsv2   
flic   flv   fmvc   fraps   frwu   g2m   
gdv   gif   h261   h263   h263i   h263p   
h264   hap   hevc   hnm4video   hq_hqa   hqx   
huffyuv   hymt   idcinvideo   idf   iff   imm4   
imm5   indeo2   indeo3   indeo4   indeo5   interplayvideo   
ipu   jpeg2000   libopenjpeg   jpegls   jv   kgv1   
kmvc   lagarith   loco   lscr   m101   eamad   
magicyuv   mdec   mimic   mjpeg   mjpegb   mmvideo   
mobiclip   motionpixels   mpeg1video   mpeg2video   mpegvideo   mpeg4   
msa1   mscc   msmpeg4v1   msmpeg4v2   msmpeg4   msp2   
msrle   mss1   mss2   msvideo1   mszh   mts2   
mv30   mvc1   mvc2   mvdv   mvha   mwsc   
mxpeg   notchlc   nuv   paf_video   pam   pbm   
pcx   pfm   pgm   pgmyuv   pgx   photocd   
pictor   pixlet   png   ppm   prores   prosumer   
psd   ptx   qdraw   qpeg   qtrle   r10k   
r210   rasc   rawvideo   rl2   roqvideo   rpza   
rscc   rv10   rv20   rv30   rv40   sanm   
scpr   screenpresso   sga   sgi   sgirle   sheervideo   
simbiosis_imx   smackvid   smc   smvjpeg   snow   sp5x   
speedhq   srgc   sunrast   svq1   svq3   targa   
targa_y216   tdsc   eatgq   eatgv   theora   thp   
tiertexseqvideo   tiff   tmv   eatqi   truemotion1   truemotion2   
truemotion2rt   camtasia   tscc2   txd   ultimotion   utvideo   
v210   v210x   v308   v408   v410   vb   
vble   vc1   vc1image   vcr1   xl   vmdvideo   
vmnc   vp3   vp4   vp5   vp6   vp6a   
vp6f   vp7   vp8   libvpx   vp9   libvpx-vp9   
wcmv   webp   wmv1   wmv2   wmv3   wmv3image   
wnv1   wrapped_avframe   vqavideo   xan_wc3   xan_wc4   xbin   
xbm   xface   xpm   xwd   y41p   ylc   
yop   yuv4   zerocodec   zlib   zmbv   

=== Supported Encoding Formats: ====================

3g2   3gp   a64   ac3   adts   adx   
aiff   alaw   alp   amr   amv   apm   
apng   aptx   aptx_hd   argo_asf   asf   asf_stream   
ass   ast   au   audiotoolbox   avi   avm2   
avs2   bit   caf   cavsvideo   codec2   codec2raw   
crc   dash   data   daud   dirac   dnxhd   
dts   dv   dvd   eac3   f32be   f32le   
f4v   f64be   f64le   ffmetadata   fifo   fifo_test   
film_cpk   filmstrip   fits   flac   flv   framecrc   
framehash   framemd5   g722   g723_1   g726   g726le   
gif   gsm   gxf   h261   h263   h264   
hash   hds   hevc   hls   ico   ilbc   
image2   image2pipe   ipod   ircam   ismv   ivf   
jacosub   kvag   latm   lrc   m4v   matroska   
md5   microdvd   mjpeg   mkvtimestamp_v2   mlp   mmf   
mov   mp2   mp3   mp4   mpeg   mpeg1video   
mpeg2video   mpegts   mpjpeg   mulaw   mxf   mxf_d10   
mxf_opatom   null   nut   oga   ogg   ogv   
oma   opus   psp   rawvideo   rm   roq   
rso   rtp   rtp_mpegts   rtsp   s16be   s16le   
s24be   s24le   s32be   s32le   s8   sap   
sbc   scc   segment   singlejpeg   smjpeg   smoothstreaming   
sox   spdif   spx   srt   stream_segment   ssegment   
streamhash   sup   svcd   swf   tee   truehd   
tta   ttml   u16be   u16le   u24be   u24le   
u32be   u32le   u8   uncodedframecrc   vc1   vc1test   
vcd   vidc   vob   voc   w64   wav   
webm   webm_chunk   webm_dash_manifest   webp   webvtt   wtv   
wv   yuv4mpegpipe   

=== Supported Decoding Formats: ====================

3dostr   4xm   aa   aac   aax   ac3   
ace   acm   act   adf   adp   ads   
adx   aea   afc   aiff   aix   alaw   
alias_pix   alp   amr   amrnb   amrwb   anm   
apc   ape   apm   apng   aptx   aptx_hd   
aqtitle   argo_asf   argo_brp   asf   asf_o   ass   
ast   au   av1   avfoundation   avi   avr   
avs   avs2   avs3   bethsoftvid   bfi   bfstm   
bin   bink   binka   bit   bmp_pipe   bmv   
boa   brender_pix   brstm   c93   caf   cavsvideo   
cdg   cdxl   cine   codec2   codec2raw   concat   
cri_pipe   data   daud   dcstr   dds_pipe   derf   
dfa   dhav   dirac   dnxhd   dpx_pipe   dsf   
dsicin   dss   dts   dtshd   dv   dvbsub   
dvbtxt   dxa   ea   ea_cdata   eac3   epaf   
exr_pipe   f32be   f32le   f64be   f64le   ffmetadata   
film_cpk   filmstrip   fits   flac   flic   flv   
frm   fsb   fwse   g722   g723_1   g726   
g726le   g729   gdv   genh   gif   gif_pipe   
gsm   gxf   h261   h263   h264   hca   
hcom   hevc   hls   hnm   ico   idcin   
idf   iff   ifv   ilbc   image2   image2pipe   
ingenient   ipmovie   ipu   ircam   iss   iv8   
ivf   ivr   j2k_pipe   jacosub   jpeg_pipe   jpegls_pipe   
jv   kux   kvag   lavfi   live_flv   lmlm4   
loas   lrc   luodat   lvf   lxf   m4v   
matroska   webm   mca   mcc   mgsts   microdvd   
mjpeg   mjpeg_2000   mlp   mlv   mm   mmf   
mods   moflex   mov   mp4   m4a   3gp   
3g2   mj2   mp3   mpc   mpc8   mpeg   
mpegts   mpegtsraw   mpegvideo   mpjpeg   mpl2   mpsub   
msf   msnwctcp   msp   mtaf   mtv   mulaw   
musx   mv   mvi   mxf   mxg   nc   
nistsphere   nsp   nsv   nut   nuv   obu   
ogg   oma   paf   pam_pipe   pbm_pipe   pcx_pipe   
pgm_pipe   pgmyuv_pipe   pgx_pipe   photocd_pipe   pictor_pipe   pjs   
pmp   png_pipe   pp_bnk   ppm_pipe   psd_pipe   psxstr   
pva   pvf   qcp   qdraw_pipe   r3d   rawvideo   
realtext   redspark   rl2   rm   roq   rpl   
rsd   rso   rtp   rtsp   s16be   s16le   
s24be   s24le   s32be   s32le   s337m   s8   
sami   sap   sbc   sbg   scc   sdp   
sdr2   sds   sdx   ser   sga   sgi_pipe   
shn   siff   simbiosis_imx   sln   smjpeg   smk   
smush   sol   sox   spdif   srt   stl   
subviewer   subviewer1   sunrast_pipe   sup   svag   svg_pipe   
svs   swf   tak   tedcaptions   thp   tiertexseq   
tiff_pipe   tmv   truehd   tta   tty   txd   
ty   u16be   u16le   u24be   u24le   u32be   
u32le   u8   v210   v210x   vag   vc1   
vc1test   vidc   vividas   vivo   vmd   vobsub   
voc   vpk   vplayer   vqf   w64   wav   
wc3movie   webm_dash_manifest   webp_pipe   webvtt   wsaud   wsd   
wsvqa   wtv   wv   wve   xa   xbin   
xbm_pipe   xmv   xpm_pipe   xvag   xwd_pipe   xwma   
yop   yuv4mpegpipe   
No ADS
No ADS