我正在尝试开发一个实时流媒体应用程序,从网络摄像头捕获视频并将其编码为 H264 视频流并使用 rtsp 将其发送到设备, 我查了一些例子,发现了这个: FFMPEG to send RTSP encoded stream C++
我一直将它用作我的程序的引用。
但是我一直在获取 AVCodec 类型“MPEG”,我一直在尝试更改文件名 扩展名并尝试使用avformat_alloc_output_context2 的不同方法
这是相关代码:
const char *filename = "rtsp://127.0.0.1:7003/live.sdp";
AVOutputFormat *fmt;
AVFormatContext *oc;
AVStream *video_st;
AVCodec *video_codec;
video_st = NULL;
av_register_all();
avformat_network_init();
// format context:
avformat_alloc_output_context2(&oc,NULL,"rtsp",filename);
在调用 avformat_alloc_output_context2 oc->oformat->video_codec 后生成为 MPEG 编解码器。 我试过将文件扩展名更改为:
const char *filename = "rtsp://127.0.0.1:7003/live.264";
const char *filename = "rtsp://127.0.0.1:7003/live.h264";
还有一些其他的扩展。
如何生成 H264 流?
最佳答案
根据 the documentation ,您需要将输出格式传递给 avformat_alloc_output_context2
。
引用(方括号里的内容是我的):
[the second parameter is the] format to use for allocating the context, if NULL format_name and filename are used instead
这个参数是一个指向类型AVOutputFormat的指针.
或者,您可以继续传递 NULL
并使用 H264 作为格式名称来推断输出。
无论哪种方式,我认为您都需要将编码过程与流媒体协议(protocol)分开。
这种关注点分离很有用,就好像你发现你想使用不同的协议(protocol)来获取流一样,编码代码不需要知道它。
关于c++ - h264编码视频流ffmpeg C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27656101/