When we have a local video file This code works for streaming. What we want to achieve is to change input file with RGB frame buffer that we grab from screen. The code below uses input file's time_base
for PTS and DTS calculation.
if(pkt.stream_index==videoindex) {
AVRational time_base=ifmt_ctx->streams[videoindex]->time_base;
AVRational time_base_q={1,AV_TIME_BASE};
int64_t pts_time = av_rescale_q(pkt.dts, time_base, time_base_q);
int64_t now_time = av_gettime() - start_time;
if (pts_time > now_time)
av_usleep(pts_time - now_time);
}
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
/* copy packet */
//Convert PTS/DTS
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
Following this answer which increments PTS value by one for every frame VideoSt.Frame->pts = VideoSt.NextPts++;
saving in local file or streaming to Wowza server works (low quality) but fails on Youtube and Twitch. av_interleaved_write_frame
function returns error -22. To improve quality we lowered qmin
and qmax
values but the bit-rate of video increased too much (30Mb/s and even more for HD video).
VideoSt.Ctx->codec_id = VideoCodec->id;
VideoSt.Ctx->width = ViewportSize.X;
VideoSt.Ctx->height = ViewportSize.Y;
VideoSt.Stream->time_base = VideoSt.Ctx->time_base = { 1, 30 };
VideoSt.Ctx->time_base = timeBase;
VideoSt.Ctx->gop_size = 60;
VideoSt.Ctx->bit_rate = 400000;
VideoSt.Ctx->pix_fmt = AV_PIX_FMT_YUV420P;
VideoSt.Ctx->qmin = 1;
VideoSt.Ctx->qmax = 2;
VideoSt.Ctx->max_qdiff = 3;
VideoSt.Ctx->qcompress = 1.0f;
How can we stream lossless quality video with reasonable bit-rate using flv1 codec? If streaming to Youtube or Twitch fails is it just a problem with requirements of this services or there's an issue with encoding?
Regarding error code -22 there's a detailed explanation which explains how we should increment PTS and DTS values.
In which cases calculating PTS and DTS values are necessary?
And by the way what are these PTS and DTS? After reading many posts and Understanding PTS and DTS in video frames accepted answer I still do not understand.