video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-28 a4cfd08f442fbf6febf06c98bbbed1723aaf5fd4
csrc/buz/recorder.cpp
@@ -34,6 +34,8 @@
        ,func_rec_info_(nullptr)
        ,thrd_(nullptr)
        ,error_occured_(false)
        ,audio_(false)
        ,cur_frame_a(0)
        {
            // logIt("RECODER ID: %s", id_.c_str());
        }
@@ -59,7 +61,7 @@
            
        }
        int Recorder::init_writer(){
        int Recorder::init_writer(const bool audio){
            if (out_) {
                delete out_;
            }
@@ -69,13 +71,26 @@
                return -1;
            }
            out_ = new FormatOut(in_->getStream(), "mp4");
            out_ = new FormatOut(in_->getFPS(), "mp4");
            int pid = getpid();
            file_path_ = dir_ + "/" + sole::uuid4().base62() + "-" + std::to_string(pid) + ".mp4";
            file_path_ = dir_ + "/" + sole::uuid4().base62() + ".mp4";
            auto ret = out_->JustWriter(in_->getStream(), file_path_.c_str());
            auto v = in_->getStream(AVMEDIA_TYPE_VIDEO);
            if (!v){
                return -2;
            }
            AVStream *a = in_->getStream(AVMEDIA_TYPE_AUDIO);
            if (!audio){
                a = NULL;
            }
            auto ret = out_->JustWriter(v, a, file_path_.c_str());
            if (ret){
                logIt("start record file: %s", file_path_.c_str());
                return 0;
            }
            logIt("failed to start record: %s", file_path_.c_str());
            return -1;
        }
@@ -90,17 +105,25 @@
                return 1;
            }
            int64_t cur = cur_frame++;
            AVPacket &op = pkt.data->getAVPacket();
            AVPacket np(op);
            av_copy_packet(&np, &op);
            auto ret = out_->writeFrame(np, cur);
            av_packet_unref(&np);
            if (!ret) return -1;
            
            if(pkt.id == id_frame_){
                id_frame_in_file_ = cur_frame-1;
            int64_t cur = cur_frame;
            if (in_->isVideoPkt(&np)){
                if(pkt.id == id_frame_){
                    id_frame_in_file_ = cur_frame;
                }
                cur_frame++;
            }else if (in_->isAudioPkt(&np)) {
                cur = cur_frame_a++;
            }
            auto ret = out_->writeFrame(&np, cur);
            av_packet_unref(&np);
            if (!ret) return -1;
            
            // logIt("WRITE FRAME ID: %d, RECORD ID: %d", pkt.id, id_frame_);
            return 0;
@@ -162,10 +185,10 @@
            end_writer();
        }
        int Recorder::Run(const char* output, const int mind, const int maxd){
        int Recorder::Run(const char* output, const int mind, const int maxd, const bool audio){
            dir_ = output;
            int ret = init_writer();
            int ret = init_writer(audio);
            if(ret != 0){
                logIt("recorder init writer error");
                return -1;
@@ -177,6 +200,8 @@
                minduration = fps * mind;
                end_frame = minduration;
            }
            audio_ = audio;
            // logIt("minduration %d maxduration %d curduration %d", minduration, maxduration, end_frame);    
@@ -221,11 +246,8 @@
            if(id_frame_ == -1){
                //wait I 
                if (list_pkt_.empty()) {
                    AVPacket &avpkt = pkt.data->getAVPacket();
                    if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
                        return -1;
                    }
                if (!audio_ && in_->isAudioPkt(&pkt.data->getAVPacket())){
                    return 0;
                }
                maybe_dump_gop();
@@ -248,12 +270,8 @@
            std::lock_guard<std::mutex> locker(mutex_pkt_);
            bool i = false;
            for (auto &p : lst){
                if (!i){
                    AVPacket &avpkt = p.data->getAVPacket();
                    if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
                        continue;
                    }
                    i = true;
                if (!audio_ && in_->isAudioPkt(&p.data->getAVPacket())){
                    continue;
                }
                
                list_pkt_.push_back(p);
@@ -270,9 +288,8 @@
            while (list_pkt_.size() > minduration) {
                list_pkt_.pop_front();
                while(!list_pkt_.empty()){
                    auto &cache = list_pkt_.front();
                    AVPacket &avpkt = cache.data->getAVPacket();
                    if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
                    auto &i = list_pkt_.front();
                    if (!(i.data->getAVPacket().flags & AV_PKT_FLAG_KEY)){
                        list_pkt_.pop_front();
                    }else{
                        break;