ffmpeg - 将图像编码为 h264 和 rtp 输出:没有 sprop-parameter-sets 的 SDP 文件无法播放
问题描述
tl; dr:我尝试将获取的相机帧编码为 h264,通过 RTP 发送并在另一台设备上播放。ffmpeg 为示例视频生成的 SDP 文件包含我自己的 SDP 文件丢失的信息。我的 SDP 文件在 ffplay 中播放,但不是 VLC,而两者都播放 ffmpeg 的 SDP 文件。我怀疑我的 SDP 文件中缺少 sprop-parameter-sets。
最终我想在 VLC 中播放这个。
我正在编写将图像编码为 h264 并输出到 RTP 服务器(或客户端?无论如何是正在监听的部分)的代码。我为此生成了一个 SDP 文件。
- ffplay 播放流没有问题
- mplayer 显示了一个嵌入在较大黑框中的绿色框,但我在某处读到它仅支持 mpegts over RTP,所以不确定
- VLC 不播放 SDP 文件。
现在,当我使用一些随机视频并让 ffmpeg 像这样输出 SDP 文件时
ffmpeg -re -i some.mp4 -an -c:v copy -f rtp -sdp_file
video.sdp "rtp://127.0.0.1:5004"
我可以看到生成的 SDP 文件(在 ffplay 和 VLC 中播放)包含 base64 编码sprop-parameter-sets
字段,删除它会导致流无法播放。
> cat video.sdp
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
a=tool:libavformat 58.76.100
m=video 5004 RTP/AVP 96
b=AS:1034
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;
sprop-parameter-sets=Z2QANKzZQDAA7fiMBagICAoAAAMAAgAAAwDwHjBjLA==,aOvjyyLA;
profile-level-id=640034
另一方面,我自己的 SDP 文件不包含此信息,VLC 挂起 10 秒,然后停止尝试“未收到数据”。
> cat test.sdp
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
a=tool:libavformat 58.76.100
m=video 44499 RTP/AVP 96
b=AS:2000
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1
所以我的理论是我的自定义代码必须以某种方式将此 SPS 信息添加到 SDP 文件中。但是,尽管搜索了几个小时,我还是找不到一种结构化的方法来设置AVStream
's
上的 extradata 字段AVCodecParams
。我正在使用的代码大致是这样的(我确定那里有不相关的错误):
// variables
std::vector<std::uint8_t> imgbuf;
AVFormatContext *ofmt_ctx = nullptr;
AVCodec *out_codec = nullptr;
AVStream *out_stream = nullptr;
AVCodecContext *out_codec_ctx = nullptr;
SwsContext *swsctx = nullptr;
cv::Mat canvas_;
unsigned int height_;
unsigned int width_;
unsigned int fps_;
AVFrame *frame_ = nullptr;
AVOutputFormat *format = av_guess_format("rtp", nullptr, nullptr);
const auto url = std::string("rtp://127.0.0.1:5001");
avformat_alloc_output_context2(ofmt_ctx, format, format->name, url.c_str());
out_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
stream = avformat_new_stream(ofmt_ctx, out_codec);
out_codec_ctx = avcodec_alloc_context3(out_codec);
// then, for each incoming image:
while (receive_image) {
static bool first_time = true;
if (first_time) {
// discover necessary params such as image dimensions from the first
// received image
first_time = false;
height_ = image.rows;
width_ = image.cols;
codec_ctx->codec_tag = 0;
codec_ctx->bit_rate = 2e6;
// does nothing, unfortunately
codec_ctx->thread_count = 1;
codec_ctx->codec_id = AV_CODEC_ID_H264;
codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
codec_ctx->width = width_;
codec_ctx->height = height_;
codec_ctx->gop_size = 6;
codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
codec_ctx->framerate = fps_;
codec_ctx->time_base = av_inv_q(fps_);
avcodec_parameters_from_context(stream, out_codec_ctx);
// this stuff is empty: is that the problem?
stream->codecpar->extradata = codec_ctx->extradata;
stream->codecpar->extradata_size = codec_ctx->extradata_size;
AVDictionary *codec_options = nullptr;
av_dict_set(&codec_options, "profile", "high", 0);
av_dict_set(&codec_options, "preset", "ultrafast", 0);
av_dict_set(&codec_options, "tune", "zerolatency", 0);
// open video encoder
avcodec_open2(codec_ctx, codec, &codec_options);
stream->time_base.num = 1;
stream->time_base.den = fps_;
avio_open(&(ofmt_ctx->pb), ofmt_ctx->filename, AVIO_FLAG_WRITE);
/* Write a file for VLC */
char buf[200000];
AVFormatContext *ac[] = {ofmt_ctx};
av_sdp_create(ac, 1, buf, 20000);
printf("sdp:\n%s\n", buf);
FILE *fsdp = fopen("test.sdp", "w");
fprintf(fsdp, "%s", buf);
fclose(fsdp);
swsctx = sws_getContext(width_, height_, AV_PIX_FMT_BGR24, width_, height_,
out_codec_ctx->pix_fmt, SWS_BICUBIC, nullptr,
nullptr, nullptr);
}
if (!frame_) {
frame_ = av_frame_alloc();
std::uint8_t *framebuf = new uint8_t[av_image_get_buffer_size(
codec_ctx->pix_fmt, width_, height_, 1)];
av_image_fill_arrays(frame_->data, frame_->linesize, framebuf,
codec_ctx->pix_fmt, width, height, 1);
frame_->width = width_;
frame_->height = height_;
frame_->format = static_cast<int>(codec_ctx->pix_fmt);
success = avformat_write_header(ofmt_ctx, nullptr);
}
if (imgbuf.empty()) {
imgbuf.resize(height_ * width_ * 3 + 16);
canvas_ = cv::Mat(height_, width_, CV_8UC3, imgbuf.data(), width_ * 3);
} else {
image.copyTo(canvas_);
}
const int stride[] = {static_cast<int>(image.step[0])};
sws_scale(swsctx, &canvas_.data, stride, 0, canvas_.rows, frame_->data,
frame_->linesize);
frame_->pts += av_rescale_q(1, out_codec_ctx->time_base, stream->time_base);
AVPacket pkt = {0};
avcodec_send_frame(out_codec_ctx, frame_);
avcodec_receive_packet(out_codec_ctx, &pkt);
av_interleaved_write_frame(ofmt_ctx, &pkt);
}
有人可以在这里提供一些建议吗?
--
更新
设置时
this->out_codec_ctx->flags |=AV_CODEC_FLAG_GLOBAL_HEADER;
extradata 实际上存在于编解码器上下文中,但我不得不移到avcodec_parameters_from_context()
之后avcodec_open2()
,因为在打开编解码器之前 extradata 是空的。我现在进入sprop-parameter-sets
SDP 文件,但 VLC 仍然无法播放。
解决方案
在我的情况下,解决方案是端口号(???)。显然,VLC 无法接收44499
我正在使用的端口,但5004
就像 ffmpeg 示例一样。我不知道这是 MacOS 的特性还是转移到了 linux。
我尝试了几个端口:
- 5001 不工作
- 5002 作品
- 5003 不工作
- 5004 作品
- 5005 不工作
- 5006件作品
- 44498 件作品
所以看来VLC要接收RTP数据包,端口号必须是偶数?笏?
解释似乎是live555丢弃了端口号的lsb:https ://github.com/rgaufman/live555/blob/master/liveMedia/MediaSession.cpp#L696
因此,只有偶数端口才能通过不变。这是RFC中推荐或强制要求的:
对于 UDP 和类似协议,RTP 应该使用偶数目标端口号,并且相应的 RTCP 流应该使用下一个更高(奇数)的目标端口号。
推荐阅读
- c++ - InitInstance 在 UpdateWindow 抛出异常
- python - 返回 Elasticsearch Python 查询中的特定嵌套字段
- gnuplot - Gnuplot 读取特定列的文件数据而不绘图
- php - 多维BBCODE
- python - 在使用条件格式时在现有工作簿上使用 ExcelWriter
- azure - 在 Docker 中设置自托管 devops 代理时无法解析远程名称:“dev.azure.com”
- powershell - 将列添加到来自 vCenter Powershell 的现有 csv 输出
- python - 在pyinstaller之后循环不循环
- node.js - 如何将多个相同的表单字段发布到数据库
- python - 使用 Python 登录文件