0

I am trying to transcode any input audio file to AAC codec (having mp4 output container). For this,I took input file as media.mp4(having opus codec). The trans coding worked but the noise is superficially attached to the stream of output file. However if the audio having aac codec is taken as input file, the output audio stream is completely fine.What am I missing?

I am trying to transcode any input audio file to AAC codec (having mp4 output container). For this,I took input file as media.mp4(having opus codec). The trans coding worked but the noise is superficially attached to the stream of output file. However if the audio having aac codec is taken as input file, the output audio stream is completely fine.What am I missing?

void configureOutputAudioCodec()
{
   
   AVStream *audio_stream=NULL;
   AVCodec *audio_encoder=NULL;
   AVCodecID audio_codec=AV_CODEC_ID_AAC;
   audio_stream = avformat_new_stream(output_ctx, NULL);
   if (!audio_stream)
   {
      cout<<"Failed to create output stream"<<endl;
   }
   audio_stream->id = audio_stream_index;
   audio_encoder = avcodec_find_encoder(audio_codec);
   if (!audio_encoder)
   {
      cout<<"Failed to find audio_encoder for output format"<<endl;
   }
   audio_encoder_ctx = avcodec_alloc_context3(audio_encoder);
   if (!audio_encoder_ctx)
   {
      cout<<"Failed to allocate encoder codec context for audio"<<endl;
   }
   audio_encoder_ctx->codec_type=AVMEDIA_TYPE_AUDIO;
   output_ctx->streams[audio_stream_index]=audio_stream;
   audio_encoder_ctx->sample_rate=audio_decoder_ctx->sample_rate;
   audio_encoder_ctx->sample_fmt=audio_decoder_ctx->sample_fmt;
   audio_encoder_ctx->channels=audio_decoder_ctx->channels;
   audio_encoder_ctx->bit_rate=audio_decoder_ctx->bit_rate;
  audio_encoder_ctx->pkt_timebase=input_ctx->streams[audio_stream_index]->time_base;
   audio_encoder_ctx->channel_layout=audio_decoder_ctx->channel_layout;
   audio_stream->time_base=audio_decoder_ctx->time_base;
   if ((output_ctx)->oformat->flags & AVFMT_GLOBALHEADER)
      audio_encoder_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

   if ((ret = avcodec_open2(audio_encoder_ctx, audio_encoder, NULL)) < 0)
   {
      cout<<"Failed to open codec for audio"<<endl;
   }

   if ((ret = avcodec_parameters_from_context(audio_stream->codecpar, audio_encoder_ctx)) < 0)
   {
      cout<<"Failed to set output codec parameters for audio"<<endl;
   }
}
void encodeAudio()
{
   AVPacket *pkt = av_packet_alloc();
   AVFrame *frame = av_frame_alloc();
   int error=0;
      for (;;)
      {
         int ret = av_read_frame(input_ctx, pkt);
         pos=pkt->pos;
         if (ret >= 0)
         {
            if(pkt->stream_index==audio_stream_index)
            {
               int ret_ = avcodec_send_packet(audio_decoder_ctx, pkt);
               if((pkt->size)>(audio_encoder_ctx->frame_size))
               {
                  cout<<"here"<<endl;
               }
               if (ret_ < 0)
               {
               cout<<error<<"."<<endl;   
               error++;

               }
               else
               {
               }
               if (ret_ >= 0)
               {
                  int ret1 = avcodec_receive_frame(audio_decoder_ctx, frame);
                  if (ret1 >= 0)
                  {
                  writeAudio(audio_encoder_ctx,frame,pkt);
                  }
               }
            }
         }
         if (ret < 0)
         {
            avcodec_send_packet(audio_decoder_ctx, NULL);
            break;
         }
      }
   av_free_packet(pkt);
   av_frame_unref(frame);   
}
void writeAudio(AVCodecContext *encoder_ctx, AVFrame *out_frame,AVPacket *pkt)
      
{
   
   ret=avcodec_send_frame(encoder_ctx,out_frame);
   while(ret>=0)
   {
      ret=avcodec_receive_packet(encoder_ctx,pkt);
      if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
          break;
      else if (ret < 0) 
      {
          cout<<"Error encoding audio frame\n";
          exit(1);
      }
     ret=av_interleaved_write_frame(output_ctx,pkt);
     if(ret<0)
     {
     cout<<"av_write_frame returns:"<<ret<<endl;
     }
   }
}

0 Answers0