如何在.NET 4.5控制台项目中安装.NET Standard的FFMpegCore NuGet包?
解决NU1202兼容性错误与PNG流转视频实现
一、修复NU1202包兼容性问题
FFMpegCore 4.8.0仅支持.NET Standard 2.0,而.NET Framework 4.5并不兼容.NET Standard 2.0(最低兼容版本是.NET Framework 4.6.1),提供两种解决路径:
方案1:升级项目目标框架
将你的控制台项目目标框架修改为NET Framework 4.6.1或更高版本,修改完成后即可正常安装FFMpegCore 4.8.0。
方案2:安装兼容.NET 4.5的FFMpegCore旧版本
选择FFMpegCore历史版本中最后一个支持.NET Framework 4.5的版本(如3.x系列),通过NuGet命令安装:Install-Package FFMpegCore -Version 3.0.0
也可在NuGet包管理器中手动选择对应版本。
二、PNG图片流转视频实现
无论采用哪种方案,核心都是通过FFMpegCore的管道输入功能,将PNG流传入FFmpeg进行视频编码。
前置准备
确保系统已安装FFmpeg,或通过NuGet安装对应平台的FFmpeg二进制包(比如Windows平台安装FFMpegCore.Binaries.Windows),让FFMpegCore能定位到FFmpeg可执行文件。
代码示例(.NET 4.6.1+、FFMpegCore 4.8.0)
单张PNG流转视频
using FFMpegCore; using FFMpegCore.Pipes; using System.IO; // 替换为你的实际PNG流 Stream pngStream = new FileStream("input.png", FileMode.Open); string outputVideoPath = "output_single_frame.mp4"; FFMpegArguments .FromPipeInput(new StreamPipeSource(pngStream) { Format = "png" }) .WithVideoCodec("libx264") // 使用H.264编码 .WithConstantRateFactor(23) // 视频质量参数,值越小质量越高 .OutputToFile(outputVideoPath, overwrite: true) .ProcessSynchronously();
多张PNG序列流转视频
using FFMpegCore; using FFMpegCore.Pipes; using System.Collections.Generic; using System.IO; // 替换为你的实际PNG流集合 List<Stream> pngStreams = new List<Stream> { new FileStream("frame1.png", FileMode.Open), new FileStream("frame2.png", FileMode.Open), new FileStream("frame3.png", FileMode.Open) }; string outputVideoPath = "output_sequence.mp4"; // 将多个PNG流包装为串联管道源 var pipeSources = pngStreams.Select(s => new StreamPipeSource(s) { Format = "png" }); var concatenateSource = new ConcatenatePipeSource(pipeSources); FFMpegArguments .FromPipeInput(concatenateSource) .WithVideoCodec("libx264") .WithFramerate(25) // 设置视频帧率,如每秒25帧 .WithConstantRateFactor(23) .OutputToFile(outputVideoPath, overwrite: true) .ProcessSynchronously();
旧版本FFMpegCore(兼容.NET 4.5)实现思路
旧版本API略有差异,但核心逻辑一致:
- 先配置FFmpeg路径:
GlobalFFOptions.Configure(opt => opt.BinaryFolder = "你的FFmpeg二进制文件目录"); - 创建管道输入对象,将PNG流传入FFmpeg标准输入
- 配置视频编码、帧率等参数,执行转换命令
内容的提问来源于stack exchange,提问作者Arjun Natarajan
相关产品推荐
相关产品推荐

