rtmp avformat_write_header -22
时间: 2023-09-02 13:13:31 浏览: 59
avformat_write_header返回值为-22表示出现了无效的参数。这可能是因为你在调用avformat_write_header函数时传递了错误的参数或者没有正确的初始化编解码器导致的。你可以检查以下几点:
1. 检查传递给avformat_write_header函数的AVFormatContext结构体是否正确初始化,包括设置正确的封装格式、视频/音频编码器等参数。
2. 检查AVIOContext参数是否正确设置,如果你使用了自定义的IO回调函数,需要确保回调函数正确实现。
3. 检查是否正确设置了视频/音频流的参数,包括编码器、分辨率、帧率、采样率、声道数等。
如果以上几点都检查无误,还是出现-22错误码,可以尝试打印出错误信息以便更好的定位问题,例如使用av_strerror函数打印错误信息。
相关问题
生成一个qt下使用opencv 进行 rtmp推流 其中包含 rtmp_sendpacket 例程
很高兴能为您提供帮助,以下是生成一个Qt下使用OpenCV进行rtmp推流的步骤和例程:
步骤:
1.安装OpenCV,可以从OpenCV官网下载对应的版本进行安装。
2.下载并安装FFmpeg,可以从FFmpeg官网下载对应的版本进行安装。
3.在Qt中配置OpenCV和FFmpeg,在项目.pro文件中添加以下代码:
```
INCLUDEPATH += [OpenCV的include目录]
LIBS += -L[OpenCV的lib目录] -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_videoio -lopencv_imgcodecs
INCLUDEPATH += [FFmpeg的include目录]
LIBS += -L[FFmpeg的lib目录] -lavcodec -lavdevice -lavfilter -lavformat -lavutil -lswresample -lswscale -lavcodec
```
4.编写rtmp推流代码,示例代码如下:
```
#include <opencv2/opencv.hpp>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#define STREAM_URL "rtmp://[推流地址]"
using namespace cv;
int main()
{
AVFormatContext *pFormatCtx;
AVOutputFormat *fmt;
AVStream *video_st;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameRGB;
uint8_t *buffer;
int videoindex = -1;
int framecnt = 0;
int64_t start_time = 0;
struct SwsContext *img_convert_ctx;
// OpenCV读取视频文件
VideoCapture capture(0);
if(!capture.isOpened())
{
printf("OpenCV: Could not open camera.\n");
return -1;
}
// 初始化FFmpeg
av_register_all();
// 初始化输出格式
avformat_alloc_output_context2(&pFormatCtx, NULL, "flv", STREAM_URL);
if(!pFormatCtx)
{
printf("FFmpeg: Could not allocate output context.\n");
return -1;
}
fmt = pFormatCtx->oformat;
// 添加视频流
video_st = avformat_new_stream(pFormatCtx, 0);
if(!video_st)
{
printf("FFmpeg: Could not create new stream.\n");
return -1;
}
videoindex = video_st->index;
// 设置编码器参数
pCodecCtx = video_st->codec;
pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
pCodecCtx->width = capture.get(CV_CAP_PROP_FRAME_WIDTH);
pCodecCtx->height = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 25;
// 查找编码器
pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if(!pCodec)
{
printf("FFmpeg: Could not find encoder.\n");
return -1;
}
// 打开编码器
if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("FFmpeg: Could not open encoder.\n");
return -1;
}
// 分配视频帧内存
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc();
// 分配视频帧缓冲区内存
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
av_image_fill_arrays(pFrame->data, pFrame->linesize, buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
// 分配视频帧RGB缓冲区内存
uint8_t *rgbBuffer = (uint8_t *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1));
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, rgbBuffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1);
// 初始化图像转换上下文
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
// 输出格式信息
av_dump_format(pFormatCtx, 0, STREAM_URL, 1);
// 打开输出URL
if(!(fmt->flags & AVFMT_NOFILE))
{
if(avio_open(&pFormatCtx->pb, STREAM_URL, AVIO_FLAG_WRITE) < 0)
{
printf("FFmpeg: Could not open output URL.\n");
return -1;
}
}
// 写入头部
avformat_write_header(pFormatCtx, NULL);
while(capture.read(pFrameRGB->data[0]))
{
// RGB转YUV
sws_scale(img_convert_ctx, pFrameRGB->data, pFrameRGB->linesize, 0, pCodecCtx->height, pFrame->data, pFrame->linesize);
// 视频帧时间
AVRational time_base = {1, 1000};
int64_t pts = framecnt * (pCodecCtx->time_base.den * 1000 / pCodecCtx->time_base.num) / 25;
pFrame->pts = av_rescale_q(pts, time_base, video_st->time_base);
pFrame->key_frame = 1;
// 编码并推送视频帧
AVPacket pkt;
int ret = avcodec_send_frame(pCodecCtx, pFrame);
if(ret < 0)
{
printf("FFmpeg: Error sending frame.\n");
break;
}
while(ret >= 0)
{
ret = avcodec_receive_packet(pCodecCtx, &pkt);
if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
break;
}
else if(ret < 0)
{
printf("FFmpeg: Error encoding frame.\n");
return -1;
}
// 发送视频帧Packet
av_packet_rescale_ts(&pkt, pCodecCtx->time_base, video_st->time_base);
pkt.stream_index = videoindex;
ret = av_interleaved_write_frame(pFormatCtx, &pkt);
av_packet_unref(&pkt);
}
// 输出进度信息
printf("FFmpeg: Encode frame index:%d\n", framecnt);
framecnt++;
// 推流时间
if(start_time == 0)
{
start_time = av_gettime();
}
int64_t now_time = av_gettime() - start_time;
int64_t duration = framecnt * pCodecCtx->time_base.num * 1000 / pCodecCtx->time_base.den;
if(now_time < duration)
{
av_usleep(duration - now_time);
}
}
// 写入尾部
av_write_trailer(pFormatCtx);
// 释放内存
avcodec_close(pCodecCtx);
av_free(pCodecCtx);
av_frame_free(&pFrame);
av_frame_free(&pFrameRGB);
av_free(buffer);
av_free(rgbBuffer);
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
return 0;
}
```
注:在实际使用中需要将[推流地址]替换为实际的地址。
这是一个简单的例程,仅供参考,具体实现方式还需要根据实际情况进行调整和完善。
FFmpeg rtsp转rtmp推流 c++代码
当然可以!以下是一个简单的示例代码,用于将RTSP流转发到RTMP服务器:
```cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
int main(int argc, char* argv[]) {
// 注册所有的FFmpeg组件
av_register_all();
// 创建输入上下文
AVFormatContext* inputContext = avformat_alloc_context();
// 打开RTSP流
if (avformat_open_input(&inputContext, "rtsp://your_rtsp_url", nullptr, nullptr) != 0) {
std::cerr << "无法打开RTSP流" << std::endl;
return -1;
}
// 查找流信息
if (avformat_find_stream_info(inputContext, nullptr) < 0) {
std::cerr << "无法获取流信息" << std::endl;
return -1;
}
// 创建输出上下文
AVFormatContext* outputContext = avformat_alloc_context();
// 设置输出格式为RTMP
AVOutputFormat* outputFormat = av_guess_format("flv", nullptr, nullptr);
outputContext->oformat = outputFormat;
// 打开输出URL
if (avio_open(&outputContext->pb, "rtmp://your_rtmp_url", AVIO_FLAG_WRITE) < 0) {
std::cerr << "无法打开RTMP URL" << std::endl;
return -1;
}
// 遍历输入流
for (unsigned int i = 0; i < inputContext->nb_streams; i++) {
AVStream* inputStream = inputContext->streams[i];
AVStream* outputStream = avformat_new_stream(outputContext, inputStream->codec->codec);
// 复制流参数
if (avcodec_copy_context(outputStream->codec, inputStream->codec) < 0) {
std::cerr << "无法复制流参数" << std::endl;
return -1;
}
outputStream->codec->codec_tag = 0;
if (outputContext->oformat->flags & AVFMT_GLOBALHEADER) {
outputStream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
// 写入输出头部
if (avformat_write_header(outputContext, nullptr) < 0) {
std::cerr << "无法写入输出头部" << std::endl;
return -1;
}
// 转发流数据
AVPacket packet;
while (av_read_frame(inputContext, &packet) >= 0) {
AVStream* inputStream = inputContext->streams[packet.stream_index];
AVStream* outputStream = outputContext->streams[packet.stream_index];
// 设置时间基
av_packet_rescale_ts(&packet, inputStream->time_base, outputStream->time_base);
packet.pos = -1;
// 写入输出流
if (av_interleaved_write_frame(outputContext, &packet) < 0) {
std::cerr << "无法写入输出流" << std::endl;
break;
}
av_packet_unref(&packet);
}
// 写入输出尾部
av_write_trailer(outputContext);
// 清理资源
avformat_close_input(&inputContext);
avio_close(outputContext->pb);
avformat_free_context(outputContext);
return 0;
}
```
请注意,这只是一个简单的示例代码,用于演示如何使用FFmpeg将RTSP流转发到RTMP服务器。你需要根据自己的需求进行适当的修改和调整。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)