[Libav-user] Muxing Problem

Kevin J. Brooks kbrooks at r2c-ss.com
Wed Sep 23 17:27:49 CEST 2015


Hi All,

Here is my code for encoding Audio.  Can anyone help and point me in the 
direction I need to fix this issue?

     AVFrame* frame;
     AVPacket pkt = { 0 };
     AVCodecContext *c = m_audio_st.st->codec;
     int got_packet;
     int dst_nb_samples;
     int bytesReady = audioBuffer.size();
     QByteArray samples;
     int sampleSize;

     if(m_errorCode != NoError){
         return;
     }

     frame = m_audio_st.tmp_frame;
     if(m_audio_st.swr_ctx == NULL || c == NULL
        || frame == NULL){
         return;
     }

     sampleSize = av_samples_get_buffer_size(NULL, c->channels,
                                             c->frame_size,
                                             c->sample_fmt, 0);

     do{
         samples.clear();

         if(m_audioBuffer.size() + bytesReady == m_sampleSize){
             samples.append(m_audioBuffer + audioBuffer);
             m_audioBuffer.clear();
         }
         else if(m_audioBuffer.size() + bytesReady > m_sampleSize){
             m_audioBuffer.append(audioBuffer);
             samples.append(m_audioBuffer.left(m_sampleSize));
             m_audioBuffer.remove(0, m_sampleSize);
         }
         else if(m_audioBuffer.size() + bytesReady < m_sampleSize){
             m_audioBuffer.append(audioBuffer);
             return;
         }

         bytesReady = 0;
         audioBuffer.clear();
         int ret;
         if((ret = avcodec_fill_audio_frame(frame, c->channels,
                                  c->sample_fmt,
                                  (uint8_t*) samples.data(),
                                  sampleSize, 0)) < 0){
             m_errorCode = AudioEncoderError;
             Q_EMIT(m_errorCode);
             continue;
         }
         frame->pts = m_audio_st.next_pts;
         m_audio_st.next_pts += frame->nb_samples;
         dst_nb_samples = 
av_rescale_rnd(swr_get_delay(m_audio_st.swr_ctx, c->sample_rate) + 
frame->nb_samples,
                                         c->sample_rate, c->sample_rate, 
AV_ROUND_UP);
         av_assert0(dst_nb_samples == frame->nb_samples);

         ret = av_frame_make_writable(m_audio_st.frame);
         if(ret < 0){
             m_errorCode = AudioEncoderError;
             Q_EMIT(m_errorCode);
             continue;
         }

         ret = swr_convert(m_audio_st.swr_ctx,
                           m_audio_st.frame->data, dst_nb_samples,
                           (const uint8_t **)frame->data, 
frame->nb_samples);
         if(ret < 0){
             m_errorCode = AudioEncoderError;
             Q_EMIT(m_errorCode);
             return;
         }


         frame = m_audio_st.frame;
         frame->pts = av_rescale_q(m_audio_st.samples_count, {1, 
c->sample_rate}, c->time_base);

         m_audio_st.samples_count += dst_nb_samples;
         av_init_packet(&pkt);
         ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
         if(ret < 0){
             m_errorCode = AudioEncoderError;
             Q_EMIT(m_errorCode);
             continue;
         }

         if(got_packet){
             ret = write_frame(m_oc, &c->time_base, m_audio_st.st, &pkt);
             if(ret < 0){
                 m_errorCode = AudioEncoderError;
                 Q_EMIT(m_errorCode);
                 continue;
             }
         }
     }while(m_audioBuffer.size() > m_sampleSize);

Sincerely,
Kevin J. Brooks

On 9/22/2015 8:34 AM, Kevin J. Brooks wrote:
> Hi All,
>
> Obviously I don't understand enough about encoding video with audio to 
> figure out how to fix this.  If I take the captured sound from the 
> microphone and pipe it directly back to my headphones, it is crystal 
> clear.  Well maybe a little tinny sounding, but clear. However, when I 
> try to encode it into the video, it is not synced up with the images 
> and there is a bunch of white noise.  It is only after the video has 
> ended that you finally can barely hear my voice and whistles.
>
> Can anyone give me and idea where to start to fix this problem?
> -- 
> Sincerely,
> Kevin J. Brooks
> Senior Software Engineer
>
>
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20150923/8e240341/attachment.html>


More information about the Libav-user mailing list