【发布时间】:2021-09-14 16:26:26
【问题描述】:
我正在尝试实现一些简单的事情:编写捕获视频流并将其“按原样”保存到 *.mkv 文件中的代码(是的,没有解复用或重新编码等)。只想存储那些AVPacket-s,MKV 容器看起来已经准备好了。
请注意,问题是关于 ffmpeg library 的使用,ffmpeg binary 工作正常,可用于通过以下方式保存 HLS steam 数据:
@ 987654322@
我知道,但目标是保存 any(或几乎任何)流,因此是 MKV。实际上,已经有一些代码可以保存流的数据,但在使用 HLS 时特别失败。
经过一些努力以提供简短但易读的 MCVE,这里有一个重现问题的示例代码。重点是使输出编解码器与 HLS 流一起使用,因此它可能缺少很多东西和细节,例如额外的错误检查、极端情况、优化、正确的时间戳处理等。
#include <atomic>
#include <condition_variable>
#include <deque>
#include <functional>
#include <iostream>
#include <memory>
#include <mutex>
#include <thread>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
// Some public stream. The code works with RTSP, RTMP, MJPEG, etc.
// static const char SOURCE_NAME[] = "http://81.83.10.9:8001/mjpg/video.mjpg"; // works!
// My goal was an actual cam streaming via HLS, but here are some random HLS streams
// that reproduce the problem quite well. Playlists may differ, but the error is exactly the same
static const char SOURCE_NAME[] = "http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"; // fails!
// static const char SOURCE_NAME[] = "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8"; // fails!
using Pkt = std::unique_ptr<AVPacket, void (*)(AVPacket *)>;
std::deque<Pkt> frame_buffer;
std::mutex frame_mtx;
std::condition_variable frame_cv;
std::atomic_bool keep_running{true};
AVCodecParameters *common_codecpar = nullptr;
std::mutex codecpar_mtx;
std::condition_variable codecpar_cv;
void read_frames_from_source(unsigned N)
{
AVFormatContext *fmt_ctx = avformat_alloc_context();
int err = avformat_open_input(&fmt_ctx, SOURCE_NAME, nullptr, nullptr);
if (err < 0) {
std::cerr << "cannot open input" << std::endl;
avformat_free_context(fmt_ctx);
return;
}
err = avformat_find_stream_info(fmt_ctx, nullptr);
if (err < 0) {
std::cerr << "cannot find stream info" << std::endl;
avformat_free_context(fmt_ctx);
return;
}
// Simply finding the first video stream, preferrably H.264. Others are ignored below
int video_stream_id = -1;
for (unsigned i = 0; i < fmt_ctx->nb_streams; i++) {
auto *c = fmt_ctx->streams[i]->codecpar;
if (c->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_id = i;
if (c->codec_id == AV_CODEC_ID_H264)
break;
}
}
if (video_stream_id < 0) {
std::cerr << "failed to find find video stream" << std::endl;
avformat_free_context(fmt_ctx);
return;
}
{ // Here we have the codec params and can launch the writer
std::lock_guard<std::mutex> locker(codecpar_mtx);
common_codecpar = fmt_ctx->streams[video_stream_id]->codecpar;
}
codecpar_cv.notify_all();
unsigned cnt = 0;
while (++cnt <= N) { // we read some limited number of frames
Pkt pkt{av_packet_alloc(), [](AVPacket *p) { av_packet_free(&p); }};
err = av_read_frame(fmt_ctx, pkt.get());
if (err < 0) {
std::cerr << "read packet error" << std::endl;
continue;
}
// That's why the cycle above, we write only one video stream here
if (pkt->stream_index != video_stream_id)
continue;
{
std::lock_guard<std::mutex> locker(frame_mtx);
frame_buffer.push_back(std::move(pkt));
}
frame_cv.notify_one();
}
keep_running.store(false);
avformat_free_context(fmt_ctx);
}
void write_frames_into_file(std::string filepath)
{
AVFormatContext *out_ctx = nullptr;
int err = avformat_alloc_output_context2(&out_ctx, nullptr, "matroska", filepath.c_str());
if (err < 0) {
std::cerr << "avformat_alloc_output_context2 failed" << std::endl;
return;
}
AVStream *video_stream = avformat_new_stream(out_ctx, avcodec_find_encoder(common_codecpar->codec_id)); // the proper way
// AVStream *video_stream = avformat_new_stream(out_ctx, avcodec_find_encoder(AV_CODEC_ID_H264)); // forcing the H.264
// ------>> HERE IS THE TROUBLE, NO CODEC WORKS WITH HLS <<------
int video_stream_id = video_stream->index;
err = avcodec_parameters_copy(video_stream->codecpar, common_codecpar);
if (err < 0) {
std::cerr << "avcodec_parameters_copy failed" << std::endl;
}
if (!(out_ctx->flags & AVFMT_NOFILE)) {
err = avio_open(&out_ctx->pb, filepath.c_str(), AVIO_FLAG_WRITE);
if (err < 0) {
std::cerr << "avio_open fail" << std::endl;
return;
}
}
err = avformat_write_header(out_ctx, nullptr); // <<--- ERROR WITH HLS HERE
if (err < 0) {
std::cerr << "avformat_write_header failed" << std::endl;
return; // here we go with hls
}
unsigned cnt = 0;
while (true) {
std::unique_lock<std::mutex> locker(frame_mtx);
frame_cv.wait(locker, [&] { return !frame_buffer.empty() || !keep_running; });
if (!keep_running)
break;
Pkt pkt = std::move(frame_buffer.front());
frame_buffer.pop_front();
++cnt;
locker.unlock();
pkt->stream_index = video_stream_id; // mandatory
err = av_write_frame(out_ctx, pkt.get());
if (err < 0) {
std::cerr << "av_write_frame failed " << cnt << std::endl;
} else if (cnt % 25 == 0) {
std::cout << cnt << " OK" << std::endl;
}
}
av_write_trailer(out_ctx);
avformat_free_context(out_ctx);
}
int main()
{
std::thread reader(std::bind(&read_frames_from_source, 1000));
std::thread writer;
// Writer wont start until reader's got AVCodecParameters
// In this example it spares us from setting writer's params properly manually
{ // Waiting for codec params to be set
std::unique_lock<std::mutex> locker(codecpar_mtx);
codecpar_cv.wait(locker, [&] { return common_codecpar != nullptr; });
writer = std::thread(std::bind(&write_frames_into_file, "out.mkv"));
}
reader.join();
keep_running.store(false);
writer.join();
return 0;
}
这里发生了什么?简单地说:
- 产生了两个线程,一个从源读取数据包并将它们存储在缓冲区中
- 作者等待读者获取
AVCodecParameters,这样你就可以看到它们被使用的是相同的,这里几乎没有手动设置参数 - 阅读器应该阅读N个数据包并完成,然后作者跟随他。这就是它与 RTSP、RTMP、MJPEG 等一起使用的方式。
有什么问题?一旦尝试了 HLS 流,就会出现以下错误:
标签 [27][0][0][0] 与输出编解码器 id '27' (H264) 不兼容
在那之后作者通过它的上下文(即avformat_write_header这里)在任何写尝试上出现段错误avformat_write_header失败并出现错误(见下面的UPD2),因此不可能成功的写操作.
什么都试过了:
- 强制使用任意编解码器(例如:
AV_CODEC_ID_H264)。运气不好。 - 尝试
AV_CODEC_ID_MPEGTS。不可能,它被记录为满足内部需求的“假”编解码器。 - 切换输入或输出上下文的多个选项中的一些,但运气不好
我现在很困惑,因为错误听起来像“标签 H264 与编解码器 H264 不兼容”。 ffmpeg 日志看起来像是库设法理解它正在处理通过 HLS 发送的 MPEG-TS,读取很好,但写入所选媒体容器失败:
[hls @ 0x7f94b0000900] Opening 'https://bitdash-a.akamaihd.net/content/MI201109210084_1/video/540_1200000/hls/segment_0.ts' for reading
[hls @ 0x7f94b0000900] Opening 'https://bitdash-a.akamaihd.net/content/MI201109210084_1/video/540_1200000/hls/segment_1.ts' for reading
[hls @ 0x7f94b0000900] Opening 'https://bitdash-a.akamaihd.net/content/MI201109210084_1/video/720_2400000/hls/segment_0.ts' for reading
[hls @ 0x7f94b0000900] Opening 'https://bitdash-a.akamaihd.net/content/MI201109210084_1/video/720_2400000/hls/segment_1.ts' for reading
[hls @ 0x7f94b0000900] Opening 'https://bitdash-a.akamaihd.net/content/MI201109210084_1/video/1080_4800000/hls/segment_0.ts' for reading
[hls @ 0x7f94b0000900] Opening 'https://bitdash-a.akamaihd.net/content/MI201109210084_1/video/1080_4800000/hls/segment_1.ts' for reading
[hls @ 0x7f94b0000900] Could not find codec parameters for stream 0 (Audio: aac ([15][0][0][0] / 0x000F), 0 channels, 112 kb/s): unspecified sample rate
Consider increasing the value for the 'analyzeduration' and 'probesize' options
[matroska @ 0x7f94a8000900] Tag [27][0][0][0] incompatible with output codec id '27' (H264)
avformat_write_header failed
Segmentation fault (core dumped)
谷歌搜索没有帮助,我有点绝望。
请分享您的想法,将不胜感激。
更新
-
ffmpeg -i https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8 out.mkv工作正常 -
ffmpeg -i http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8 -c:v copy out.mkv也可以正常工作
...这意味着ffmpeg 可以做到这一点并且可以达到预期的结果
UPD2
出现标签错误可以通过out_ctx->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL;抑制
我认为这是关于在字符串标签中正确拼写“h264”的问题,看起来并不严重。
另外,仔细观察后发现实际上是段错误是av_write_frame。难怪 - 使用 HLS 流 avformat_write_header 失败并返回错误:
处理输入时发现无效数据
这仍然让我没有任何线索,问题出在哪里=((
【问题讨论】:
标签: c++ video ffmpeg http-live-streaming mkv