用ffmpeg+x264如何编出动态gop的视频流

一 一般编码h264的时候需要设置gop size, group of picture size,意思是多少帧出现一个I帧。

这个值设置之后,编码器出帧就会严格按照这个间隔来的,那有人想要在画面运动剧烈的时候尽可能降低gop,在静态的时候增大gop,怎样做呢?

看看x264源码AVFrame结构体:

  uint8_t *data[AV_NUM_DATA_POINTERS];//存放yuv数据的指针,具体怎么存放和YUV layout格式有关,像YUV420P,YUV444,YUV422等
   int linesize[AV_NUM_DATA_POINTERS];//stride size,意思是上面这data对应的一行的数据长度
   enum AVPictureType pict_type;//帧类型

 
 enum AVPictureType {
    AV_PICTURE_TYPE_NONE = 0, ///< Undefined
    AV_PICTURE_TYPE_I,     ///< Intra
    AV_PICTURE_TYPE_P,     ///< Predicted
    AV_PICTURE_TYPE_B,     ///< Bi-dir predicted
    AV_PICTURE_TYPE_S,     ///< S(GMC)-VOP MPEG-4
    AV_PICTURE_TYPE_SI,    ///< Switching Intra
    AV_PICTURE_TYPE_SP,    ///< Switching Predicted
    AV_PICTURE_TYPE_BI,    ///< BI type
};

可以通过上面这个域pict_type来指定编码出来的帧类型,从而实现动态的gop大小。


注意:用x264编码的时候,有一个预设参数:

av_opt_set(f_DstCodecCtx_->priv_data, "x264opts", "rc-lookahead=30", 0); 
设定mb-tree位元率控制和vbv-lookahead使用的帧数。最大允许值是250。对于mb-tree部分,增加帧数带来更好的效果但也会更慢。mb-tree使用的最大缓冲值是MIN(rc-lookahead, --keyint)。上面设置为30,编码前30帧是不会出画面的。



这里提供一个使用 FFmpeg 推送 H264 视频的 C# 类示例,供你参考。请注意,这只是一个概述,具体实现可能会因你的需求而异。 ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; using FFmpeg.AutoGen; using System.IO; namespace FFmpegPusher { public class FFmpegPusher { private static readonly object syncLock = new object(); private bool isInitialized = false; private AVFormatContext* outputFormatContext = null; private AVStream* videoStream = null; private AVCodecContext* videoCodecContext = null; private AVFrame* videoFrame = null; private AVPacket* videoPacket = null; private byte[] videoBuffer = null; private int videoBufferSize = 0; private int videoFrameCount = 0; public FFmpegPusher() { // 初始化 FFmpeg 库 lock (syncLock) { if (!isInitialized) { avformat.av_register_all(); avformat.avformat_network_init(); isInitialized = true; } } } public void Open(string url, int width, int height, int frameRate, int bitRate) { // 打开输出流 avformat.avformat_alloc_output_context2(&outputFormatContext, null, "flv", url); if (outputFormatContext == null) { throw new Exception("Failed to allocate output context"); } // 打开视频流 var codec = avcodec.avcodec_find_encoder(AVCodecID.AV_CODEC_ID_H264); if (codec == null) { throw new Exception("Failed to find H264 encoder"); } videoStream = avformat.avformat_new_stream(outputFormatContext, codec); if (videoStream == null) { throw new Exception("Failed to allocate video stream"); } videoCodecContext = videoStream->codec; videoCodecContext->codec_id = codec->id; videoCodecContext->codec_type = AVMediaType.AVMEDIA_TYPE_VIDEO; videoCodecContext->pix_fmt = AVPixelFormat.AV_PIX_FMT_YUV420P; videoCodecContext->width = width; videoCodecContext->height = height; videoCodecContext->bit_rate = bitRate; videoCodecContext->time_base = new AVRational { num = 1, den = frameRate }; videoCodecContext->gop_size = frameRate; videoCodecContext->qmin = 10; videoCodecContext->qmax = 51; videoCodecContext->max_b_frames = 0; if (outputFormatContext->oformat->flags.HasFlag(AVFormatFlag.AVFMT_GLOBALHEADER)) { videoCodecContext->flags |= AVCodecFlag.AV_CODEC_FLAG_GLOBAL_HEADER; } avcodec.avcodec_open2(videoCodecContext, codec, null); // 分配视频帧和数据包 videoFrame = avutil.av_frame_alloc(); videoFrame->format = (int)videoCodecContext->pix_fmt; videoFrame->width = videoCodecContext->width; videoFrame->height = videoCodecContext->height; avutil.av_frame_get_buffer(videoFrame, 32); videoPacket = avutil.av_packet_alloc(); // 打开输出流 avio.avio_open(&outputFormatContext->pb, url, AVIOFlag.AVIO_FLAG_WRITE); avformat.avformat_write_header(outputFormatContext, null); } public void PushFrame(byte[] data) { if (videoBuffer == null) { videoBuffer = new byte[avutil.av_image_get_buffer_size(videoCodecContext->pix_fmt, videoCodecContext->width, videoCodecContext->height, 32)]; videoBufferSize = videoBuffer.Length; } fixed (byte* src = data) { avutil.av_image_fill_arrays(videoFrame->data, videoFrame->linesize, src, videoCodecContext->pix_fmt, videoCodecContext->width, videoCodecContext->height, 32); } videoFrame->pts = videoFrameCount++; avcodec.avcodec_send_frame(videoCodecContext, videoFrame); while (avcodec.avcodec_receive_packet(videoCodecContext, videoPacket) == 0) { videoPacket->stream_index = videoStream->index; avformat.av_write_frame(outputFormatContext, videoPacket); avutil.av_packet_unref(videoPacket); } } public void Close() { avcodec.avcodec_send_frame(videoCodecContext, null); while (avcodec.avcodec_receive_packet(videoCodecContext, videoPacket) == 0) { videoPacket->stream_index = videoStream->index; avformat.av_write_frame(outputFormatContext, videoPacket); avutil.av_packet_unref(videoPacket); } av_write_trailer(outputFormatContext); avio.avio_close(outputFormatContext->pb); avcodec.avcodec_free_context(&videoCodecContext); avformat.avformat_free_context(outputFormatContext); av_frame_free(&videoFrame); av_packet_free(&videoPacket); videoBuffer = null; videoBufferSize = 0; videoFrameCount = 0; } } } ``` 在代码中,我们使用 FFmpeg 库的 API 来创建一个输出流,打开视频流,并将 H264 数据推送到输出流中。具体实现细节请参考代码中的注释。在 WPF 应用程序中,你可以使用该类来推送 H264 视频流
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值