video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2020-07-24 f93ee1a42e8c47e472332287b7350b66a6b0fa11
csrc/buz/recorder.cpp
@@ -5,6 +5,8 @@
#include <unistd.h>
#include <chrono>
#include <sys/stat.h>
extern "C"{
#include <libavcodec/avcodec.h>
}
@@ -82,6 +84,12 @@
            std::string filename(sole::uuid4().base62() + "-" + std::to_string(pid) + ".mp4");
            file_path_ = dir_ + "/" + filename;
            std::string backup_dir("./video");
            size_t pos = dir_.rfind("/");
            if (pos != std::string::npos){
                backup_dir = dir_.substr(0, pos);
            }
            auto v = in_->getStream(AVMEDIA_TYPE_VIDEO);
            if (!v){
                return -2;
@@ -95,9 +103,11 @@
                logIt("start record h264 file: %s", file_path_.c_str());                
                return 0;
            }else{
                file_path_ = "./" + filename;
                ret = out_->JustWriter(v, a, file_path_.c_str());
                mkdir(backup_dir.c_str(), 0777);
                file_path_ = backup_dir + "/" + filename;
                logIt("failed in dir %s, try file %s to start record file", dir_.c_str(), file_path_.c_str());                
                ret = out_->JustWriter(v, a, file_path_.c_str());
                if (ret){
                    logIt("start record h264 file: %s", file_path_.c_str());                
                    return 0;
@@ -118,9 +128,17 @@
            std::string filename(sole::uuid4().base62() + "-" + std::to_string(pid) + ".hevc");
            file_path_ = dir_ + "/" + filename;
            std::string backup_dir("./video");
            size_t pos = dir_.rfind("/");
            if (pos != std::string::npos){
                backup_dir = dir_.substr(0, pos);
            }
            fp_ = fopen(file_path_.c_str(), "wb");
            if (!fp_){
                file_path_ = "./" + filename;
                mkdir(backup_dir.c_str(), 0777);
                file_path_ = backup_dir + "/" + filename;
                logIt("failed in dir %s, try file %s to start record hevc file", dir_.c_str(), file_path_.c_str());                
                fp_ = fopen(file_path_.c_str(), "wb");
                if (!fp_){
@@ -216,8 +234,6 @@
                v_cur_frame_++;
            }
            fwrite(op.data, op.size, 1, fp_); 
            logIt("hevc write data len: %d frame id: %d key %d",
                    op.size, v_cur_frame_, op.flags & AV_PKT_FLAG_KEY);
            return 0;
        }