video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2020-10-09 a8b447be656145c9ba2a2d8319a10ae8f726de1f
csrc/ffmpeg/format/FormatOut.cpp
@@ -33,16 +33,12 @@
    ,format_name_("mp4")
    ,in_v_stream_(NULL)
    ,in_a_stream_(NULL)
    ,bsf_h264(NULL)
    ,bsf_hevc(NULL)
   {}
   FormatOut::~FormatOut()
   {
      clear();
        if (bsf_h264) av_bsf_free(&bsf_h264);
        if (bsf_hevc) av_bsf_free(&bsf_hevc);
   }
    void FormatOut::clear(){
@@ -355,22 +351,6 @@
            av_dict_free(&avdic);
        }
        if (v){
            if (v->codecpar->codec_id == AV_CODEC_ID_H264){
                char p[100] = {0};
                char *sub = av_fourcc_make_string(p, v->codecpar->codec_tag);
                if (strcmp(sub, "avc1") == 0){
                    const AVBitStreamFilter *f = av_bsf_get_by_name("h264_mp4toannexb");
                    if (f){
                        if (av_bsf_alloc(f, &bsf_h264) >= 0){
                            if (avcodec_parameters_copy(bsf_h264->par_in, v->codecpar) >= 0){
                                if (av_bsf_init(bsf_h264) < 0) bsf_h264 = NULL;
                            }
                        }
                    }
                }
            }
        }
        return flag;
    }
    
@@ -467,7 +447,7 @@
                              bool interleaved/* = true*/){
        adjustPTS(pkt, frame_cnt);
        auto ret = writeFrame2(pkt, interleaved);
        auto ret = writeFrameInternal(pkt, interleaved);
        if (!ret){
            logIt("write to file failed, pkt.pts: %lld, dts: %lld, frame count: %d",
                    pkt->pts, pkt->dts, frame_cnt);
@@ -475,29 +455,15 @@
        return ret;
    }
    bool FormatOut::writeFrame2(AVPacket *pkt, bool interleaved){
        // h264_mp4toanatax
        if (bsf_h264){
            if (av_bsf_send_packet(bsf_h264, pkt) < 0){
                logIt("bsf_h264 send packet failed");
                return true;
            }
            if (av_bsf_receive_packet(bsf_h264, pkt) < 0){
                logIt("bsf_h264 recv packet failed");
                return true;
            }
        }
        //
    static bool write_frame(AVFormatContext *ctx, AVPacket *pkt, bool interleaved){
        int ret = 0;
        if(interleaved){
            ret = av_interleaved_write_frame(ctx_, pkt);
            ret = av_interleaved_write_frame(ctx, pkt);
        }else{
            // returns 1 if flushed and there is no more data to flush
            ret = av_write_frame(ctx_, pkt);
            ret = av_write_frame(ctx, pkt);
        }
        if(ret < -22 || ret == 0){
            return true;
        }
@@ -505,6 +471,11 @@
        return false;
    }
    bool FormatOut::writeFrameInternal(AVPacket *pkt, bool interleaved){
        return write_frame(ctx_, pkt, interleaved);
    }
    bool FormatOut::writeTrailer(){
        const int ret = av_write_trailer(ctx_);
        if(ret != 0)