0

I am new to libav. I have a online video camera and want save video from archive to the video file with libav

Camera provides such data

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

I tried this

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);
}

in output file I have black vindow and time is not correct (input 30 seconds in out 2 seconds) What am I doing wrong?

szatmary
  • 29,969
  • 8
  • 44
  • 57
Orest
  • 43
  • 1
  • 7
  • Does the frame data have start codes? Is the sps/pps inband? Did you try to just write an annexb .264 stream (without using libav) and see if that plays? – szatmary Mar 31 '20 at 15:52
  • @szatmary Does the frame data have start codes? - first frame is the I frame, if I understood the question correctly Is the sps/pps inband? - no Did you try to just write an annexb .264 stream (without using libav) and see if that plays? - there is no intup stream. – Orest Mar 31 '20 at 17:30
  • Yes. read this: https://stackoverflow.com/questions/24884827/possible-locations-for-sequence-picture-parameter-sets-for-h-264-stream – szatmary Mar 31 '20 at 17:32
  • `there is no intup stream` Yes there is. The sequence of frames IS the stream. If they are annex B, you can just write them to a .264 file and play it (for testing purposes). If the SPS/PPS is not in band, where are you getting them from? – szatmary Mar 31 '20 at 18:07
  • @szatmary How can i do this? – Orest Mar 31 '20 at 19:55

0 Answers0