gpt4 book ai didi

c++ - 如何使用 FFMPEG 从存储为视频 AVPackets 的 NAL 单元播放 H.264 流

转载 作者:行者123 更新时间:2023-11-28 03:19:16 25 4
gpt4 key购买 nike

我正在编写客户端-服务器系统,该系统使用 FFMPEG 库将 H.264 流解析为服务器端的 NAL 单元,然后使用信道编码将它们通过网络发送到客户端,我的应用程序必须能够在客户端播放视频.

问题是如何在我的应用程序中将接收到的 AVPackets(NAL 单元)作为视频流播放。我找到了 this tutorial很有帮助,并将其用作服务器端和客户端的基础。

一些与播放视频相关的示例代码或资源不是来自文件,而是来自使用 FFMPEG 库的程序内部数据将非常有帮助。

我确信收到的信息足以播放视频,因为我尝试将收到的数据保存为 .h264 或 .mp4 文件,并且可以通过 VLC 播放器播放。

最佳答案

我从你的问题中了解到,你有 AVPackets 并且想要播放视频。实际上这是两个问题; 1. 解码您的数据包,以及 2. 播放视频。

要使用 FFmpeg 解码您的数据包,您应该查看 AVPacket 的文档, AVCodecContextavcodec_decode_video2获得一些想法;一般的想法是你想做一些事情(只是在浏览器中写这个,用一粒盐):

//the context, set this appropriately based on your video. See the above links for the documentation 
AVCodecContext *decoder_context;
std::vector<AVPacket> packets; //assume this has your packets
...
AVFrame *decoded_frame = av_frame_alloc();
int ret = -1;
int got_frame = 0;
for(AVPacket packet : packets)
{
avcodec_get_frame_defaults(frame);
ret = avcodec_decode_video2(decoder_context, decoded_frame, &got_frame, &packet);
if (ret <= 0) {
//had an error decoding the current packet or couldn't decode the packet
break;
}

if(got_frame)
{
//send to whatever video player queue you're using/do whatever with the frame
...
}
got_frame = 0;
av_free_packet(&packet);
}

这是一个非常粗略的草图,但这是解码 AVPackets 问题的一般思路。至于你播放视频的问题,你有很多选择,这可能更多地取决于你的客户。你问的是一个相当大的问题,我建议你熟悉 FFmpeg 文档和在 the FFmpeg site 提供的示例。 .希望这是有道理的

关于c++ - 如何使用 FFMPEG 从存储为视频 AVPackets 的 NAL 单元播放 H.264 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15938469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com