【问题标题】:ffmpeg recording h264 live stream got errorffmpeg 录制 h264 直播时出错
【发布时间】:2015-08-07 03:35:57
【问题描述】:

我正在尝试使用以下代码录制 h.264 直播:

   AVOutputFormat* fmt = av_guess_format(NULL, "test.mpeg", NULL);
   AVFormatContext* oc  = avformat_alloc_context();
   oc->oformat = fmt;
   avio_open2(&oc->pb, "test.mpeg", AVIO_FLAG_WRITE, NULL, NULL);
   AVStream* stream = NULL;
   ...
   while(!done)
   {

      // Read a frame
      if(av_read_frame(inputStreamFormatCtx, &packet)<0)
         return false;      

      if(packet.stream_index==correct_index)
      {
          ////////////////////////////////////////////////////////////////////////
         // Is this a packet from the video stream -> decode video frame
          if (stream == NULL){//create stream in file
              stream = avformat_new_stream(oc, pFormatCtx->streams[videoStream]->codec->codec);
              avcodec_copy_context(stream->codec, pFormatCtx->streams[videoStream]->codec);
              stream->sample_aspect_ratio = pFormatCtx->streams[videoStream]->codec->sample_aspect_ratio;

              stream->sample_aspect_ratio.num = pFormatCtx->streams[videoStream]->codec->sample_aspect_ratio.num;
              stream->sample_aspect_ratio.den = pFormatCtx->streams[videoStream]->codec->sample_aspect_ratio.den;

              // Assume r_frame_rate is accurate
              stream->r_frame_rate = pFormatCtx->streams[videoStream]->r_frame_rate;
              stream->avg_frame_rate = stream->r_frame_rate;
              stream->time_base = av_inv_q(stream->r_frame_rate);
              stream->codec->time_base = stream->time_base;

              avformat_write_header(oc, NULL);
          }
          av_write_frame(oc, &packet);
          ...
      }
    }

但是,ffmpeg 说

encoder did not produce proper pts making some up

当代码运行到 av_write_frame();这里有什么问题?

【问题讨论】:

    标签: c video ffmpeg video-streaming video-capture


    【解决方案1】:

    首先确保inputStreamFormatCtx 分配和填充正确的值(这是解复用/再复用问题的 90% 问题的原因) - 检查互联网上的一些示例以了解您应该如何分配和设置其值。

    该错误告诉我们正在发生的事情,它似乎只是一个警告。 PTS(Presentation Time Stamp)是一个基于stream-&gt;time_base的数字,它告诉我们什么时候应该显示这个数据包的解码帧。当您通过网络获得实时流时,服务器可能没有为数据包的 PTS 输入有效编号,并且当您收到数据时,它具有 INVALID PTS(您可以通过阅读 packet.pts 并检查它是否为AV_NOPTS_VALUE)。所以 libav 会尝试根据流的帧速率和 time_base 生成正确的 pts。这是一个有用的尝试,如果录制的文件可以以真实动作(fps-wise)播放,你应该很高兴。如果录制的文件将以快动作或慢动作(fps-wise)播放,你就会遇到问题,你不能再依赖 libav 来纠正 fps。那么你应该通过解码数据包来计算正确的fps,然后根据stream-&gt;time_base计算正确的pts并将其设置为packet.pts

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 2018-12-18
      • 2020-08-08
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 2012-03-10
      相关资源
      最近更新 更多