开发者社区> 问答> 正文

无法在C ++中使用ffmpeg通过RTSP流式传输

我正在尝试将通过rtsp在IP地址上读取的流发送到也通过rtsp的另一个IP地址。换句话说,我想复制正在读取的流。

我已经使用ffmpeg将流保存到MP4中。因此,我认为我也可以将其用于此目的。

这是代码示例:

m_outputURL ="rtsp://192.168.8.225:8554";
 avformat_alloc_output_context2(&m_outFormatContext, nullptr, "rtsp", &m_outputUrlFormatB[0]);
 m_outFormatContext->flags |= AVFMT_FLAG_GENPTS;

 if (!m_outFormatContext) {
     qDebug()<<"Could not create video storage stream as no output context could not be assigned based on filename";
     SSW_endStreaming();
     return ;
 }
 else
 {
     m_outputFmt = m_outFormatContext->oformat;
 }

 //Create output AVStream according to input AVStream
 AVStream *inputStream=m_inFormatContext->streams[m_videoStreamId];

 //find encoder from input stream
 AVCodec* codec = avcodec_find_encoder(inputStream->codecpar->codec_id);

 if (!(codec)) {
     qDebug()<<"Could not find codec from input stream";
     SSW_endStreaming();
     return ;
 }
 else
 {
     /* create video out stream */
     m_videoStream = avformat_new_stream(m_outFormatContext, codec);
 }

 if (!m_videoStream)
 {
     printf( "Failed allocating output stream\n");
     SSW_endStreaming();
 }
 else
 {
     /* copying input video context to output video context */
     ret = avcodec_parameters_copy(m_videoStream->codecpar, inputStream->codecpar);
 }

 if (ret < 0) {
     qDebug()<<"Unable to copy input video context to output video context";
     SSW_endStreaming();
     return;
 }
 else
 {
     m_videoStream->codecpar->codec_tag = 0;
 }

 /* open the output file, if needed */
 if (!(m_outputFmt->flags & AVFMT_NOFILE))
 {
     ret = avio_open(&m_outFormatContext->pb, &m_outputUrlFormatB[0], AVIO_FLAG_WRITE);
     if (ret < 0) {
         qDebug()<<"Could not open output file";
         SSW_endStreaming();
         return;
     }
     else
     {
         /* Write the stream header, if any. */
         ret = avformat_write_header(m_outFormatContext, nullptr);
     }
 }

每次m_outputFmt-> flags和AVFMT_NOFILE返回true或从avio_open收到错误。

有什么办法可以使用rtsp吗?还是您认为我可以使用更简单的方法来实现流传输?

谢谢

展开
收起
几许相思几点泪 2019-12-10 19:49:40 2359 1
2 条回答
写回答
取消 提交回答
  • 你好 请问这个问题解决了吗 我也碰见这种问题 转发rtmp协议的时候是可以的 但是换成rtsp协议 avio_open就会返回失败

    2020-07-31 17:54:43
    赞同 展开评论 打赏
  • 好难的问题

    2020-03-29 15:17:26
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载