我正在尝试将通过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->标记和AVFMT_NOFILE返回true或从avio_open收到错误。
有什么办法可以使用rtsp? 还是您认为我可以使用更简单的方法来实现流传输?
谢谢