使用ffmpeg将h264格式的RTSP实时视频流转Mp4格式保存

自己写的demo,也是在官方demo的基础上改的,做个记录 ,以后说不定用得上,转存的文件用软件看了一下格式,目测没问题

int VideoWork::saveVideoToMp4(const char *rtspUrl)
{
    m_bStop = false;
    int id = (int)QThread::currentThreadId();
    emit signal_sendLog(u8"子线程编码:" + QString::number(id));
   /* should set to NULL so that avformat_open_input() allocate a new one */
      m_pFmtContext = NULL;
//       char rtspUrl[] = "rtsp://admin:[email protected]:554/h264/ch33/main/av_stream";
      QString name = "./" + QDateTime::currentDateTime().toString("yyyyMMdd_hhmmsszzz") + ".mp4";
      char cname[256] = {0};
      std::strcpy(cname,name.toStdString().c_str());
      const char *filename = cname;
      if (avformat_open_input(&m_pFmtContext, rtspUrl, NULL, NULL)!=0)
      {
          emit signal_sendLog( "could not open input file\n");
          return -1;
      }

      if (avformat_find_stream_info(m_pFmtContext, NULL)<0)
      {
          emit signal_sendLog( "could not find stream info\n");
          return -1;
      }

      //av_dump_format(m_pFmtContext, 0, argv[1], 0);

      /* find first video stream */
      for (unsigned i=0; i<m_pFmtContext->nb_streams; i++)
      {
          if (m_pFmtContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
          {
              m_pVideoStream = m_pFmtContext->streams[i];
              break;
          }
      }
      if (m_pVideoStream == NULL)
      {
          emit signal_sendLog("didn't find any video stream\n");
          return -1;
      }

      avformat_alloc_output_context2(&o_fmt_ctx, NULL, NULL, filename);

      /*
      * since all input files are supposed to be identical (framerate, dimension, color format, ...)
      * we can safely set output codec values from first input file
      */
      o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);
      {
          AVCodecContext *c;
          c = o_video_stream->codec;
          c->bit_rate = 400000;
          c->codec_id = m_pVideoStream->codec->codec_id;
          c->codec_type = m_pVideoStream->codec->codec_type;
          c->time_base.num = m_pVideoStream->time_base.num;
          c->time_base.den = m_pVideoStream->time_base.den;
          QString sLog = "time_base.num = " + QString::number(c->time_base.num) + "  time_base.den = " + QString::number(c->time_base.den);
          emit signal_sendLog( sLog);
          c->width = m_pVideoStream->codec->width;
          c->height = m_pVideoStream->codec->height;
          c->pix_fmt = m_pVideoStream->codec->pix_fmt;
//          emit signal_sendLog("%d %d %d", c->width, c->height, c->pix_fmt);
          c->flags = m_pVideoStream->codec->flags;
          c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
          c->me_range = m_pVideoStream->codec->me_range;
          c->max_qdiff = m_pVideoStream->codec->max_qdiff;

          c->qmin = m_pVideoStream->codec->qmin;
          c->qmax = m_pVideoStream->codec->qmax;

          c->qcompress = m_pVideoStream->codec->qcompress;
      }

      avio_open(&o_fmt_ctx->pb, filename, AVIO_FLAG_WRITE);

      avformat_write_header(o_fmt_ctx, NULL);

      int last_pts = 0;
      int last_dts = 0;

      int64_t pts, dts;
      while (!m_bStop)
      {
          AVPacket i_pkt;
          av_init_packet(&i_pkt);
          i_pkt.size = 0;
          i_pkt.data = NULL;
          if (av_read_frame(m_pFmtContext, &i_pkt) <0 )
              break;
          /*
          * pts and dts should increase monotonically
          * pts should be >= dts
          */
          i_pkt.flags |= AV_PKT_FLAG_KEY;
          pts = i_pkt.pts;
          i_pkt.pts += last_pts;
          dts = i_pkt.dts;
          i_pkt.dts += last_dts;
          i_pkt.stream_index = 0;


          // emit signal_sendLog("%lld %lld\n", i_pkt.pts, i_pkt.dts);
          static int num = 1;
          printf("frame %d\n", num++);
          av_interleaved_write_frame(o_fmt_ctx, &i_pkt);
          //av_free_packet(&i_pkt);
          //av_init_packet(&i_pkt);
           _sleep(10);
      }

      emit signal_sendLog( "stop video ,free ffmpeg");
      last_dts += dts;
      last_pts += pts;

      avformat_close_input(&m_pFmtContext);

      av_write_trailer(o_fmt_ctx);
      //注意资源释放,不然内存一直涨额
      avcodec_close(o_fmt_ctx->streams[0]->codec);
      av_freep(&o_fmt_ctx->streams[0]->codec);
      av_freep(&o_fmt_ctx->streams[0]);

      avio_close(o_fmt_ctx->pb);
      av_free(o_fmt_ctx);
      return  0;
}

猜你喜欢

转载自blog.csdn.net/summer_9527/article/details/121830179