video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-24 93f44a10e2e8942e57e62bb210a2ca7d206a51b7
csrc/buz/recorder.cpp
@@ -71,17 +71,10 @@
            }
            out_ = new FormatOut(in_->getFPS(), "mp4");
            AVStream *vs = in_->getStream(0);
            AVStream *as = in_->getStream(1);
            if (!vs) return -1;
            if (audio && !as) logIt("there is no audio");
            
            std::vector<AVStream*> in;
            in.push_back(vs);
            if (audio && as) in.push_back(as);
            file_path_ = dir_ + "/" + sole::uuid4().base62() + ".mp4";
            auto ret = out_->JustWriter(in, file_path_.c_str());
            auto ret = out_->JustWriter(in_->getFromatContext(), file_path_.c_str());
            if (ret){
                return 0;
            }
@@ -99,17 +92,23 @@
                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++;
            }
            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;
@@ -232,15 +231,8 @@
            if(id_frame_ == -1){
                //wait I 
                if (!audio_ && in_->isAudioPkt(pkt.data->getAVPacket())){
                        return 0;
                }
                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();
@@ -263,15 +255,8 @@
            std::lock_guard<std::mutex> locker(mutex_pkt_);
            bool i = false;
            for (auto &p : lst){
                if (!i){
                    if (!audio_ && in_->isAudioPkt(p.data->getAVPacket())){
                        continue;
                    }
                    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);
@@ -288,9 +273,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;