How to extract audio from video using xuggler?

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 he does not create an audio file for me. Please guide me where I am doing wrong. if you fix it or provide some other code for this purpose that is different from mine, then I will be grateful.

+4
source share
5 answers

after a lot of searching on the Internet about this problem, I found that I can do the same job with JAVE (Java Audio Video Encoder), so I try to do this and it works for me .. so I thought I would post a solution there if anyone someone else is facing the same problem, then he / she can see my work.

actually uses ffmpeg behind the scenes, and it will extract audio from the video file and much more for encoding. here is the link for jave http://www.sauronsoftware.it/projects/jave/index.php

Also see one example 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); 

hope this helps you ...!

+14
source

You must force the writer to listen to the reader for packages.

 reader.addListener(writer); 

This is all you need to get it working.

+2
source

This is an old post, however, I hope my answer helps someone:

I used the same code as in the question, but I ran into a problem because some files could not be converted using this code.

In this answer, I open the file, and when it contains audio, I create the file. Otherwise, the file will not be created. Thus, this is the right way to extract the audio part of any file.

The same picture can be applied to the video. I think, I think this code will also give this idea.

 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) { } } } 
+2
source

In my code, after @Kostas Andrianopoulos answer, answer. Throws an exception: "The error is not resolved, the header could not be written to the container .....". After changing the codes:

  IMediaReader reader = ToolFactory.makeReader("D:\\123.flv"); IMediaWriter writer = ToolFactory.makeWriter("D:\\output.mp3", reader); int sampleRate = 44100; int channels = 2; writer.setMaskLateStreamExceptions(true); writer.addAudioStream(1, 0, ICodec.ID.CODEC_ID_MP3, channels, sampleRate); reader.addListener(writer); while(reader.readPacket() == null) ; 

Everything is good. But the most important thing: I donโ€™t know WHY? what's the point of the first parameter addAudioStream (after reading the document), so someone will give me some information, thanks.

0
source

I think this is an improvement, it is good for n audio files.

 import com.xuggle.mediatool.IMediaWriter; import com.xuggle.mediatool.ToolFactory; import com.xuggle.xuggler.IAudioSamples; import com.xuggle.xuggler.IContainer; import com.xuggle.xuggler.IPacket; import com.xuggle.xuggler.IStreamCoder; public class Concatenador_Audios { public static void main(String[] args) { ConcatenarAudios("D:\\out concatenate.mp3", "D:\\in Audio (1).mp3", "D:\\in Audio (2).mp3", "D:\\in Audio (3).mp3"); } public static void ConcatenarAudios(String Ruta_AudioConcatenado,String... ruta_Audio) { int n = ruta_Audio.length; IMediaWriter mWriter = ToolFactory.makeWriter(Ruta_AudioConcatenado); IPacket packet = IPacket.make(); for (int i = 0; i < n; i++) { IContainer container = IContainer.make(); container.open(ruta_Audio[i], IContainer.Type.READ, null); IStreamCoder audio = container.getStream(0).getStreamCoder(); audio.open(null, null); if (i == 0) { mWriter.addAudioStream(0, 0, audio.getChannels(), audio.getSampleRate()); } while (container.readNextPacket(packet) >= 0) { IAudioSamples samples = IAudioSamples.make(512, audio.getChannels(), IAudioSamples.Format.FMT_S32); audio.decodeAudio(samples, packet, 0); mWriter.encodeAudio(0, samples); } container.close(); audio.close(); } mWriter.close(); } } } 
-1
source

Source: https://habr.com/ru/post/1435760/


All Articles