Ejemplo n.º 1
0
 public static MediaItem CreateMediaItem(string filename)
 {
   IMediaAccessor mediaAccessor = ServiceRegistration.Get<IMediaAccessor>();
   IEnumerable<Guid> meIds = mediaAccessor.GetMetadataExtractorsForMIATypes(NECESSARY_VIDEO_MIAS);
   ResourceLocator resourceLocator = new ResourceLocator(LocalFsResourceProviderBase.ToResourcePath(filename));
   IResourceAccessor ra = resourceLocator.CreateAccessor();
   if (ra == null)
     return null;
   using (ra)
     return mediaAccessor.CreateLocalMediaItem(ra, meIds);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Detects if the directory of the given <paramref name="resourcePath"/> contains video, image or audio media files and
 /// returns <see cref="MediaItem"/> instances for those files.
 /// </summary>
 /// <param name="resourcePath">The resource path instance for the directory to be examined.</param>
 /// <param name="videoMIATypeIds">Ids of the media item aspects to be extracted from video items.</param>
 /// <param name="imageMIATypeIds">Ids of the media item aspects to be extracted from image items.</param>
 /// <param name="audioMIATypeIds">Ids of the media item aspects to be extracted from audio items.</param>
 /// <param name="mediaItems">Returns a collection of media items in the given <paramref name="resourcePath"/> or <c>null</c>,
 /// if the return value is <see cref="MultiMediaType.None"/>.</param>
 /// <returns>Type of media items found.</returns>
 public static MultiMediaType DetectMultimedia(ResourcePath resourcePath,
     IEnumerable<Guid> videoMIATypeIds, IEnumerable<Guid> imageMIATypeIds, IEnumerable<Guid> audioMIATypeIds,
     out ICollection<MediaItem> mediaItems)
 {
   try
   {
     mediaItems = new List<MediaItem>();
     IMediaAccessor mediaAccessor = ServiceRegistration.Get<IMediaAccessor>();
     IEnumerable<Guid> meIds = mediaAccessor.GetMetadataExtractorsForMIATypes(videoMIATypeIds.Union(imageMIATypeIds).Union(audioMIATypeIds));
     using (IResourceAccessor ra = new ResourceLocator(resourcePath).CreateAccessor())
     {
       IFileSystemResourceAccessor directoryRA = ra as IFileSystemResourceAccessor;
       if (ra != null)
         AddLocalMediaItemsRecursive(directoryRA, mediaItems, meIds, mediaAccessor);
     }
     MultiMediaType result = MultiMediaType.None;
     foreach (MediaItem item in mediaItems)
     { // Check the type of our extracted media items
       MultiMediaType itemType = GetTypeOfMediaItem(item);
       if (result == MultiMediaType.None) // Initialize the result type with the type of the first media item
         result = itemType;
       else if (result != itemType) // Check if we have different item types
       {
         result = MultiMediaType.Diverse;
         break;
       }
     }
     return result;
   }
   catch (Exception e)
   {
     ServiceRegistration.Get<ILogger>().Warn("Error while detecting the media items in resource {0}", e, resourcePath);
     mediaItems = null;
     return MultiMediaType.None;
   }
 }
Ejemplo n.º 3
0
    /// <summary>
    /// Gets a list of <see cref="FanArtImage"/>s for a requested <paramref name="mediaType"/>, <paramref name="fanArtType"/> and <paramref name="name"/>.
    /// The name can be: Series name, Actor name, Artist name depending on the <paramref name="mediaType"/>.
    /// </summary>
    /// <param name="mediaType">Requested FanArtMediaType</param>
    /// <param name="fanArtType">Requested FanArtType</param>
    /// <param name="name">Requested name of Series, Actor, Artist...</param>
    /// <param name="maxWidth">Maximum width for image. <c>0</c> returns image in original size.</param>
    /// <param name="maxHeight">Maximum height for image. <c>0</c> returns image in original size.</param>
    /// <param name="singleRandom">If <c>true</c> only one random image URI will be returned</param>
    /// <param name="result">Result if return code is <c>true</c>.</param>
    /// <returns><c>true</c> if at least one match was found.</returns>
    public bool TryGetFanArt(string mediaType, string fanArtType, string name, int maxWidth, int maxHeight, bool singleRandom, out IList<IResourceLocator> result)
    {
      result = null;
      Guid mediaItemId;

      // Don't try to load "fanart" for images
      if (!Guid.TryParse(name, out mediaItemId) || mediaType == FanArtMediaTypes.Image)
        return false;

      IMediaLibrary mediaLibrary = ServiceRegistration.Get<IMediaLibrary>(false);
      if (mediaLibrary == null)
        return false;

      IFilter filter = new MediaItemIdFilter(mediaItemId);
      IList<MediaItem> items = mediaLibrary.Search(new MediaItemQuery(NECESSARY_MIAS, filter), false);
      if (items == null || items.Count == 0)
        return false;

      MediaItem mediaItem = items.First();
      var mediaIteamLocator = mediaItem.GetResourceLocator();
      var fanArtPaths = new List<ResourcePath>();
      var files = new List<IResourceLocator>();
      // File based access
      try
      {
        var mediaItemPath = mediaIteamLocator.NativeResourcePath;
        var mediaItemDirectoryPath = ResourcePathHelper.Combine(mediaItemPath, "../");
        var mediaItemFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(mediaItemPath.ToString());
        var mediaItemExtension = ResourcePathHelper.GetExtension(mediaItemPath.ToString());

        using (var directoryRa = new ResourceLocator(mediaIteamLocator.NativeSystemId, mediaItemDirectoryPath).CreateAccessor())
        {
          var directoryFsra = directoryRa as IFileSystemResourceAccessor;
          if (directoryFsra != null)
          {
            var potentialFanArtFiles = GetPotentialFanArtFiles(directoryFsra);

            if (fanArtType == FanArtTypes.Poster || fanArtType == FanArtTypes.Thumbnail)
              fanArtPaths.AddRange(
                from potentialFanArtFile in potentialFanArtFiles
                let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString())
                where /* Allow same file name only for non-images, otherwise each image would be its own thumbnail */
                      potentialFanArtFileNameWithoutExtension == mediaItemFileNameWithoutExtension && !EXTENSIONS.Contains(mediaItemExtension) || 
                      potentialFanArtFileNameWithoutExtension == mediaItemFileNameWithoutExtension + "-poster" ||
                      potentialFanArtFileNameWithoutExtension == "folder"
                select potentialFanArtFile);

            if (fanArtType == FanArtTypes.FanArt)
            {
              fanArtPaths.AddRange(
                from potentialFanArtFile in potentialFanArtFiles
                let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString())
                where potentialFanArtFileNameWithoutExtension == "backdrop" ||
                      potentialFanArtFileNameWithoutExtension.StartsWith(mediaItemFileNameWithoutExtension + "-fanart")
                select potentialFanArtFile);
              if (directoryFsra.ResourceExists("ExtraFanArt/"))
                using (var extraFanArtDirectoryFsra = directoryFsra.GetResource("ExtraFanArt/"))
                  fanArtPaths.AddRange(GetPotentialFanArtFiles(extraFanArtDirectoryFsra));
            }

            files.AddRange(fanArtPaths.Select(path => new ResourceLocator(mediaIteamLocator.NativeSystemId, path)));
          }
        }
      }
      catch (Exception ex)
      {
#if DEBUG
        ServiceRegistration.Get<ILogger>().Warn("LocalFanArtProvider: Error while searching fanart of type '{0}' for '{1}'", ex, fanArtType, mediaIteamLocator);
#endif
      }
      result = files;
      return files.Count > 0;
    }