WPF应用调用FlyleafLib的Engine.Start时FFmpeg库加载失败求助
问题:FlyleafLib初始化失败,无法加载FFmpeg库
我正在开发一款WPF应用,选用FlyleafLib实现视频播放功能。已通过NuGet安装FlyleafLib,并将FFmpeg共享构建包(包含所需.dll文件)放置在C:\ffmpeg路径下。启动应用时直接崩溃,用try-catch捕获到如下异常:
严重错误: 加载FFmpeg库'C:\ffmpeg'失败
堆栈跟踪: at FlyleafLib.FFmpegEngine..ctor() at FlyleafLib.Engine.StartInternalNonUI()
相关代码文件
App.xaml.cs
using System; using System.IO; using System.Windows; using FlyleafLib; namespace NUR { public partial class App : System.Windows.Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); try { Engine.Start(new EngineConfig() { FFmpegPath = @"C:\ffmpeg", #if RELEASE FFmpegLogLevel = Flyleaf.FFmpeg.LogLevel.Quiet, LogLevel = LogLevel.Quiet, #else FFmpegLogLevel = Flyleaf.FFmpeg.LogLevel.Warn, LogLevel = LogLevel.Debug, LogOutput = ":debug", //LogOutput = ":console", //LogOutput = @"C:\Flyleaf\Logs\flyleaf.log", #endif //PluginsPath = @"C:\Flyleaf\Plugins", UIRefresh = false, // Required for Activity, BufferedDuration, Stats in combination with Config.Player.Stats = true UIRefreshInterval = 250, // How often (in ms) to notify the UI }); } catch (Exception ex) { System.Windows.MessageBox.Show( $"Критическая ошибка инициализации Flyleaf:\n\n" + $"Сообщение: {ex.Message}\n\n" + $"Внутренняя ошибка: {ex.InnerException?.Message}\n\n" + $"Стек вызовов:\n{ex.StackTrace}", "Ошибка запуска", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(); } } } }
VideoForm.xaml.cs
using System; using System.Windows.Controls; using FlyleafLib; using FlyleafLib.MediaPlayer; namespace NUR.Views { public partial class VideoForm : UserControl { public Player Player { get; set; } public Config Config { get; set; } public VideoForm() { Config = new Config(); Player = new Player(Config); InitializeComponent(); DataContext = this; } } }
VideoForm.xaml
<UserControl x:Class="NUR.Views.VideoForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:fl="clr-namespace:FlyleafLib.Controls.WPF;assembly=FlyleafLib" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="744" d:DesignWidth="1200" mc:Ignorable="d"> <Grid Background="Black"> <fl:FlyleafHost Player="{Binding Player}" AttachedDragMove="Both" KeyBindings="Both"> <Viewbox> <TextBlock Text="Hello Flyleaf Overlay!" Foreground="DarkOrange"/> </Viewbox> </fl:FlyleafHost> </Grid> </UserControl>
已尝试的解决步骤
- 确认项目构建目标为x64
- 更换过多个FFmpeg版本(包括旧版4.4及新版)
- 确认
C:\ffmpeg下包含avcodec、avformat等.dll文件,而非仅ffmpeg.exe
进一步排查与解决建议
- 确认FFmpeg位数匹配:必须使用x64版本的FFmpeg共享构建包,32位库无法在64位WPF进程中加载。
- 检查路径权限与位置:
- 验证应用对
C:\ffmpeg有读取权限,若权限不足,可将FFmpeg文件夹复制到项目输出目录(如bin\x64\Debug),并修改FFmpegPath为该目录的绝对路径或相对路径。
- 验证应用对
- 排查依赖缺失:
- 使用Dependency Walker或Process Monitor工具,检查FFmpeg的.dll文件是否存在缺失的依赖组件(如
vcruntime140.dll等VC运行库),确保已安装对应版本的Visual C Redistributable。
- 使用Dependency Walker或Process Monitor工具,检查FFmpeg的.dll文件是否存在缺失的依赖组件(如
- 调整初始化顺序:
- 确保
Engine.Start完全执行完成后再创建Player实例。可将Player的创建移至VideoForm的Loaded事件中,避免控件构造时引擎尚未初始化完成。
- 确保
- 启用详细日志:
- 在Debug模式下,将
LogOutput改为:console,查看FlyleafLib输出的详细日志,定位具体是哪个FFmpeg库文件加载失败。
- 在Debug模式下,将
- 核对版本兼容性:
- 确认FlyleafLib的NuGet版本与FFmpeg版本兼容,部分新版本FlyleafLib可能需要特定版本的FFmpeg支持,可查看NuGet包的描述文档确认兼容范围。
内容的提问来源于stack exchange,提问作者Trevor




