Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:33:22+00:00 2026-06-16T03:33:22+00:00

I am trying to convert an audio file with the header Opening audio decoder:

  • 0

I am trying to convert an audio file with the header

Opening audio decoder: [pcm] Uncompressed PCM audio decoder
AUDIO: 44100 Hz, 2 ch, s16le, 1411.2 kbit/100.00% (ratio: 176400->176400)
Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)

I want to transcode this file to mp3 format. I have following code snippet but its not working well. I have written it using XUGGLER code snippet for transcoding audio and video.

Audio decoder is

    audioDecoder = IStreamCoder.make(IStreamCoder.Direction.DECODING, ICodec.findDecodingCodec(ICodec.ID.CODEC_ID_PCM_S16LE));
    audioDecoder.setSampleRate(44100);
    audioDecoder.setBitRate(176400);
    audioDecoder.setChannels(2);
    audioDecoder.setTimeBase(IRational.make(1,1000));
    if (audioDecoder.open(IMetaData.make(), IMetaData.make()) < 0)
        return false;
    return true; 

Audio encoder is

    outContainer = IContainer.make();
    outContainerFormat = IContainerFormat.make();
    outContainerFormat.setOutputFormat("mp3", urlOut, null);
    int retVal = outContainer.open(urlOut, IContainer.Type.WRITE, outContainerFormat);
    if (retVal < 0) {
        System.out.println("Could not open output container");
        return false;
    }
    outAudioCoder = IStreamCoder.make(IStreamCoder.Direction.ENCODING, ICodec.findEncodingCodec(ICodec.ID.CODEC_ID_MP3));
    outAudioStream = outContainer.addNewStream(outAudioCoder);
    outAudioCoder.setSampleRate(new Integer(44100));
    outAudioCoder.setChannels(2);
    retVal = outAudioCoder.open(IMetaData.make(), IMetaData.make());
    if (retVal < 0) {
        System.out.println("Could not open audio coder");
        return false;
    }
    retVal = outContainer.writeHeader();
    if (retVal < 0) {
        System.out.println("Could not write output FLV header: ");
        return false;
    }
    return true;

And here is encode method where i send packets of 32 byte to transcode

public void encode(byte[] audioFrame){
    //duration of 1 video frame
    long lastVideoPts = 0;

    IPacket packet_out = IPacket.make();
    int lastPos = 0;
    int lastPos_out = 0;

    IAudioSamples audioSamples = IAudioSamples.make(48000, audioDecoder.getChannels());
    IAudioSamples audioSamples_resampled = IAudioSamples.make(48000, audioDecoder.getChannels());

    //we always have 32 bytes/sample
    int pos = 0;
    int audioFrameLength = audioFrame.length;
    int audioFrameCnt = 1;
    iBuffer = IBuffer.make(null, audioFrame, 0, audioFrameLength);
    IPacket packet = IPacket.make(iBuffer);
    //packet.setKeyPacket(true);
    packet.setTimeBase(IRational.make(1,1000));
    packet.setDuration(20);
    packet.setDts(audioFrameCnt*20);
    packet.setPts(audioFrameCnt*20);
    packet.setStreamIndex(1);
    packet.setPosition(lastPos);
    lastPos+=audioFrameLength;
    int pksz = packet.getSize();
    packet.setComplete(true, pksz);
    /*
    * A packet can actually contain multiple samples
    */
    int offset = 0;
    int retVal;
    while(offset < packet.getSize())
    {
        int bytesDecoded = audioDecoder.decodeAudio(audioSamples, packet, offset);
        if (bytesDecoded < 0)
            throw new RuntimeException("got error decoding audio ");
        offset += bytesDecoded;
        if (audioSamples.isComplete())
        {
            int samplesConsumed = 0;
            while (samplesConsumed < audioSamples.getNumSamples()) {
                retVal = outAudioCoder.encodeAudio(packet_out, audioSamples, samplesConsumed);
                if (retVal <= 0)
                    throw new RuntimeException("Could not encode audio");
                samplesConsumed += retVal;
                if (packet_out.isComplete()) {
                    packet_out.setPosition(lastPos_out);
                    packet_out.setStreamIndex(1);
                    lastPos_out+=packet_out.getSize();
                    retVal = outContainer.writePacket(packet_out);
                    if(retVal < 0){
                        throw new RuntimeException("Could not write data packet");
                    }
                }
            }
        }

    }

}

