tongg 7 months ago
commit c9ac730545
  1. 36
      Views/UserControl/ViewportData/Helper/VideoHelper.cs

@ -10,6 +10,7 @@ using System.Windows.Media.Imaging;
using System.Drawing; using System.Drawing;
using System.Windows; using System.Windows;
using BrilliantSightClient.Model.Attributes; using BrilliantSightClient.Model.Attributes;
using BrilliantSightClient.Model.Extension;
namespace BrilliantSightClient.Views.UserControl.ViewportData.Helper; namespace BrilliantSightClient.Views.UserControl.ViewportData.Helper;
@ -34,10 +35,10 @@ public class VideoHelper
} }
// Use FFmpeg to create a video from the PNG frames // Use FFmpeg to create a video from the PNG frames
string ffmpegPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ffmpeg.exe"); // Assuming ffmpeg.exe is in the same directory as the application string ffmpegPath = GetFFmpegPath();
if (!File.Exists(ffmpegPath)) if (ffmpegPath.IsNullOrEmpty())
{ {
throw new FileNotFoundException("FFmpeg executable not found.", ffmpegPath); throw new FileNotFoundException("FFmpeg executable not found.");
} }
string framePattern = Path.Combine(tempDirectory, "frame_%05d.png").Replace("\\", "/"); // Replace backslashes with forward slashes for FFmpeg compatibility string framePattern = Path.Combine(tempDirectory, "frame_%05d.png").Replace("\\", "/"); // Replace backslashes with forward slashes for FFmpeg compatibility
@ -101,11 +102,11 @@ public class VideoHelper
} }
// Use FFmpeg to create a video from the PNG frames // Use FFmpeg to create a video from the PNG frames
string ffmpegPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ffmpeg.exe"); // Assuming ffmpeg.exe is in the same directory as the application string ffmpegPath = GetFFmpegPath();
if (!File.Exists(ffmpegPath)) if (ffmpegPath.IsNullOrEmpty())
{ {
// throw new FileNotFoundException("", ffmpegPath); // throw new FileNotFoundException("", ffmpegPath);
MessageBox.Show("FFmpeg executable not found.", ffmpegPath); MessageBox.Show("FFmpeg executable not found.");
} }
string framePattern = Path.Combine(tempDirectory, "frame_%05d.png").Replace("\\", "/"); // Replace backslashes with forward slashes for FFmpeg compatibility string framePattern = Path.Combine(tempDirectory, "frame_%05d.png").Replace("\\", "/"); // Replace backslashes with forward slashes for FFmpeg compatibility
@ -228,4 +229,27 @@ public class VideoHelper
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
return encoder; return encoder;
} }
/// <summary>
/// 多级查找FFmpeg可执行文件路径
/// </summary>
/// <returns>存在的FFmpeg路径,不存在返回null</returns>
private static string GetFFmpegPath()
{
// 优先检查应用程序基目录
string baseDirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ffmpeg.exe");
if (File.Exists(baseDirPath))
{
return baseDirPath;
}
// 检查系统目录(C:\Windows\System32)
string system32Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "ffmpeg.exe");
if (File.Exists(system32Path))
{
return system32Path;
}
return string.Empty;
}
} }
Loading…
Cancel
Save