4

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.

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33

5 Answers5

16

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..!

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33
  • do you aware to the fact that FFMPEG is under GPL licence? – Nativ Aug 06 '14 at 09:54
  • ffmpeg-2.5.4.tar .i could not imort this ibrary into java project. – Faisal Ashraf Feb 26 '15 at 12:59
  • Sallu bahi can u tell me how can add this library into java project? – Faisal Ashraf Feb 26 '15 at 13:00
  • 1
    @Faisal Ashraf . bro, there are many ways(e.g maven, gradle dependancies) you can add libs into your project but here i am telling you the simple one. follow these steps. 1. download libs 2. add into your project path 3. then select all those libs using mouse click 4. right click on all of them and go to option "build path" -> "add to build path" 5. improt classes where required all done. – Muhammad Suleman Feb 27 '15 at 06:42
  • Sallu bro when i am import android-ffmpeg-library library into project RED ! , this sign will be show and my this project is not builting, and path could not set can u give me your skype address. – Faisal Ashraf Mar 03 '15 at 09:30
  • RED lines mean you are not successfully config your libs path. i would suggest try to search online that how to add libs into eclipes project and follow that guide – Muhammad Suleman Mar 03 '15 at 09:43
3

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) {
        }
    }
}
Soley
  • 1,716
  • 1
  • 19
  • 33
  • You can also add **System.out.println(event.getTimeStamp() / 1000);** to see the progress. // added – Soley Jul 10 '15 at 18:23
2

You should make the writer listen to the reader for packets.

reader.addListener(writer);

That's all you need to get it working.

0

In my code, after follow @Kostas Andrianopoulos 's answer. Throws exception:"Error Operation not permitted, failed to write header to container .....". After the modification, the codes are:

    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)
        ;

All things are OK.But the most import thing is: I do not know WHY? what's meaning of addAudioStream's first paramer(after reading the document), So Would somebody give me some infomation, thanks.

yetuweiba
  • 241
  • 1
  • 4
  • 13
0

I think this is an improvement, it's 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();
        }
    }
}
clemens
  • 16,716
  • 11
  • 50
  • 65