title: how do i create a stereo mp3 file with latest version of ffmpeg?
body: <p>I'm updating my code from the older version of ffmpeg (53) to the newer (54/55). Code that did work has now been deprecated or removed so i'm having problems updating it.</p>
  
  <p>Previously I could create a stereo MP3 file using a sample format called:</p>
  
  <p>SAMPLE_FMT_S16</p>
  
  <p>That matched up perfectly with my source stream. This has now been replace with </p>
  
  <p>AV_SAMPLE_FMT_S16</p>
  
  <p>Which works fine for mono recordings but when I try to create a stereo MP3 file it bugs out at avcodec_open2 with:</p>
  
  <p>"Specified sample_fmt is not supported."</p>
  
  <p>Through trial and error I've found that using</p>
  
  <p>AV_SAMPLE_FMT_S16P</p>
  
  <p>...is accepted by avcodec_open2 but when I get through and create the MP3 file the sound is very distorted - it sounds about 2 octaves lower than usual with a massive hum in the background  - here's an example recording:</p>
  
  <p><a href="http://hosting.ispyconnect.com/example.mp3" rel="nofollow">http://hosting.ispyconnect.com/example.mp3</a> </p>
  
  <p>I've been told by the ffmpeg guys that this is because I now need to manually deinterleave my byte stream before calling:</p>
  
  <p>avcodec_fill_audio_frame</p>
  
  <p>How do I do that? I've tried using the swrescale library without success and i've tried manually feeding in L/R data into avcodec_fill_audio_frame but the results i'm getting are sounding exactly the same as without interleaving.</p>
  
  <p>Here is my code for encoding:</p>
  
  <pre><code>void add_audio_sample( AudioWriterPrivateData^ data, BYTE* soundBuffer, int soundBufferSize)
  {
      libffmpeg::AVCodecContext* c = data-&gt;AudioStream-&gt;codec;
      memcpy(data-&gt;AudioBuffer + data-&gt;AudioBufferSizeCurrent,  soundBuffer, soundBufferSize);
      data-&gt;AudioBufferSizeCurrent += soundBufferSize;
      uint8_t* pSoundBuffer = (uint8_t *)data-&gt;AudioBuffer;
      DWORD nCurrentSize    = data-&gt;AudioBufferSizeCurrent;
  
      libffmpeg::AVFrame *frame;
  
      int got_packet;
      int ret;
      int size = libffmpeg::av_samples_get_buffer_size(NULL, c-&gt;channels,
                                                data-&gt;AudioInputSampleSize,
                                                c-&gt;sample_fmt, 1);
  
      while( nCurrentSize &gt;= size)    {
  
          frame=libffmpeg::avcodec_alloc_frame();
          libffmpeg::avcodec_get_frame_defaults(frame);
  
          frame-&gt;nb_samples = data-&gt;AudioInputSampleSize;
  
          ret = libffmpeg::avcodec_fill_audio_frame(frame, c-&gt;channels, c-&gt;sample_fmt, pSoundBuffer, size, 1);
          if (ret&lt;0)
          {
              throw gcnew System::IO::IOException("error filling audio");
          }
          //audio_pts = (double)audio_st-&gt;pts.val * audio_st-&gt;time_base.num / audio_st-&gt;time_base.den;
  
          libffmpeg::AVPacket pkt = { 0 };
          libffmpeg::av_init_packet(&amp;pkt);
  
          ret = libffmpeg::avcodec_encode_audio2(c, &amp;pkt, frame, &amp;got_packet);
  
          if (ret&lt;0)
                  throw gcnew System::IO::IOException("error encoding audio");
          if (got_packet) {
              pkt.stream_index = data-&gt;AudioStream-&gt;index;
  
              if (pkt.pts != AV_NOPTS_VALUE)
                  pkt.pts = libffmpeg::av_rescale_q(pkt.pts, c-&gt;time_base, c-&gt;time_base);
              if (pkt.duration &gt; 0)
                  pkt.duration = av_rescale_q(pkt.duration, c-&gt;time_base, c-&gt;time_base);
  
              pkt.flags |= AV_PKT_FLAG_KEY;
  
              if (libffmpeg::av_interleaved_write_frame(data-&gt;FormatContext, &amp;pkt) != 0)
                      throw gcnew System::IO::IOException("unable to write audio frame.");
  
  
          }
          nCurrentSize -= size;  
          pSoundBuffer += size;   
      }
      memcpy(data-&gt;AudioBuffer, data-&gt;AudioBuffer + data-&gt;AudioBufferSizeCurrent - nCurrentSize, nCurrentSize);
      data-&gt;AudioBufferSizeCurrent = nCurrentSize; 
  
  }
  </code></pre>
  
  <p>Would love to hear any ideas - I've been trying to get this working for 3 days now :(</p>