使用AddictedCS SoundFingerprinting遇System.NullReferenceException排查求助
FFmpeg.AutoGen 空引用异常排查指南
问题概述
在Visual Studio 2022的X64控制台项目中,结合使用SoundFingerprinting与FFmpeg.AutoGen时,FFmpeg.AutoGen.Abstractions抛出System.NullReferenceException,触发点位于ffmpeg.avformat_alloc_context()方法(源码第2906行),异常信息显示vectors对象未实例化。已确认FFmpeg 4.4.1二进制文件放置在指定路径。
异常详情
System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object Source=FFmpeg.AutoGen.Abstractions StackTrace: at FFmpeg.AutoGen.Abstractions.ffmpeg.avformat_alloc_context() in C:\FFmpeg.AutoGen-master\FFmpeg.AutoGen.Abstractions\generated\ffmpeg.functions.facade.g.cs:line 2906
用户代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SoundFingerprinting; using SoundFingerprinting.Builder; using SoundFingerprinting.Emy; using FFmpeg.AutoGen.Bindings.DynamicallyLoaded; using System.IO; namespace ConsoleApp1 { internal class Program { private readonly IFingerprintCommandBuilder fingerprintCommandBuilder = new FingerprintCommandBuilder(); static async Task Main(string[] args) { var current = Environment.CurrentDirectory; var probe = Path.Combine("FFmpeg", "bin", Environment.Is64BitProcess ? "x64" : "x86"); var ffmpegBinaryPath = Path.Combine(current, probe); Console.WriteLine(Environment.CurrentDirectory); Console.WriteLine($"FFmpeg binaries found in: {ffmpegBinaryPath}"); DynamicallyLoadedBindings.LibrariesPath = ffmpegBinaryPath; var audioService = new FFmpegAudioService(); var hashedFingerprints = await FingerprintCommandBuilder.Instance .BuildFingerprintCommand() .From("c:\\temp\\chopin_short2.wav") .UsingServices(audioService) .Hash(); } } }
排查步骤
- 检查FFmpeg库文件完整性:仅放置
ffmpeg.exe不够,需确保ffmpegBinaryPath目录下包含FFmpeg核心动态链接库(如avformat-58.dll、avcodec-58.dll、avutil-56.dll等),这些是FFmpeg.AutoGen绑定必需的文件,缺失会导致加载失败。 - 验证版本兼容性:确认FFmpeg.AutoGen版本与FFmpeg 4.4.1匹配,不同版本的FFmpeg.AutoGen对FFmpeg版本有对应要求,版本不兼容会引发绑定异常。
- 调整绑定初始化时机:将
DynamicallyLoadedBindings.LibrariesPath的设置移至Main方法最开头,确保在任何FFmpeg相关操作前完成路径配置,避免提前触发未初始化的绑定逻辑。 - 手动触发FFmpeg初始化:在设置
LibrariesPath后添加以下代码,强制加载FFmpeg库并完成绑定:FFmpeg.AutoGen.ffmpeg.RootPath = ffmpegBinaryPath; FFmpeg.AutoGen.ffmpeg.Initialize(); - 确认文件复制到输出目录:将FFmpeg的所有
.dll文件复制到项目bin\x64\Debug目录,或在项目中添加这些文件并设置“复制到输出目录”为“始终复制”,确保程序运行时能找到库文件。 - 检查平台一致性:确保项目平台目标为x64,且FFmpeg库也是x64版本,平台不匹配会导致库加载失败。
关于控制台项目支持
SoundFingerprinting和FFmpeg.AutoGen完全支持C#控制台项目,当前问题并非项目类型导致,而是FFmpeg库加载或绑定环节出现异常。
内容的提问来源于stack exchange,提问作者J D
相关产品推荐
相关产品推荐

