Hello,

I have made some progress until my last mail 3 hours ago. Now I write
the video but all my frames are on each others. It's like the video is
very very accelerated I don't understand why ?. So here is my code if
anyone has an idea.


Thank you for your help,

Best regards,

DELMAS P.


Le 16/04/2017 à 13:17, Pierre Delmas a écrit :
> non-strictly-monotonic PTS

int Video::writeVideo(string nameVideo)
{
    AVOutputFormat *oformat = av_guess_format(NULL, nameVideo.c_str(), NULL);
    if (oformat == NULL)
    {
        oformat = av_guess_format("h264", NULL, NULL);
    }

    AVCodec* m_codec;
    AVFormatContext* m_formatContext;
    AVStream* m_stream;

    av_register_all();
    avcodec_register_all();
    avformat_network_init();

    int errorStatus = 0;
    av_log_set_level(AV_LOG_TRACE);

    if (avformat_alloc_output_context2(&m_formatContext, NULL, "h264", nameVideo.c_str()) < 0)
    {
        throw "Couldn't allocated context";
    }

    m_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
    if (!m_codec)
    {
        throw "Couldn't find encoder";
    }

    m_stream = avformat_new_stream(m_formatContext, m_codec);
    if (!m_stream)
    {
        throw "Couldn't open stream";
    }

    av_dump_format(m_formatContext, 0, nameVideo.c_str(), 1);

    m_stream->codec->pix_fmt = pxFmt;
    m_stream->codec->width = width;
    m_stream->codec->height = height;
    //m_stream->codec->max_b_frames = maxBFrames;
    //m_stream->time_base = frameRate;
    m_stream->codec->gop_size = gop;
    m_stream->codec->bit_rate = bitRate;
    m_stream->time_base = timeBase;

    m_formatContext->oformat = oformat;
    m_formatContext->video_codec = m_codec;
    m_formatContext->video_codec_id = oformat->video_codec;

    if (avcodec_open2(m_stream->codec, m_codec, NULL) < 0)
    {
        throw "Couldn't open codec";
    }

    av_dump_format(m_formatContext, 0, nameVideo.c_str(), 1);

    if ((errorStatus = avio_open(&m_formatContext->pb, nameVideo.c_str(), AVIO_FLAG_WRITE)) < 0)
    {
        throw "Couldn't open video";
    }

    if (avformat_write_header(m_formatContext, NULL) < 0)
    {
        throw "Couldn't write header";
    }

    int numBytes = avpicture_get_size(m_stream->codec->pix_fmt, m_stream->codec->width, m_stream->codec->height);
    int gotPacket=-1;

    for(int i=0;i<frames->size();i++)
    {
            ptsWrite = i+1;
            uint8_t *outbuf = (uint8_t *)malloc(numBytes);

            AVPacket pkt;
            av_init_packet(&pkt);
            pkt.data = outbuf;
            pkt.size = numBytes;
            pkt.dts = AV_NOPTS_VALUE; // also tried incrementing numbers, or zero
            pkt.stream_index = m_stream->index;
            pkt.duration = 0;
            pkt.pos = -1;
            pkt.flags = 0;
            pkt.flags |= AV_PKT_FLAG_KEY;
            AVFrame pFrame = frames->at(i);
            pFrame.pts = ptsWrite;

            if(avcodec_encode_video2(m_stream->codec,&pkt,&pFrame,&gotPacket)<0)
            {
                throw "Couldn't encode video";
            }

            if(gotPacket)
            {
                if(av_interleaved_write_frame(m_formatContext,&pkt)<0)
                {
                    throw "Couldn't write frame";
                }
            }
    }

    if(av_write_trailer(m_formatContext)<0)
    {
        throw "Couldn't write trailer";
    }

    ptsWrite = 0;
    return 0;
}

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Libav-user mailing list
[email protected]
http://ffmpeg.org/mailman/listinfo/libav-user

Reply via email to