From d794eecec4f4b9a46df422b28c86e136bfd92abf Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Sun, 19 Aug 2012 11:58:35 -0400 Subject: Added initial implementation of the metadata provider network, along with the first few providers --- MediaBrowser.Controller/FFMpeg/FFProbe.cs | 50 +++++++++++++++++++---- MediaBrowser.Controller/FFMpeg/FFProbeResult.cs | 54 ++++++++++--------------- 2 files changed, 63 insertions(+), 41 deletions(-) (limited to 'MediaBrowser.Controller/FFMpeg') diff --git a/MediaBrowser.Controller/FFMpeg/FFProbe.cs b/MediaBrowser.Controller/FFMpeg/FFProbe.cs index efd5491de0..fd9b2ff438 100644 --- a/MediaBrowser.Controller/FFMpeg/FFProbe.cs +++ b/MediaBrowser.Controller/FFMpeg/FFProbe.cs @@ -1,13 +1,41 @@ using System; using System.Diagnostics; +using System.IO; +using System.Threading.Tasks; using MediaBrowser.Common.Logging; using MediaBrowser.Common.Serialization; +using MediaBrowser.Model.Entities; namespace MediaBrowser.Controller.FFMpeg { + /// + /// Runs FFProbe against a media file and returns metadata. + /// public static class FFProbe { - public static FFProbeResult Run(string path) + public async static Task Run(Audio item, string outputCachePath) + { + // Use try catch to avoid having to use File.Exists + try + { + using (FileStream stream = File.OpenRead(outputCachePath)) + { + return JsonSerializer.DeserializeFromStream(stream); + } + } + catch (FileNotFoundException) + { + } + + await Run(item.Path, outputCachePath); + + using (FileStream stream = File.OpenRead(outputCachePath)) + { + return JsonSerializer.DeserializeFromStream(stream); + } + } + + private async static Task Run(string input, string output) { ProcessStartInfo startInfo = new ProcessStartInfo(); @@ -21,13 +49,15 @@ namespace MediaBrowser.Controller.FFMpeg startInfo.FileName = Kernel.Instance.ApplicationPaths.FFProbePath; startInfo.WorkingDirectory = Kernel.Instance.ApplicationPaths.FFMpegDirectory; - startInfo.Arguments = string.Format("\"{0}\" -v quiet -print_format json -show_streams -show_format", path); + startInfo.Arguments = string.Format("\"{0}\" -v quiet -print_format json -show_streams -show_format", input); - Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments); + //Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments); Process process = new Process(); process.StartInfo = startInfo; + FileStream stream = new FileStream(output, FileMode.Create); + try { process.Start(); @@ -36,18 +66,23 @@ namespace MediaBrowser.Controller.FFMpeg // If we ever decide to disable the ffmpeg log then you must uncomment the below line. process.BeginErrorReadLine(); - FFProbeResult result = JsonSerializer.DeserializeFromStream(process.StandardOutput.BaseStream); + await process.StandardOutput.BaseStream.CopyToAsync(stream); process.WaitForExit(); - Logger.LogInfo("FFMpeg exited with code " + process.ExitCode); + stream.Dispose(); - return result; + if (process.ExitCode != 0) + { + Logger.LogInfo("FFProbe exited with code {0} for {1}", process.ExitCode, input); + } } catch (Exception ex) { Logger.LogException(ex); + stream.Dispose(); + // Hate having to do this try { @@ -56,8 +91,7 @@ namespace MediaBrowser.Controller.FFMpeg catch { } - - return null; + File.Delete(output); } finally { diff --git a/MediaBrowser.Controller/FFMpeg/FFProbeResult.cs b/MediaBrowser.Controller/FFMpeg/FFProbeResult.cs index cc449991de..43167b521a 100644 --- a/MediaBrowser.Controller/FFMpeg/FFProbeResult.cs +++ b/MediaBrowser.Controller/FFMpeg/FFProbeResult.cs @@ -13,6 +13,11 @@ namespace MediaBrowser.Controller.FFMpeg public MediaFormat format { get; set; } } + /// + /// Represents a stream within the output + /// A number of properties are commented out to improve deserialization performance + /// Enable them as needed. + /// public class MediaStream { public int index { get; set; } @@ -20,28 +25,28 @@ namespace MediaBrowser.Controller.FFMpeg public string codec_name { get; set; } public string codec_long_name { get; set; } public string codec_type { get; set; } - public string codec_time_base { get; set; } - public string codec_tag { get; set; } - public string codec_tag_string { get; set; } - public string sample_fmt { get; set; } + //public string codec_time_base { get; set; } + //public string codec_tag { get; set; } + //public string codec_tag_string { get; set; } + //public string sample_fmt { get; set; } public string sample_rate { get; set; } public int channels { get; set; } - public int bits_per_sample { get; set; } - public string r_frame_rate { get; set; } - public string avg_frame_rate { get; set; } - public string time_base { get; set; } - public string start_time { get; set; } + //public int bits_per_sample { get; set; } + //public string r_frame_rate { get; set; } + //public string avg_frame_rate { get; set; } + //public string time_base { get; set; } + //public string start_time { get; set; } public string duration { get; set; } public string bit_rate { get; set; } public int width { get; set; } public int height { get; set; } - public int has_b_frames { get; set; } - public string sample_aspect_ratio { get; set; } - public string display_aspect_ratio { get; set; } - public string pix_fmt { get; set; } - public int level { get; set; } - public MediaTags tags { get; set; } + //public int has_b_frames { get; set; } + //public string sample_aspect_ratio { get; set; } + //public string display_aspect_ratio { get; set; } + //public string pix_fmt { get; set; } + //public int level { get; set; } + public Dictionary tags { get; set; } } public class MediaFormat @@ -54,23 +59,6 @@ namespace MediaBrowser.Controller.FFMpeg public string duration { get; set; } public string size { get; set; } public string bit_rate { get; set; } - public MediaTags tags { get; set; } - } - - public class MediaTags - { - public string title { get; set; } - public string comment { get; set; } - public string artist { get; set; } - public string album { get; set; } - public string album_artist { get; set; } - public string composer { get; set; } - public string copyright { get; set; } - public string publisher { get; set; } - public string track { get; set; } - public string disc { get; set; } - public string genre { get; set; } - public string date { get; set; } - public string language { get; set; } + public Dictionary tags { get; set; } } } -- cgit v1.2.3