| | |
| | | |
| | | out_ = new FormatOut(in_->getFPS(), "mp4"); |
| | | |
| | | |
| | | file_path_ = dir_ + "/" + sole::uuid4().base62() + ".mp4"; |
| | | auto ret = out_->JustWriter(in_->getFromatContext(), file_path_.c_str()); |
| | | int pid = getpid(); |
| | | file_path_ = dir_ + "/" + sole::uuid4().base62() + "-" + std::to_string(pid) + ".mp4"; |
| | | auto ret = out_->JustWriter(in_->allStreams(), 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; |
| | | } |
| | | |
| | |
| | | return false; |
| | | } |
| | | |
| | | logIt("there are %d stream", ctx_->nb_streams); |
| | | // logIt("there are %d stream", ctx_->nb_streams); |
| | | |
| | | for (int i = 0; i < ctx_->nb_streams; ++i) |
| | | { |
| | | auto type = ctx_->streams[i]->codecpar->codec_type; |
| | | logIt("there are %d stream, stream %d, type %d", ctx_->nb_streams, i, type); |
| | | |
| | | if (type == AVMEDIA_TYPE_VIDEO){ |
| | | vs_idx_ = i; |
| | | |
| | |
| | | }else if(in->r_frame_rate.num >=1 && in->r_frame_rate.den >= 1){ |
| | | fps_ = av_q2d(in->r_frame_rate); |
| | | } |
| | | logIt("in stream fps %f, time_base: %d : %d", fps_, in->time_base.num, in->time_base.den); |
| | | logIt("in stream video fps %f, time_base: %d : %d", fps_, in->time_base.num, in->time_base.den); |
| | | } |
| | | if (type == AVMEDIA_TYPE_AUDIO){ |
| | | as_idx_ = i; |
| | | auto in = ctx_->streams[i]; |
| | | logIt("in stream audio %d time_base: %d : %d", in->codecpar->codec_id, in->time_base.num, in->time_base.den); |
| | | if (in->codecpar->codec_id == AV_CODEC_ID_AAC) |
| | | as_idx_ = i; |
| | | else |
| | | logIt("record not support audio codec: %d", in->codecpar->codec_id); |
| | | } |
| | | } |
| | | |
| | |
| | | continue; |
| | | }else{ |
| | | av_dict_set(&avdic, "gpu", std::to_string(idle_gpu).c_str(), 0); |
| | | // av_dict_set(&avdic, "gpu", std::to_string(2).c_str(), 0); |
| | | } |
| | | }else{ |
| | | dec = avcodec_find_decoder(codecpar->codec_id); |
| | |
| | | return NULL; |
| | | } |
| | | |
| | | std::vector<AVStream*> FormatIn::allStreams(){ |
| | | std::vector<AVStream*> vec; |
| | | auto v = getStream(AVMEDIA_TYPE_VIDEO); |
| | | if (v){ |
| | | vec.push_back(v); |
| | | } |
| | | auto a = getStream(AVMEDIA_TYPE_AUDIO); |
| | | if (a){ |
| | | vec.push_back(a); |
| | | } |
| | | return vec; |
| | | } |
| | | |
| | | AVCodecContext *FormatIn::getCodecContext(int type){ |
| | | return dec_ctx_; |
| | | } |
| | |
| | | return false; |
| | | } |
| | | |
| | | bool FormatIn::notVideoAudio(AVPacket *pkt){ |
| | | return !isVideoPkt(pkt) && !isAudioPkt(pkt); |
| | | } |
| | | |
| | | int FormatIn::readPacket(AVPacket *pkt_out){ |
| | | |
| | | return av_read_frame(ctx_, pkt_out); |
| | |
| | | |
| | | #include <stdint.h> |
| | | #include <memory> |
| | | #include <vector> |
| | | #include "PsToEs.hpp" |
| | | |
| | | struct AVFormatContext; |
| | |
| | | |
| | | bool isVideoPkt(AVPacket *pkt); |
| | | bool isAudioPkt(AVPacket *pkt); |
| | | bool notVideoAudio(AVPacket *pkt); |
| | | private: |
| | | bool allocCodec(AVCodec *dec, AVStream *s, AVDictionary **options); |
| | | public: |
| | | AVStream *getStream(int type = -1); |
| | | std::vector<AVStream*> allStreams(); |
| | | AVCodecContext *getCodecContext(int type = 0); |
| | | AVFormatContext *getFromatContext(){return ctx_;} |
| | | const double getFPS()const{return fps_;} |
| | |
| | | return true; |
| | | } |
| | | |
| | | bool FormatOut::copyCodecFromIn(AVFormatContext* in){ |
| | | bool FormatOut::copyCodecFromIn(std::vector<AVStream*> in){ |
| | | |
| | | for(int i = 0; i < in->nb_streams; i++) |
| | | { //根据输入流创建输出流 |
| | | AVStream *in_stream = in->streams[i]; |
| | | for (int i = 0; i < in.size(); i++){ |
| | | |
| | | AVStream *in_stream = in[i]; |
| | | |
| | | AVStream *out_stream = avformat_new_stream(ctx_, in_stream->codec->codec); |
| | | if(!out_stream) |
| | | { |
| | |
| | | |
| | | if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){ |
| | | v_idx_ = i; |
| | | }else{ |
| | | a_idx_ = i; |
| | | } |
| | | logIt("copy video from instream"); |
| | | |
| | | out_stream->codecpar->codec_tag = out_stream->codec->codec_tag = 0; |
| | | out_stream->codecpar->codec_tag = out_stream->codec->codec_tag = 0; |
| | | |
| | | if(ctx_->oformat->flags & AVFMT_GLOBALHEADER) |
| | | out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
| | | if(ctx_->oformat->flags & AVFMT_GLOBALHEADER) |
| | | out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
| | | } |
| | | if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO){ |
| | | logIt("copy audio from instream"); |
| | | a_idx_ = i; |
| | | |
| | | out_stream->codecpar->codec_tag = out_stream->codec->codec_tag = 0; |
| | | |
| | | if(ctx_->oformat->flags & AVFMT_GLOBALHEADER) |
| | | out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; |
| | | |
| | | } |
| | | } |
| | | |
| | | in_ctx_ = in; |
| | | in_streams_ = in; |
| | | |
| | | return true; |
| | | } |
| | | |
| | | bool FormatOut::JustWriter(AVFormatContext *in, const char *filename){ |
| | | bool FormatOut::JustWriter(std::vector<AVStream*> in, const char *filename){ |
| | | if(ctx_){ |
| | | clear(); |
| | | } |
| | |
| | | av_dict_set(&avdic,option_key,option_value,0); |
| | | flag = writeHeader(&avdic); |
| | | av_dict_free(&avdic); |
| | | |
| | | } |
| | | |
| | | return flag; |
| | |
| | | } |
| | | |
| | | AVStream *in_stream,*out_stream; |
| | | int out_idx = -1; |
| | | for (auto i : in_streams_){ |
| | | if (i->index == pkt->stream_index){ |
| | | if (i->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){ |
| | | out_idx = v_idx_; |
| | | in_stream = i; |
| | | break; |
| | | }else if (i->codecpar->codec_type == AVMEDIA_TYPE_AUDIO){ |
| | | in_stream = i; |
| | | out_idx = a_idx_; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | if (out_idx == -1) return; |
| | | |
| | | out_stream = ctx_->streams[out_idx]; |
| | | pkt->stream_index = out_idx; |
| | | |
| | | in_stream = in_ctx_->streams[pkt->stream_index]; |
| | | out_stream = ctx_->streams[pkt->stream_index]; |
| | | |
| | | // logIt("BEFORE in stream timebase %d:%d, out timebase %d:%d, \ |
| | | // logIt("BEFORE in stream timebase %d:%d, out timebase %d:%d, |
| | | // pts: %lld, dts: %lld, duration: %lld", |
| | | // in_stream->time_base.num, in_stream->time_base.den, |
| | | // out_stream->time_base.num, out_stream->time_base.den, |
| | |
| | | int encode(AVPacket *pkt, AVFrame *frame); |
| | | |
| | | public: |
| | | bool copyCodecFromIn(AVFormatContext* in); |
| | | bool copyCodecFromIn(std::vector<AVStream*> in); |
| | | bool openResource(const char *filename, const int flags); |
| | | bool closeResource(); |
| | | |
| | | bool JustWriter(AVFormatContext* in, const char *filename); |
| | | bool JustWriter(std::vector<AVStream*> in, const char *filename); |
| | | bool EncodeWriter(const char *filename); |
| | | bool writeFrame(AVPacket *pkt, const int64_t &frame_cnt, bool interleaved = true); |
| | | void adjustPTS(AVPacket *pkt, const int64_t &frame_cnt); |
| | |
| | | std::string format_name_; |
| | | |
| | | // rec |
| | | AVFormatContext *in_ctx_; |
| | | std::vector<AVStream*> in_streams_; |
| | | }; |
| | | } |
| | | #endif |
| | |
| | | std::string lc(temp); |
| | | lc = "LIB-libcffmpeg.so-> " + lc; |
| | | spdlog::get(log_name)->error(lc); |
| | | spdlog::get(log_name)->flush(); |
| | | }else{ |
| | | printf("%s\n", temp); |
| | | } |
| | |
| | | int decoder::SetFrame(std::shared_ptr<ffwrapper::CodedData> data, int64_t &id){ |
| | | |
| | | if (!data) return -1; |
| | | if (decRef_->isAudioPkt(&data->getAVPacket())) return -2; |
| | | if (!decRef_->isVideoPkt(&data->getAVPacket())) return -2; |
| | | |
| | | if (!conv_){ |
| | | if (initDecoder() != 0) return -3; |
| | |
| | | break; |
| | | } |
| | | |
| | | if (in->notVideoAudio(&data->getAVPacket())){ |
| | | continue; |
| | | } |
| | | |
| | | if (!gb_ && id < 0){ |
| | | id++; |
| | | continue; |