srt转rtmp(mpegts -> flv)

一、使用ffmpeg拉流srt转rtmp推流过程中遇到音视频问题

1、音频

    虽然从mpegts到flv都是AAC格式,但是mpegts多了ADTS头,在flvenc的时候会报错误:

    Malformed AAC bitstream detected: use the audio bitstream filter 'aac_adtstoasc' to fix it ('-bsf:a aac_adtstoasc' option with ffmpeg)

    修复方式:添加avstreamfilter:aac_adtstoasc

        const AVBitStreamFilter *filter = NULL;
        filter = av_bsf_get_by_name("aac_adtstoasc");
        if (!filter) {
            TLOGE(m_e2eId.c_str(), "Unknown bitstream filter\n");
            return;
        }
        ret = av_bsf_alloc(filter, &m_bsfAacContext);
        if (ret < 0) {
            TLOGE(m_e2eId.c_str(), "Error allocating a bitstream filter context\n");
            return;
        }

-----------------------------------------------------

        if (stream->codecID == AV_CODEC_ID_AAC && m_bsfAacContext != nullptr) {
                int ret;
                ret = av_bsf_send_packet(m_bsfAacContext, &opkt);
                if (ret < 0) {
                    TLOGE(m_e2eId.c_str(), "av_bsf_send_packet failed");
                    return ret;
                }
                ret = av_bsf_receive_packet(m_bsfAacContext, &opkt);
                if (ret < 0) {
                    TLOGE(m_e2eId.c_str(), "av_bsf_receive_packet failed");
                    return ret;
                }
            }

2、视频

    视频会多出nalu类型为9的AUD,去除方法为使用avstreamfilter:h264_metadata

srt转rtmp(mpegts -> flv)_第1张图片

 

        const AVBitStreamFilter *filter = NULL;
        filter = av_bsf_get_by_name("h264_metadata");
        if (!filter) {
            TLOGE(m_e2eId.c_str(), "Unknown bitstream filter\n");
            return;
        }
        ret = av_bsf_alloc(filter, &m_bsfH264Context);
        if (ret < 0) {
            TLOGE(m_e2eId.c_str(), "Error allocating a bitstream filter context\n");
            return;
        }
        av_opt_set_int(m_bsfH264Context->priv_data, "aud", 2, 0); // 2 REMOVE
-----------------------------------------------------------------
            if (stream->codecID == AV_CODEC_ID_H264 && m_bsfH264Context != nullptr) {
                int ret;
                ret = av_bsf_send_packet(m_bsfH264Context, &opkt);
                if (ret < 0) {
                    TLOGE(m_e2eId.c_str(), "av_bsf_send_packet failed");
                    return ret;
                }
                ret = av_bsf_receive_packet(m_bsfH264Context, &opkt);
                if (ret < 0) {
                    TLOGE(m_e2eId.c_str(), "av_bsf_receive_packet failed");
                    return ret;
                }
            }

你可能感兴趣的:(ffmpeg,ffmpeg)