using System; using System.Diagnostics; using System.IO; using UnityEngine; namespace Funique { /// /// Extensions: FFmpeg
/// ------------------------------------------------
/// 擴充: FFmpeg ///
public static class FFmpegExtensions { public static string FFmpeg_StreamingAsset(this string arg) { using (Process process = new Process()) { process.StartInfo.FileName = Path.Combine(Application.streamingAssetsPath, "ffmpeg", "ffmpeg.exe"); process.StartInfo.Arguments = arg; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); string result = process.StandardError.ReadToEnd(); process.Close(); process.Dispose(); return result; } } public static string FFmpeg(this string arg) { using (Process process = new Process()) { process.StartInfo.FileName = "ffmpeg"; process.StartInfo.Arguments = arg; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); string result = process.StandardError.ReadToEnd(); process.Close(); process.Dispose(); return result; } } public static void FFmpegSetVideoInfo(this VideoInfo video, FFmpegPlace place) { string text = Path.Combine(Application.streamingAssetsPath, video.Path, video.FilterName); string ffmpegMsg = string.Empty; switch (place) { case FFmpegPlace.StreamingAsset: ffmpegMsg = FFmpeg_StreamingAsset($" -i \"{text}\""); break; case FFmpegPlace.environmentVariable: ffmpegMsg = FFmpeg($" -i \"{text}\""); break; } try { video.Time = (float)FFmpegDurationTime(ffmpegMsg); video.AudioChannel = FFmpegAudioChannel(ffmpegMsg); } catch (Exception ex) { FuniqueLogger.ErrorLog($"Set ffmpeg video info failed: {text}\n{ex.Message}", "Tool"); } } public static double FFmpegDurationTime(this string ffmpegMsg) { int num = ffmpegMsg.IndexOf("Duration: ") + 10; int length = ffmpegMsg.IndexOf(",", num) - num; string text = string.Empty; if (num > 0 && length > 0) text = ffmpegMsg.Substring(num, length); TimeSpan timeSpan; if (TimeSpan.TryParse(text, out timeSpan)) { return timeSpan.TotalSeconds; } return 0; } public static string FFmpegAudioChannel(this string ffmpegMsg) { string text = string.Empty; int num = ffmpegMsg.LastIndexOf("Stream #0:1"); int length = -1; if (num != -1) length = ffmpegMsg.IndexOf("fltp", num) - num; if (num <= 0 || length <= 0) return null; text = ffmpegMsg.Substring(num, length); num = text.LastIndexOf("Hz, ") + 4; length = text.IndexOf(",", num) - num; if (num == -1 || length == -1) return null; text.Substring(num, length); return text; } public static bool FileSearch(this string sDir, string fileName) { return FileSearch(sDir, fileName, out _); } public static bool FileSearch(this string sDir, string fileName, out string fullpath) { string[] fileSystemEntries = Directory.GetFileSystemEntries(sDir, "*", SearchOption.AllDirectories); foreach (var i in fileSystemEntries) { if (Path.GetFileName(i) == fileName) { fullpath = i; return true; } } fullpath = null; return false; } } }