From 71f51aa23bbbc306bc2f9c5656674f4db0dea50e Mon Sep 17 00:00:00 2001 From: januwA Date: Fri, 25 Sep 2020 09:36:37 +0800 Subject: [PATCH] v3.0.0 --- MainWindow.xaml.cs | 41 +++++++++++++++++++++++++++++++---------- README.md | 4 ++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 24b20e5..0bfc595 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -1,5 +1,5 @@ using System; -using fpath = System.IO.Path; +using io = System.IO; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -28,19 +28,40 @@ public partial class MainWindow : Window public MainWindow() { InitializeComponent(); - this.AllowDrop = true; - ffmpegExe = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe"; - ffplayExe = AppDomain.CurrentDomain.BaseDirectory + "ffplay.exe"; + // 1. 先在命令行PATH中找ffmpeg + // 2. 其次在程序执行目录中ffmpeg + // 3. 都没找到退出 - if(System.IO.File.Exists(ffmpegExe) == false) + ffmpegExe = GetFullPath("ffmpeg.exe"); + ffplayExe = GetFullPath("ffplay.exe"); + + if(ffmpegExe == null) ffmpegExe = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe"; + if (ffplayExe == null) ffplayExe = AppDomain.CurrentDomain.BaseDirectory + "ffplay.exe"; + + if (System.IO.File.Exists(ffmpegExe) == false) { - MessageBox.Show("未找到[ffmpeg.exe]", "文件未找到"); + MessageBox.Show("未找到[ffmpeg.exe],工具将无法使用", "错误",MessageBoxButton.OK, MessageBoxImage.Error); + System.Windows.Application.Current.Shutdown(); } - if (System.IO.File.Exists(ffplayExe) == false) { - MessageBox.Show("未找到[ffplay.exe]", "文件未找到"); + MessageBox.Show("未找到[ffplay.exe],播放功能无法使用", "错误",MessageBoxButton.OK, MessageBoxImage.Error); + } + } + + private string GetFullPath(string fileName) + { + if (io.File.Exists(fileName)) + return io.Path.GetFullPath(fileName); + + var values = Environment.GetEnvironmentVariable("PATH"); + foreach (var path in values.Split(io.Path.PathSeparator)) + { + var fullPath = io.Path.Combine(path, fileName); + if (io.File.Exists(fullPath)) + return fullPath; } + return null; } private void Drop1(object sender, DragEventArgs e) @@ -84,11 +105,11 @@ private void execute(string command) /// private string getOutputFilepath(string extension = "") { - extension = extension.Length != 0 ? extension : fpath.GetExtension(srcFilePath); + extension = extension.Length != 0 ? extension : io.Path.GetExtension(srcFilePath); string time = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString(); // xxx/xxx/[time]-oldname.extension - return fpath.GetDirectoryName(srcFilePath) + "\\[" + time + "]-" + fpath.GetFileNameWithoutExtension(srcFilePath) + extension; + return io.Path.GetDirectoryName(srcFilePath) + "\\[" + time + "]-" + io.Path.GetFileNameWithoutExtension(srcFilePath) + extension; } /// diff --git a/README.md b/README.md index 018a69b..b09106d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ ## 使用FFmpeg命令行处理视频的简单工具 +- [ffmpeg官网](https://ffmpeg.org/) +- [下载ffmpeg](https://github.com/BtbN/FFmpeg-Builds/releases)可执行文件(lgpl.zip),然后将bin目录添加到PATH环境变量,或者将bin下面的exe拷贝到本软件的exe同级目录下 +- 没有找到ffmpeg.exe将无法运行 + ![](./images/2020-09-24-22-49-01.png) \ No newline at end of file