I get an output file but it doesnt get played. I have very little experience of audio encoding and sampling. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:33:23+00:00Added an answer on June 16, 2026 at 3:33 am

    Finally I am able to live transcode a pcm stream to mp3 stream.

    There were couple of issues in the code :

    • I was trying to transcode only one thing i.e audio and the code snippet was transcoding audio as well as video so there was an issue in setting stream index.

      packet_out.setStreamIndex(1); packet_out.setStreamIndex(0)

    • Second thing was calculations ffmpeg guid

      channel * bits * sampling rate = bit rate
      

      This thing was miscalculated at my end.

    • Number of samples in audio samples depends upon your sampling rate. That was wrong in my code.

    NOTE : this is a pretty old code

     byte[] data = new byte[418];
    
    public void encode(byte[] audioFrame) {
        IPacket packet_out = IPacket.make();
        int lastPos_out = 0;
    
    
        IAudioSamples audioSamples = IAudioSamples.make(11025, audioDecoder.getChannels());
        //IAudioSamples audioSamples_resampled = IAudioSamples.make(48000, audioDecoder.getChannels());
    
        //we always have 32 bytes/sample
        int pos = 0;
        int audioFrameLength = audioFrame.length;
        int audioFrameCnt = 1;
        iBuffer = IBuffer.make(null, audioFrame, 0, audioFrameLength);
        IPacket packet = IPacket.make(iBuffer);
        //packet.setKeyPacket(true);
        packet.setTimeBase(IRational.make(1, 1000));
        packet.setStreamIndex(0);
        int pksz = packet.getSize();
        packet.setComplete(true, pksz);
       /*
        * A packet can actually contain multiple samples
        */
        int offset = 0;
        int retVal;
        while (offset < packet.getSize()) {
            int bytesDecoded = audioDecoder.decodeAudio(audioSamples, packet, offset);
            if (bytesDecoded < 0)
                throw new RuntimeException("got error decoding audio ");
            offset += bytesDecoded;
            if (audioSamples.isComplete()) {
                /*audioResampler.resample(audioSamples_resampled, audioSamples, audioSamples.getNumSamples());
                audioSamples_resampled.setPts(Global.NO_PTS);*/
                int samplesConsumed = 0;
                while (samplesConsumed < audioSamples.getNumSamples()) {
                    retVal = outAudioCoder.encodeAudio(packet_out, audioSamples, samplesConsumed);
                    if (retVal <= 0)
                        throw new RuntimeException("Could not encode audio");
                    samplesConsumed += retVal;
                    if (packet_out.isComplete()) {
                        packet_out.setPosition(lastPos_out);
                        packet_out.setStreamIndex(0);
                        lastPos_out += packet_out.getSize();
                        System.out.println("size" + packet_out.getSize());
                        packet_out.getByteBuffer().get(data,0,packet_out.getSize());
                        try {
                            fo.write(data);
                        } catch (IOException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                        packet_out.reset();
                        //retVal = outContainer.writePacket(packet_out);
                        if (retVal < 0) {
                            throw new RuntimeException("Could not write data packet");
                        }
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert a PCM 32-bit float audio stream to ALAC. I found
I'm trying to convert some video file containing video , audio and subtitles streams
I'm trying to convert an audio file in PHP using ffmpeg, I get the
I'm trying to convert an .avi file to .flv using ffmpeg-php on a centos
I am trying to convert a DVD to mkv file with gstreamer. The pipeline
I'm trying to convert an image into an audio signal in MATLAB by treating
I have an XML file and I'm trying convert it into a (table( HTML
I am trying to create an MP3 audio file on the device (iPhone). I
I have a *.mp4 video file(MPEG4 video codec) and I am trying to convert
I'm trying to find a Python library that would take an audio file (e.g.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.