简体   繁体   English

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

[英]How to use FFMPEG to play H.264 stream from NAL units that are stored as video AVPackets

I am writing client-server system that uses FFMPEG library to parse H.264 stream into NAL units on the server side, then uses channel coding to send them over network to client side, where my application must be able to play video. 我正在编写客户端 - 服务器系统,它使用FFMPEG库将H.264流解析为服务器端的NAL单元,然后使用信道编码通过网络将它们发送到客户端,我的应用程序必须能够播放视频。

The question is how to play received AVPackets (NAL units) in my application as video stream. 问题是如何在我的应用程序中播放收到的AVPackets(NAL单元)作为视频流。 I have found this tutorial helpful and used it as base for both server and client side. 我发现本教程很有用,并将其用作服务器端和客户端的基础。

Some sample code or resource related to playing video not from file, but from data inside program using FFMPEG library would be very helpful. 一些示例代码或资源与播放视频不是来自文件,而是来自使用FFMPEG库的程序内的数据相关将非常有用。

I am sure that received information will be sufficient to play video, because I tried to save received data as .h264 or .mp4 file and it can be played by VLC player. 我确信收到的信息足以播放视频,因为我试图将收到的数据保存为.h264或.mp4文件,它可以由VLC播放器播放。

Of what I understand from your question, you have the AVPackets and want to play a video. 我从你的问题中理解,你有AVPackets并想播放视频。 In reality this is two problems; 实际上这是两个问题; 1. decoding your packets, and 2. playing the video. 1.解码你的数据包,以及2.播放视频。

For decoding your packets, with FFmpeg, you should take a look at the documentation for AVPacket , AVCodecContext and avcodec_decode_video2 to get some ideas; 要使用FFmpeg对数据包进行解码,您应该查看AVPacketAVCodecContextavcodec_decode_video2的文档以获得一些想法; the general idea is that you want to do something (just wrote this in the browser, take with a grain of salt) along the lines of: 一般的想法是你想要做一些事情(只是在浏览器中写下这个,并带着一点点盐)沿着这样的方向:

//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);
}

It's a pretty rough sketch, but that's the general idea for your problem of decoding the AVPackets. 这是一个非常粗略的草图,但这是解决AVPackets问题的一般想法。 As for your problem of playing the video, you have many options, which will likely depend more on your clients. 至于你播放视频的问题,你有很多选择,这可能更多地取决于你的客户。 What you're asking is a pretty large problem, I'd advise familiarizing yourself with the FFmpeg documentation and the provided examples at the FFmpeg site . 您要问的是一个非常大的问题,我建议您熟悉FFmpeg文档以及FFmpeg站点提供的示例。 Hope that makes sense 希望有道理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM