[Libav-user] RTSP stream capture lost seeking and speed

Dnii Volna dniivolna at gmail.com
Wed May 20 17:58:25 EEST 2020


    i'm trying capture rtsp stream (h264) to local file and there is 3
problem.
1) sometimes (usually at the stream starts) i got pts,dts values of packet
incorrect (non monotonically increasing)
2) output file has no seeking information
3) output file speed seems 1.5x lower than rtsp source
any avice please

    AVFormatContext* inFormatCtx = avformat_alloc_context();
    if
(avformat_open_input(&inFormatCtx,m_RTSPPath.toLocal8Bit().data(),nullptr,nullptr))
return;
    if (avformat_find_stream_info(inFormatCtx,nullptr) < 0)  return;

    int videoStreamIdx =
av_find_best_stream(inFormatCtx,AVMEDIA_TYPE_VIDEO,-1,-1,nullptr,0);
    if (videoStreamIdx == -1) return;

    AVStream *inVideoStream = inFormatCtx->streams[videoStreamIdx];
    av_dump_format(inFormatCtx,videoStreamIdx,nullptr,0);
    av_read_play(inFormatCtx);

    AVFormatContext *outFormatCtx = nullptr;
    QString filename = m_archivePath + "/" +
QDateTime::currentDateTime().toString(m_dateTimeFormat);

    if
(avformat_alloc_output_context2(&outFormatCtx,nullptr,"avi",filename.toLocal8Bit().data()))
return;

    AVStream *outVideoStream = avformat_new_stream(outFormatCtx,nullptr);
    if
(avcodec_parameters_copy(outVideoStream->codecpar,inVideoStream->codecpar)
< 0)  return;

    if (!(outFormatCtx->oformat->flags & AVFMT_NOFILE) &&

(avio_open(&outFormatCtx->pb,filename.toLocal8Bit().data(),AVIO_FLAG_WRITE)<0))
return;

    if (avformat_write_header(outFormatCtx,nullptr) < 0) return;

    AVPacket packet;
    int count = 10000;
    while (count--) {
        if (av_read_frame(inFormatCtx,&packet) < 0) continue;

        if (videoStreamIdx != packet.stream_index) {
            av_packet_unref(&packet);
            continue;
        }

        qInfo()<<"<<<<<<<"<<packet.stream_index<<packet.pts<<packet.dts;

        packet.pts = av_rescale_q_rnd(packet.pts,
                                      inVideoStream->time_base,
                                      outVideoStream->time_base,

static_cast<AVRounding>(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
        packet.dts = av_rescale_q_rnd(packet.dts,
                                      inVideoStream->time_base,
                                      outVideoStream->time_base,

static_cast<AVRounding>(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
        packet.duration = av_rescale_q(packet.duration,
                                       inVideoStream->time_base,
                                       outVideoStream->time_base);
        packet.stream_index = outVideoStream->id;

        qInfo()<<">>>>>>>"<<packet.stream_index<<packet.pts<<packet.dts;

        if (av_interleaved_write_frame(outFormatCtx,&packet) < 0) continue;
        }
        av_packet_unref(&packet);
    }

    av_write_trailer(outFormatCtx);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20200520/a6fe8a94/attachment.html>


More information about the Libav-user mailing list