浏览代码

Initial check-in of VideoInfoProvider, although it's currently disabled.

LukePulverenti Luke Pulverenti luke pulverenti 13 年之前
父节点
当前提交
aae259d2cd

+ 3 - 3
MediaBrowser.Api/HttpHandlers/VideoHandler.cs

@@ -279,15 +279,15 @@ namespace MediaBrowser.Api.HttpHandlers
                 }
             }
 
-            if (audio.Format.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
+            if (audio.Codec.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
             {
                 return false;
             }
-            if (audio.Format.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.Format.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
+            if (audio.Codec.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
             {
                 return false;
             }
-            if (audio.Format.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.Format.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
+            if (audio.Codec.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
             {
                 return false;
             }

+ 22 - 0
MediaBrowser.Controller/Configuration/ServerApplicationPaths.cs

@@ -193,6 +193,28 @@ namespace MediaBrowser.Controller.Configuration
                 return _FFProbeAudioCacheDirectory;
             }
         }
+
+        private string _FFProbeVideoCacheDirectory = null;
+        /// <summary>
+        /// Gets the folder path to the ffprobe video cache directory
+        /// </summary>
+        public string FFProbeVideoCacheDirectory
+        {
+            get
+            {
+                if (_FFProbeVideoCacheDirectory == null)
+                {
+                    _FFProbeVideoCacheDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.CacheDirectory, "ffprobe-video");
+
+                    if (!Directory.Exists(_FFProbeVideoCacheDirectory))
+                    {
+                        Directory.CreateDirectory(_FFProbeVideoCacheDirectory);
+                    }
+                }
+
+                return _FFProbeVideoCacheDirectory;
+            }
+        }
         
         private string _FFMpegDirectory = null;
         /// <summary>

+ 22 - 0
MediaBrowser.Controller/FFMpeg/FFProbe.cs

@@ -35,6 +35,28 @@ namespace MediaBrowser.Controller.FFMpeg
             }
         }
 
+        public async static Task<FFProbeResult> Run(Video item, string outputCachePath)
+        {
+            // Use try catch to avoid having to use File.Exists
+            try
+            {
+                using (FileStream stream = File.OpenRead(outputCachePath))
+                {
+                    return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
+                }
+            }
+            catch (FileNotFoundException)
+            {
+            }
+
+            await Run(item.Path, outputCachePath);
+
+            using (FileStream stream = File.OpenRead(outputCachePath))
+            {
+                return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
+            }
+        }
+
         private async static Task Run(string input, string output)
         {
             ProcessStartInfo startInfo = new ProcessStartInfo();

+ 10 - 0
MediaBrowser.Controller/Kernel.cs

@@ -252,6 +252,16 @@ namespace MediaBrowser.Controller
                     );
             }
 
+            // Third priority providers
+            providers = supportedProviders.Where(i => !i.RequiresInternet && i.Priority == MetadataProviderPriority.Third);
+
+            if (providers.Any())
+            {
+                await Task.WhenAll(
+                    providers.Select(i => i.Fetch(item, args))
+                    );
+            }
+            
             // Lowest priority providers
             providers = supportedProviders.Where(i => !i.RequiresInternet && i.Priority == MetadataProviderPriority.Last);
 

+ 1 - 0
MediaBrowser.Controller/MediaBrowser.Controller.csproj

@@ -64,6 +64,7 @@
     <Compile Include="Providers\FolderProviderFromXml.cs" />
     <Compile Include="Providers\ImageFromMediaLocationProvider.cs" />
     <Compile Include="Providers\LocalTrailerProvider.cs" />
+    <Compile Include="Providers\VideoInfoProvider.cs" />
     <Compile Include="Resolvers\AudioResolver.cs" />
     <Compile Include="Resolvers\BaseItemResolver.cs" />
     <Compile Include="Resolvers\FolderResolver.cs" />

+ 13 - 8
MediaBrowser.Controller/Providers/AudioInfoProvider.cs

@@ -229,21 +229,26 @@ namespace MediaBrowser.Controller.Providers
         {
             base.Init();
 
+            EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory);
+        }
+
+        internal static void EnsureCacheSubFolders(string root)
+        {
             // Do this now so that we don't have to do this on every operation, which would require us to create a lock in order to maintain thread-safety
             for (int i = 0; i <= 9; i++)
             {
-                EnsureDirectory(Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, i.ToString()));
+                EnsureDirectory(Path.Combine(root, i.ToString()));
             }
 
-            EnsureDirectory(Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, "a"));
-            EnsureDirectory(Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, "b"));
-            EnsureDirectory(Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, "c"));
-            EnsureDirectory(Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, "d"));
-            EnsureDirectory(Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, "e"));
-            EnsureDirectory(Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, "f"));
+            EnsureDirectory(Path.Combine(root, "a"));
+            EnsureDirectory(Path.Combine(root, "b"));
+            EnsureDirectory(Path.Combine(root, "c"));
+            EnsureDirectory(Path.Combine(root, "d"));
+            EnsureDirectory(Path.Combine(root, "e"));
+            EnsureDirectory(Path.Combine(root, "f"));
         }
 
