ffmpeg - 如何使用 GPU 使用 ffmpeg.autogen 解码 MP4 文件?
问题描述
如何使用 GPU 解码 MP4 文件?
我使用 FFmpeg.AutoGen 的演示。对代码有好处: private static unsafe void DecodeAllFramesToImages() 但这是使用 CPU 解码。我想要一个使用 GPU 解码的演示。我怎样才能做到这一点 ?
这是 FFmpeg.AutoGen 演示:
private static unsafe void DecodeAllFramesToImages()
{
// decode all frames from url, please not it might local resorce, e.g. string url = "../../sample_mpeg4.mp4";
var url = "https://rtmp-luzhi.oss-cn-beijing.aliyuncs.com/eryuan2019/%E5%84%BF%E7%AB%A5%E6%89%8B%E8%B6%B3%E5%8F%A3%E7%97%85%E7%9A%84%E9%98%B2%E6%AD%A2%E4%B8%8E%E6%B2%BB%E7%96%97.mp4"; // be advised this file holds 1440 frames
using (var vsd = new VideoStreamDecoder(url))
{
Console.WriteLine($"codec name: {vsd.CodecName}");
var info = vsd.GetContextInfo();
info.ToList().ForEach(x => Console.WriteLine($"{x.Key} = {x.Value}"));
var sourceSize = vsd.FrameSize;
var sourcePixelFormat = vsd.PixelFormat;
var destinationSize = sourceSize;
var destinationPixelFormat = AVPixelFormat.AV_PIX_FMT_BGR24;
using (var vfc = new VideoFrameConverter(sourceSize, sourcePixelFormat, destinationSize, destinationPixelFormat))
{
var frameNumber = 0;
while (vsd.TryDecodeNextFrame(out var frame))
{
var convertedFrame = vfc.Convert(frame);
using (var bitmap = new Bitmap(convertedFrame.width, convertedFrame.height, convertedFrame.linesize[0], PixelFormat.Format24bppRgb, (IntPtr) convertedFrame.data[0]))
bitmap.Save($"frame.{frameNumber:D8}.jpg", ImageFormat.Jpeg);
Console.WriteLine($"frame: {frameNumber}");
frameNumber++;
}
}
}
}
解决方案
您需要更改 VideoStreamDecoder 类:
首先,您需要AVCodecContext
使用您机器的 HWDevice 来配置您的设备。像这样的东西:
AVCodecHWConfig* config = ffmpeg.avcodec_get_hw_config(codec, 0);
ffmpeg.av_hwdevice_ctx_create(&pCodecContext->hw_device_ctx, HWType, null, null, 0);
这HWType
是您的硬件解码器类型。
然后,当您打开上下文并开始解码帧时,您需要从硬件设备获取解码帧:
ffmpeg.av_hwframe_transfer_data(cpuFrame, pFrame, 0)
pFrame
- 从硬件硬件解码器接收的帧
cpuFrame
- 数据复制到的帧。
更新:
现在在FFMPEG.AutoGet 存储库的示例中,您可以使用一些硬件解码器。
推荐阅读
- c# - ASP.Net 找不到其他项目的视图
- php - 我如何在 mod_rewrite 引擎的正则表达式中允许空间
- bash - 在bash中初始化多个变量
- linux - glibc configure 无法识别 Linux 头文件
- angular - 单击时Angular + gsap3动画不起作用
- java - 如何在 Spring Boot 中发送 400、401、405、403 和 500 错误的自定义响应?
- php - 如何使用 .htaccess 或其他方式从网站 url 中删除 .php 扩展名
- php - 如何删除产品页面链接?woocommerce
- javascript - jquery代码中的问题在哪里。它执行警报两次
- android - TextView 无法在 AutoCompleteTextView DropDownListVIew 中更改行