Как сохранить AVPacket, если у меня есть входная информация с онлайн-камеры - PullRequest
0 голосов
/ 31 марта 2020

Я новичок в libav. У меня есть видеокамера онлайн, и я хочу сохранить видео из архива в видеофайл с помощью libav

Камера предоставляет такие данные

uint32_t frameType, // I frame or P frame

void *frame, //pointer to the frame

size_t frameSize, //size of the frame in bytes

uint64_t timeStamp, //time stamp in time_t units

uint32_t width, //frame width

uint32_t height, //frame heigh

uint32_t genTime, //I do not now what is this. allways 0

const char *encodingType //H264 or H265

Я пробовал

void writeHeader(){
mOutputFilePath = outputFilePath;
    int ret = 0;
    avformat_alloc_output_context2(&output_format_context, nullptr, nullptr, outputFilePath.c_str());

AVStream *out_stream;
        out_stream = avformat_new_stream(output_format_context, nullptr);

        out_stream->discard = AVDISCARD_DEFAULT;//не змінювати
        out_stream->codecpar->level = 42;//не змінювати
        out_stream->codecpar->profile = FF_PROFILE_H264_HIGH;//не змінювати
        out_stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;

        if(codecID == "H264") out_stream->codecpar->codec_id = AV_CODEC_ID_H264;
        else if(codecID == "H265") out_stream->codecpar->codec_id = AV_CODEC_ID_H265;

        out_stream->codecpar->format = AV_PIX_FMT_YUV420P;
        out_stream->codecpar->height = heightFrame;
        out_stream->codecpar->width = widthFrame;
        //        out_stream->codecpar->bit_rate = 2478235;
        //        out_stream->codecpar->bits_per_coded_sample = 24;
        //        out_stream->codecpar->bits_per_raw_sample = 8;
        out_stream->codecpar->sample_aspect_ratio.num = 0;
        out_stream->codecpar->sample_aspect_ratio.den = 1;
        out_stream->codecpar->color_primaries = AVCOL_PRI_UNSPECIFIED;//не змінювати

avio_open(&output_format_context->pb, mOutputFilePath.c_str(), AVIO_FLAG_WRITE);
avformat_write_header(output_format_context, &opt);
}

void writePacket(){
 AVPacket inputPacket;
        av_init_packet(&inputPacket);
        inputPacket.buf = NULL;
        inputPacket.pts = (int)timeStamp;
        inputPacket.dts = inputPacket.pts; 
        inputPacket.data = (unsigned char*)frame;
        inputPacket.size = (int)frameSize;

        if (frameType == KP2P_FRAME_TYPE_IFRAME)
        {
            inputPacket.flags = AV_PKT_FLAG_KEY;
        }
        inputPacket.duration = 0;
        inputPacket.pos = -1;
av_interleaved_write_frame(output_format_context, &inputPacket);
    av_packet_unref(&inputPacket);
}

void closeFile()
{
av_write_trailer(output_format_context);
    if (output_format_context && !(output_format_context->oformat->flags & AVFMT_NOFILE))
        avio_closep(&output_format_context->pb);
    avformat_free_context(output_format_context);
}

в выходной файл У меня черное окно, а время неверное (введите 30 секунд из 2 секунд) Что я делаю не так?

...