i am trying to make an audio file (*.mp3 , .wav etc) from a video file (.avi,*.mp4 etc) using xuggler here is my code
Code:
IMediaReader reader = ToolFactory.makeReader("D:/Frames/my.mp4");
IMediaWriter writer = ToolFactory.makeWriter("D:/a.mp3",reader);
int sampleRate = 44100;
int channels = 1;
writer.addAudioStream(0, 0, ICodec.ID.CODEC_ID_MP3, channels, sampleRate);
while (reader.readPacket() == null);
but its not create an audio file for me. please guide me where i am doing wrong. if you will correct it or provide some other code for this purpose which is different from mine then i'll be thankful.
after searching a lot on internet about this problem i have found that i can do this same work with JAVE (Java Audio Video Encoder) so i try that and it works for me ..so i thought i post the solution there that if some one else face the same problem then he/she can see my work.
its actually use ffmpeg behind the scene and it will extract audio from video file and much more for encoding stuff. here is the link for JAVE http://www.sauronsoftware.it/projects/jave/index.php
also see one example there and i am posting it here also for your convenience
File source = new File("source.mp4");
File target = new File("target.mp3");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(new Integer(128000));
audio.setChannels(new Integer(2));
audio.setSamplingRate(new Integer(44100));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);
hopefully it will help u..!
This is an old post, however, I hope my answer help someone:
I was using the same code as in question, but I ran into trouble as some files could not be converted using that code.
In this answer, I open the file, and when it contains audio, I create the file. Otherwise no file will be created. This way is the correct way to extract the audio part of any file.
The same pattern can be applied to video. Thought I think this code will give the idea to that as well.
package test.video;
import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.MediaToolAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IAudioSamplesEvent;
import com.xuggle.mediatool.event.ICloseEvent;
import com.xuggle.mediatool.event.IOpenCoderEvent;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IStreamCoder;
/**
*
* @author Pasban
*/
public class separateAudioVideo {
public static void main(String[] args) throws Exception {
String file = "pasban/22.mp4";
String to = "pasban/22.mp3";
convert(file, to);
}
public static void convert(String from, final String to) {
IMediaReader mediaReader = ToolFactory.makeReader(from);
final int mySampleRate = 44100;
final int myChannels = 2;
mediaReader.addListener(new MediaToolAdapter() {
private IContainer container;
private IMediaWriter mediaWriter;
@Override
public void onOpenCoder(IOpenCoderEvent event) {
container = event.getSource().getContainer();
mediaWriter = null;
}
@Override
public void onAudioSamples(IAudioSamplesEvent event) {
if (container != null) {
if (mediaWriter == null) {
mediaWriter = ToolFactory.makeWriter(to);
mediaWriter.addListener(new MediaListenerAdapter() {
@Override
public void onAddStream(IAddStreamEvent event) {
IStreamCoder streamCoder = event.getSource().getContainer().getStream(event.getStreamIndex()).getStreamCoder();
streamCoder.setFlag(IStreamCoder.Flags.FLAG_QSCALE, false);
streamCoder.setBitRate(128);
streamCoder.setChannels(myChannels);
streamCoder.setSampleRate(mySampleRate);
streamCoder.setBitRateTolerance(0);
}
});
mediaWriter.addAudioStream(0, 0, myChannels, mySampleRate);
}
mediaWriter.encodeAudio(0, event.getAudioSamples());
//System.out.println(event.getTimeStamp() / 1000);
}
}
@Override
public void onClose(ICloseEvent event) {
if (mediaWriter != null) {
mediaWriter.close();
}
}
});
while (mediaReader.readPacket() == null) {
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With