public void convertWavToMP3(String filepathToConvert, String convertedFilePath) { Encoder wavToMp3Converter = new Encoder(); EncodingAttributes att = new EncodingAttributes(); AudioAttributes audioAtt = new AudioAttributes(); String codecs[] = null; String formats[] = null; try { formats = wavToMp3Converter.getSupportedDecodingFormats(); } catch (EncoderException e) { e.printStackTrace(); } try { codecs = wavToMp3Converter.getAudioEncoders(); } catch (EncoderException e) { e.printStackTrace(); } int mp3CodecIndex = -1; /*for(int i=codecs.length; i>0; i--) { if(codecs[i].compareTo("") == 0) { mp3CodecIndex = i; break; } }*/ audioAtt.setVolume(255); audioAtt.setSamplingRate(new Integer(this.getWavTagReader().sampleRate)); audioAtt.setChannels(new Integer(wavTagReader.numOfChannels)); audioAtt.setBitRate( new Integer( this.wavTagReader.sampleRate * this.wavTagReader.bitsPerSample * this.wavTagReader.numOfChannels)); audioAtt.setCodec("libmp3lame"); att.setAudioAttributes(audioAtt); att.setFormat("mp3"); att.setOffset(new Float((float) 0)); att.setVideoAttributes(null); // att.setDuration(new Float((float)100)); try { wavToMp3Converter.encode( new File(filepathToConvert), new File(convertedFilePath), att, new EncoderProgressListener() { @Override public void sourceInfo(MultimediaInfo multimediaInfo) {} @Override public void progress(int i) {} @Override public void message(String s) {} }); } catch (EncoderException e) { e.getMessage(); e.getCause(); } }
@Test public void getInfo() throws InputFormatException, EncoderException { Encoder encoder = new Encoder(); MultimediaInfo info = encoder.getInfo(new File("src/test/resources/sample.mp4")); System.out.println(info.getFormat()); System.out.println(info.getAudio().getDecoder()); System.out.println(info.getVideo().getDecoder()); }
/** * Decodes an audio file (mp3, flac, wav, etc. everything which can be decoded by ffmpeg) to a * downsampled wav file. * * @param input an audio file which will be decoded to wav * @param wavoutput the output wav file * @param samplerate the samplerate of the output wav. * @throws IllegalArgumentException * @throws InputFormatException * @throws EncoderException */ public static void decodeAudioFile(File input, File wavoutput, int samplerate) throws IllegalArgumentException, InputFormatException, EncoderException { assert wavoutput.getName().endsWith(".wav"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("pcm_s16le"); audio.setChannels(Integer.valueOf(1)); audio.setSamplingRate(new Integer(samplerate)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("wav"); attrs.setAudioAttributes(audio); Encoder encoder = new Encoder(); encoder.encode(input, wavoutput, attrs); }
/** * Converts WAV file to MP3 * * @param filePath path on which MP3 file will be saved */ public void convertWavToMP3(String filePath) { String codecs[]; String formats[]; Encoder encoder = new Encoder(); EncodingAttributes att = new EncodingAttributes(); try { codecs = encoder.getAudioEncoders(); formats = encoder.getSupportedEncodingFormats(); AudioAttributes audioAtt = new AudioAttributes(); audioAtt.setBitRate( this.wavTagReader.getByteRate() * this.wavTagReader.getBitsPerSample() * this.wavTagReader.getNumOfChannels()); audioAtt.setChannels(Integer.valueOf(this.wavTagReader.getNumOfChannels())); audioAtt.setSamplingRate(this.wavTagReader.getSampleRate()); audioAtt.setVolume(255); audioAtt.setCodec(AudioAttributes.DIRECT_STREAM_COPY); att.setAudioAttributes(audioAtt); att.setFormat("mp3"); encoder.encode(this.file, new File(filePath), att); } catch (EncoderException e) { e.printStackTrace(); } }