-        private void EnsureDirectory(string path)
+        private static void EnsureDirectory(string path)
         {
             if (!Directory.Exists(path))
             {

+ 3 - 0
MediaBrowser.Controller/Providers/BaseMetadataProvider.cs

@@ -47,6 +47,9 @@ namespace MediaBrowser.Controller.Providers
         // Run this provider after all first priority providers
         Second,
 
+        // Run this provider after all second priority providers
+        Third,
+
         // Run this provider last
         Last
     }

+ 89 - 0
MediaBrowser.Controller/Providers/VideoInfoProvider.cs

@@ -0,0 +1,89 @@
+using System.ComponentModel.Composition;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using MediaBrowser.Controller.Events;
+using MediaBrowser.Controller.FFMpeg;
+using MediaBrowser.Model.Entities;
+
+namespace MediaBrowser.Controller.Providers
+{
+    //[Export(typeof(BaseMetadataProvider))]
+    public class VideoInfoProvider : BaseMetadataProvider
+    {
+        public override bool Supports(BaseEntity item)
+        {
+            return item is Video;
+        }
+
+        public override MetadataProviderPriority Priority
+        {
+            // Give this second priority
+            // Give metadata xml providers a chance to fill in data first
+            // Then we can skip this step whenever possible
+            get { return MetadataProviderPriority.Second; }
+        }
+
+        public override async Task Fetch(BaseEntity item, ItemResolveEventArgs args)
+        {
+            Video video = item as Video;
+
+            if (video.VideoType != VideoType.VideoFile)
+            {
+                // Not supported yet
+                return;
+            }
+
+            if (CanSkip(video))
+            {
+                return;
+            }
+
+            string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory, item.Id.ToString().Substring(0, 1));
+
+            string outputPath = Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
+
+            FFProbeResult data = await FFProbe.Run(video, outputPath);
+        }
+
+        /// <summary>
+        /// Determines if there's already enough info in the Video object to allow us to skip running ffprobe
+        /// </summary>
+        private bool CanSkip(Video video)
+        {
+            if (video.AudioStreams == null || !video.AudioStreams.Any())
+            {
+                return false;
+            }
+
+            if (string.IsNullOrEmpty(video.Codec))
+            {
+                return false;
+            }
+
+            if (string.IsNullOrEmpty(video.ScanType))
+            {
+                return false;
+            }
+
+            if (string.IsNullOrEmpty(video.FrameRate))
+            {
+                return false;
+            }
+
+            if (video.Height == 0 || video.Width == 0 || video.BitRate == 0)
+            {
+                return false;
+            }
+            
+            return true;
+        }
+
+        public override void Init()
+        {
+            base.Init();
+
+            AudioInfoProvider.EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory);
+        }
+    }
+}

+ 2 - 47
MediaBrowser.Controller/Xml/BaseItemXmlParser.cs

@@ -378,53 +378,8 @@ namespace MediaBrowser.Controller.Xml
                             break;
 
                         case "Codec":
-                            {
-                                string codec = await reader.ReadElementContentAsStringAsync();
-
-                                switch (codec.ToLower())
-                                {
-                                    case "dts-es":
-                                    case "dts-es matrix":
-                                    case "dts-es discrete":
-                                        stream.Format = "DTS";
-                                        stream.Profile = "ES";
-                                        break;
-                                    case "dts-hd hra":
-                                    case "dts-hd high resolution":
-                                        stream.Format = "DTS";
-                                        stream.Profile = "HRA";
-                                        break;
-                                    case "dts ma":
-                                    case "dts-hd ma":
-                                    case "dts-hd master":
-                                        stream.Format = "DTS";
-                                        stream.Profile = "MA";
-                                        break;
-                                    case "dolby digital":
-                                    case "dolby digital surround ex":
-                                    case "dolby surround":
-                                        stream.Format = "AC-3";
-                                        break;
-                                    case "dolby digital plus":
-                                        stream.Format = "E-AC-3";
-                                        break;
-                                    case "dolby truehd":
-                                        stream.Format = "AC-3";
-                                        stream.Profile = "TrueHD";
-                                        break;
-                                    case "mp2":
-                                        stream.Format = "MPEG Audio";
-                                        stream.Profile = "Layer 2";
-                                        break;
-                                    case "other":
-                                        break;
-                                    default:
-                                        stream.Format = codec;
-                                        break;
-                                }
-
-                                break;
-                            }
+                            stream.Codec = await reader.ReadElementContentAsStringAsync();
+                            break;
 
                         default:
                             await reader.SkipAsync();

+ 1 - 2
MediaBrowser.Model/Entities/Video.cs

@@ -19,8 +19,7 @@ namespace MediaBrowser.Model.Entities
 
     public class AudioStream
     {
-        public string Format { get; set; }
-        public string Profile { get; set; }
+        public string Codec { get; set; }
         public string Language { get; set; }
         public int BitRate { get; set; }
         public int Channels { get; set; }