diff --git a/Code/IO/IOUtils.cs b/Code/IO/IOUtils.cs index dafeeac..2463d03 100644 --- a/Code/IO/IOUtils.cs +++ b/Code/IO/IOUtils.cs @@ -309,11 +309,14 @@ namespace Flowframes.IO public static async Task GetVideoFramerate (string path) { + string[] preferFfmpegReadoutFormats = new string[] { ".gif", ".png", ".apng", ".webp" }; + bool preferFfmpegReadout = preferFfmpegReadoutFormats.Contains(Path.GetExtension(path).ToLower()); + Fraction fps = new Fraction(); try { - fps = await FfmpegCommands.GetFramerate(path); + fps = await FfmpegCommands.GetFramerate(path, preferFfmpegReadout); Logger.Log("Detected FPS of " + Path.GetFileName(path) + " as " + fps + " FPS", true); } catch diff --git a/Code/Media/FfmpegCommands.cs b/Code/Media/FfmpegCommands.cs index 876d9c2..2d36a5a 100644 --- a/Code/Media/FfmpegCommands.cs +++ b/Code/Media/FfmpegCommands.cs @@ -80,26 +80,27 @@ namespace Flowframes return FormatUtils.TimestampToMs(output); } - public static async Task GetFramerate(string inputFile) + public static async Task GetFramerate(string inputFile, bool preferFfmpeg = false) { - Logger.Log($"GetFramerate('{inputFile}')", true, false, "ffmpeg"); + Logger.Log($"GetFramerate(inputFile = '{inputFile}', preferFfmpeg = {preferFfmpeg})", true, false, "ffmpeg"); + Fraction ffprobeFps = new Fraction(0, 1); + Fraction ffmpegFps = new Fraction(0, 1); try { - try - { - string ffprobeArgs = $"-v panic -select_streams v:0 -show_entries stream=r_frame_rate {inputFile.Wrap()}"; - string ffprobeOutput = GetFfprobeOutput(ffprobeArgs); - string fpsStr = ffprobeOutput.SplitIntoLines().Where(x => x.Contains("r_frame_rate")).First(); - string[] numbers = fpsStr.Split('=')[1].Split('/'); - Logger.Log($"Fractional FPS from ffprobe: {numbers[0]}/{numbers[1]} = {((float)numbers[0].GetInt() / numbers[1].GetInt())}", true, false, "ffmpeg"); - return new Fraction(numbers[0].GetInt(), numbers[1].GetInt()); - } - catch (Exception ffprobeEx) - { - Logger.Log("GetFramerate ffprobe Error: " + ffprobeEx.Message, true, false); - } + string ffprobeOutput = await GetVideoInfoCached.GetFfprobeInfoAsync(inputFile, "r_frame_rate"); + string fpsStr = ffprobeOutput.SplitIntoLines().First(); + string[] numbers = fpsStr.Split('=')[1].Split('/'); + Logger.Log($"Fractional FPS from ffprobe: {numbers[0]}/{numbers[1]} = {((float)numbers[0].GetInt() / numbers[1].GetInt())}", true, false, "ffmpeg"); + ffprobeFps = new Fraction(numbers[0].GetInt(), numbers[1].GetInt()); + } + catch (Exception ffprobeEx) + { + Logger.Log("GetFramerate ffprobe Error: " + ffprobeEx.Message, true, false); + } + try + { string ffmpegOutput = await GetVideoInfoCached.GetFfmpegInfoAsync(inputFile); string[] entries = ffmpegOutput.Split(','); @@ -108,9 +109,7 @@ namespace Flowframes if (entry.Contains(" fps") && !entry.Contains("Input ")) // Avoid reading FPS from the filename, in case filename contains "fps" { string num = entry.Replace(" fps", "").Trim().Replace(",", "."); - float value; - float.TryParse(num, NumberStyles.Any, CultureInfo.InvariantCulture, out value); - return new Fraction(value); + ffmpegFps = new Fraction(num.GetFloat()); } } } @@ -118,8 +117,21 @@ namespace Flowframes { Logger.Log("GetFramerate ffmpeg Error: " + ffmpegEx.Message, true, false); } - - return new Fraction(0, 1); + + if(preferFfmpeg) + { + if (ffmpegFps.GetFloat() > 0) + return ffmpegFps; + else + return ffprobeFps; + } + else + { + if (ffprobeFps.GetFloat() > 0) + return ffprobeFps; + else + return ffmpegFps; + } } public static Size GetSize(string inputFile) diff --git a/Code/Media/GetMediaResolutionCached.cs b/Code/Media/GetMediaResolutionCached.cs index 38f1258..0e7c677 100644 --- a/Code/Media/GetMediaResolutionCached.cs +++ b/Code/Media/GetMediaResolutionCached.cs @@ -29,7 +29,6 @@ namespace Flowframes.Media } Size size; - size = await IOUtils.GetVideoOrFramesRes(path); Logger.Log($"Adding hash with value {size} to cache.", true);