'Ffmpeg uses libavfilter to transcode FLAC. There is no time display, but it can be played

This is my code. This is the decoder setting:

while (avcodec_receive_frame(m_pCodecCtx, m_pFrame) >= 0)
{
   if (m_audioEncoder) {
      m_audioEncoder->encodeAudio(m_pFrame, start_time);
   }
}

This is the encoder setting:

m_pCodecCtx->codec_id = m_pOutFmt->audio_codec;
m_pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
m_pCodecCtx->sample_fmt = m_pCodec->sample_fmts[0];
m_pCodecCtx->sample_rate = m_pInStream->codec->sample_rate;
m_pCodecCtx->channel_layout = m_pInStream->codec->channel_layout;
m_pCodecCtx->channels = av_get_channel_layout_nb_channels(m_pCodecCtx->channel_layout);
m_pCodecCtx->bit_rate = m_pInStream->codec->bit_rate;
m_pCodecCtx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;

This is the filter setting:

const AVFilter *buffsink = avfilter_get_by_name("abuffersink");
        const AVFilter *buffsrc = avfilter_get_by_name("abuffer");
...
av_opt_set_bin(m_buffsinkCtx, "sample_fmts",
            (uint8_t*)&outCtx->sample_fmt, sizeof(outCtx->sample_fmt),
            AV_OPT_SEARCH_CHILDREN);
av_opt_set_bin(m_buffsinkCtx, "channel_layouts",
            (uint8_t*)&outCtx->channel_layout, sizeof(outCtx->channel_layout),
            AV_OPT_SEARCH_CHILDREN);
av_opt_set_bin(m_buffsinkCtx, "sample_rates",
            (uint8_t*)&outCtx->sample_rate, sizeof(outCtx->sample_rate),
            AV_OPT_SEARCH_CHILDREN);

Set PTS and write frame:

pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, m_pStream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
            pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, m_pStream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
            pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, m_pCodecCtx->time_base);
pkt.pos = -1;
av_interleaved_write_frame(m_pFormatCtx, &pkt);

It can be transcoded, but the duration cannot be displayed correctly.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source