Radarr/src/NzbDrone.Core/Extras/Metadata/Consumers/Xbmc/XbmcMetadata.cs

277 lines
10 KiB
C#
Raw Normal View History

using System;
2014-01-26 07:14:55 +00:00
using System.Collections.Generic;
2014-01-22 05:22:09 +00:00
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
2014-01-22 05:22:09 +00:00
using System.Xml;
using System.Xml.Linq;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Extras.Metadata.Files;
2014-01-22 05:22:09 +00:00
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.MediaFiles;
New: Refactor MediaInfo tokens (fixes old tokens adds new stuff) (#3058) * Rename all 'episodeFile' variables to 'movieFile' * Improve media info extraction with more fields * Improve media info tokens extraction * Add missing fields to MediaInfoModel * Restore to previous implementation of null handling * Forgot to add MediaInfoFormatter to project * Add missing EqualsIgnoreCase extension method * Simplify Logger.Debug() invocations * Add missing StartsWithIgnoreCase extension method * This '.Value' shouldn't be required * Remove TODO comment * Upgrade MediaInfo from 17.10 to 18.08.1 * Use correct media info field for files listing * Replace media info "VideoCodec" (deprecated) with "VideoFormat" * Fix 'Formatiting' typos * Add support for media info Format_AdditionalFeatures' field * Add proper support for all DTS and TrueHD flavors * Add support for '3D' media info token * Remove deprecated media info video/audio profile fields * Add support for 'HDR' media info token * Add new video parameters to anime file name sample * Adapt tests for new media info fields * Revert "Remove deprecated media info video/audio profile fields" * Include missing test files in core test project * Fix small regression issue * Allow sample movie to be detected as HDR * Do not parse audio channel positions if there are no channels * Clean up extra blank line * Reuse already declared variable * Fix wrong audio channels detection on DTS:X streams * Fix all failing unit tests * Fix remaining failing unit tests
2018-10-30 20:44:59 +00:00
using NzbDrone.Core.MediaFiles.MediaInfo;
using NzbDrone.Core.Movies;
2014-01-22 05:22:09 +00:00
namespace NzbDrone.Core.Extras.Metadata.Consumers.Xbmc
2014-01-22 05:22:09 +00:00
{
public class XbmcMetadata : MetadataBase<XbmcMetadataSettings>
2014-01-22 05:22:09 +00:00
{
private readonly IMapCoversToLocal _mediaCoverService;
private readonly Logger _logger;
private readonly IDetectXbmcNfo _detectNfo;
private readonly IDiskProvider _diskProvider;
2014-01-22 05:22:09 +00:00
public XbmcMetadata(IDetectXbmcNfo detectNfo,
IDiskProvider diskProvider,
IMapCoversToLocal mediaCoverService,
2014-01-22 05:22:09 +00:00
Logger logger)
{
_logger = logger;
_mediaCoverService = mediaCoverService;
_diskProvider = diskProvider;
_detectNfo = detectNfo;
New: Refactor MediaInfo tokens (fixes old tokens adds new stuff) (#3058) * Rename all 'episodeFile' variables to 'movieFile' * Improve media info extraction with more fields * Improve media info tokens extraction * Add missing fields to MediaInfoModel * Restore to previous implementation of null handling * Forgot to add MediaInfoFormatter to project * Add missing EqualsIgnoreCase extension method * Simplify Logger.Debug() invocations * Add missing StartsWithIgnoreCase extension method * This '.Value' shouldn't be required * Remove TODO comment * Upgrade MediaInfo from 17.10 to 18.08.1 * Use correct media info field for files listing * Replace media info "VideoCodec" (deprecated) with "VideoFormat" * Fix 'Formatiting' typos * Add support for media info Format_AdditionalFeatures' field * Add proper support for all DTS and TrueHD flavors * Add support for '3D' media info token * Remove deprecated media info video/audio profile fields * Add support for 'HDR' media info token * Add new video parameters to anime file name sample * Adapt tests for new media info fields * Revert "Remove deprecated media info video/audio profile fields" * Include missing test files in core test project * Fix small regression issue * Allow sample movie to be detected as HDR * Do not parse audio channel positions if there are no channels * Clean up extra blank line * Reuse already declared variable * Fix wrong audio channels detection on DTS:X streams * Fix all failing unit tests * Fix remaining failing unit tests
2018-10-30 20:44:59 +00:00
2014-01-22 05:22:09 +00:00
}
private static readonly Regex MovieImagesRegex = new Regex(@"^(?<type>poster|banner|fanart|clearart|discart|landscape|logo|backdrop|clearlogo)\.(?:png|jpg)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex MovieFileImageRegex = new Regex(@"(?<type>-thumb|-poster|-banner|-fanart|-clearart|-discart|-landscape|-logo|-backdrop|-clearlogo)\.(?:png|jpg)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
2016-12-09 06:54:15 +00:00
public override string Name => "Kodi (XBMC) / Emby";
2015-04-25 16:10:43 +00:00
public override string GetFilenameAfterMove(Movie movie, MovieFile movieFile, MetadataFile metadataFile)
2014-01-22 05:22:09 +00:00
{
var movieFilePath = Path.Combine(movie.Path, movieFile.RelativePath);
var metadataPath = Path.Combine(movie.Path, metadataFile.RelativePath);
if (metadataFile.Type == MetadataType.MovieMetadata)
{
return GetMovieMetadataFilename(movieFilePath);
}
_logger.Debug("Unknown movie file metadata: {0}", metadataFile.RelativePath);
return Path.Combine(movie.Path, metadataFile.RelativePath);
}
public override MetadataFile FindMetadataFile(Movie movie, string path)
{
var filename = Path.GetFileName(path);
if (filename == null) return null;
var metadata = new MetadataFile
{
MovieId = movie.Id,
Consumer = GetType().Name,
RelativePath = movie.Path.GetRelativePath(path)
};
if (MovieImagesRegex.IsMatch(filename))
{
metadata.Type = MetadataType.MovieImage;
return metadata;
}
if (MovieFileImageRegex.IsMatch(filename))
{
metadata.Type = MetadataType.MovieImage;
return metadata;
}
if (filename.Equals("movie.nfo", StringComparison.OrdinalIgnoreCase) &&
_detectNfo.IsXbmcNfoFile(path))
{
metadata.Type = MetadataType.MovieMetadata;
return metadata;
}
var parseResult = Parser.Parser.ParseMovieTitle(filename, false);
if (parseResult != null &&
Path.GetExtension(filename).Equals(".nfo", StringComparison.OrdinalIgnoreCase) &&
_detectNfo.IsXbmcNfoFile(path))
{
metadata.Type = MetadataType.MovieMetadata;
return metadata;
}
2014-01-22 05:22:09 +00:00
return null;
2014-01-22 05:22:09 +00:00
}
public override MetadataFileResult MovieMetadata(Movie movie, MovieFile movieFile)
2014-01-22 05:22:09 +00:00
{
if (!Settings.MovieMetadata)
2014-04-30 23:39:54 +00:00
{
return null;
}
_logger.Debug("Generating Movie Metadata for: {0}", Path.Combine(movie.Path, movieFile.RelativePath));
var watched = GetExistingWatchedStatus(movie, movieFile.RelativePath);
var xmlResult = string.Empty;
2014-01-22 05:22:09 +00:00
var sb = new StringBuilder();
var xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = false;
using (var xw = XmlWriter.Create(sb, xws))
{
var doc = new XDocument();
var image = movie.Images.SingleOrDefault(i => i.CoverType == MediaCoverTypes.Screenshot);
var details = new XElement("movie");
2014-01-22 05:22:09 +00:00
details.Add(new XElement("title", movie.Title));
if (movie.Ratings != null && movie.Ratings.Votes > 0)
{
details.Add(new XElement("rating", movie.Ratings.Value));
}
details.Add(new XElement("plot", movie.Overview));
details.Add(new XElement("id", movie.ImdbId));
if (movie.ImdbId.IsNotNullOrWhiteSpace())
{
var imdbId = new XElement("uniqueid", movie.ImdbId);
imdbId.SetAttributeValue("type", "imdb");
imdbId.SetAttributeValue("default", true);
details.Add(imdbId);
}
var uniqueId = new XElement("uniqueid", movie.TmdbId);
uniqueId.SetAttributeValue("type", "tmdb");
details.Add(uniqueId);
details.Add(new XElement("year", movie.Year));
2014-01-22 05:22:09 +00:00
if (movie.InCinemas.HasValue)
2014-01-22 05:22:09 +00:00
{
details.Add(new XElement("premiered", movie.InCinemas.Value.ToString("yyyy-MM-dd")));
2014-01-22 05:22:09 +00:00
}
foreach (var genre in movie.Genres)
2014-01-22 05:22:09 +00:00
{
details.Add(new XElement("genre", genre));
2014-01-22 05:22:09 +00:00
}
details.Add(new XElement("studio", movie.Studio));
2014-01-22 05:22:09 +00:00
if (image == null)
2014-01-22 05:22:09 +00:00
{
details.Add(new XElement("thumb"));
2014-01-22 05:22:09 +00:00
}
else
2014-01-22 05:22:09 +00:00
{
details.Add(new XElement("thumb", image.Url));
}
details.Add(new XElement("watched", watched));
if (movieFile.MediaInfo != null)
{
New: Refactor MediaInfo tokens (fixes old tokens adds new stuff) (#3058) * Rename all 'episodeFile' variables to 'movieFile' * Improve media info extraction with more fields * Improve media info tokens extraction * Add missing fields to MediaInfoModel * Restore to previous implementation of null handling * Forgot to add MediaInfoFormatter to project * Add missing EqualsIgnoreCase extension method * Simplify Logger.Debug() invocations * Add missing StartsWithIgnoreCase extension method * This '.Value' shouldn't be required * Remove TODO comment * Upgrade MediaInfo from 17.10 to 18.08.1 * Use correct media info field for files listing * Replace media info "VideoCodec" (deprecated) with "VideoFormat" * Fix 'Formatiting' typos * Add support for media info Format_AdditionalFeatures' field * Add proper support for all DTS and TrueHD flavors * Add support for '3D' media info token * Remove deprecated media info video/audio profile fields * Add support for 'HDR' media info token * Add new video parameters to anime file name sample * Adapt tests for new media info fields * Revert "Remove deprecated media info video/audio profile fields" * Include missing test files in core test project * Fix small regression issue * Allow sample movie to be detected as HDR * Do not parse audio channel positions if there are no channels * Clean up extra blank line * Reuse already declared variable * Fix wrong audio channels detection on DTS:X streams * Fix all failing unit tests * Fix remaining failing unit tests
2018-10-30 20:44:59 +00:00
var sceneName = movieFile.GetSceneOrFileName();
var fileInfo = new XElement("fileinfo");
var streamDetails = new XElement("streamdetails");
var video = new XElement("video");
video.Add(new XElement("aspect", (float)movieFile.MediaInfo.Width / (float)movieFile.MediaInfo.Height));
video.Add(new XElement("bitrate", movieFile.MediaInfo.VideoBitrate));
New: Refactor MediaInfo tokens (fixes old tokens adds new stuff) (#3058) * Rename all 'episodeFile' variables to 'movieFile' * Improve media info extraction with more fields * Improve media info tokens extraction * Add missing fields to MediaInfoModel * Restore to previous implementation of null handling * Forgot to add MediaInfoFormatter to project * Add missing EqualsIgnoreCase extension method * Simplify Logger.Debug() invocations * Add missing StartsWithIgnoreCase extension method * This '.Value' shouldn't be required * Remove TODO comment * Upgrade MediaInfo from 17.10 to 18.08.1 * Use correct media info field for files listing * Replace media info "VideoCodec" (deprecated) with "VideoFormat" * Fix 'Formatiting' typos * Add support for media info Format_AdditionalFeatures' field * Add proper support for all DTS and TrueHD flavors * Add support for '3D' media info token * Remove deprecated media info video/audio profile fields * Add support for 'HDR' media info token * Add new video parameters to anime file name sample * Adapt tests for new media info fields * Revert "Remove deprecated media info video/audio profile fields" * Include missing test files in core test project * Fix small regression issue * Allow sample movie to be detected as HDR * Do not parse audio channel positions if there are no channels * Clean up extra blank line * Reuse already declared variable * Fix wrong audio channels detection on DTS:X streams * Fix all failing unit tests * Fix remaining failing unit tests
2018-10-30 20:44:59 +00:00
video.Add(new XElement("codec", MediaInfoFormatter.FormatVideoCodec(movieFile.MediaInfo, sceneName)));
video.Add(new XElement("framerate", movieFile.MediaInfo.VideoFps));
video.Add(new XElement("height", movieFile.MediaInfo.Height));
video.Add(new XElement("scantype", movieFile.MediaInfo.ScanType));
video.Add(new XElement("width", movieFile.MediaInfo.Width));
if (movieFile.MediaInfo.RunTime != null)
{
video.Add(new XElement("duration", movieFile.MediaInfo.RunTime.TotalMinutes));
video.Add(new XElement("durationinseconds", movieFile.MediaInfo.RunTime.TotalSeconds));
}
streamDetails.Add(video);
2014-04-30 23:39:54 +00:00
var audio = new XElement("audio");
audio.Add(new XElement("bitrate", movieFile.MediaInfo.AudioBitrate));
audio.Add(new XElement("channels", movieFile.MediaInfo.AudioChannels));
New: Refactor MediaInfo tokens (fixes old tokens adds new stuff) (#3058) * Rename all 'episodeFile' variables to 'movieFile' * Improve media info extraction with more fields * Improve media info tokens extraction * Add missing fields to MediaInfoModel * Restore to previous implementation of null handling * Forgot to add MediaInfoFormatter to project * Add missing EqualsIgnoreCase extension method * Simplify Logger.Debug() invocations * Add missing StartsWithIgnoreCase extension method * This '.Value' shouldn't be required * Remove TODO comment * Upgrade MediaInfo from 17.10 to 18.08.1 * Use correct media info field for files listing * Replace media info "VideoCodec" (deprecated) with "VideoFormat" * Fix 'Formatiting' typos * Add support for media info Format_AdditionalFeatures' field * Add proper support for all DTS and TrueHD flavors * Add support for '3D' media info token * Remove deprecated media info video/audio profile fields * Add support for 'HDR' media info token * Add new video parameters to anime file name sample * Adapt tests for new media info fields * Revert "Remove deprecated media info video/audio profile fields" * Include missing test files in core test project * Fix small regression issue * Allow sample movie to be detected as HDR * Do not parse audio channel positions if there are no channels * Clean up extra blank line * Reuse already declared variable * Fix wrong audio channels detection on DTS:X streams * Fix all failing unit tests * Fix remaining failing unit tests
2018-10-30 20:44:59 +00:00
audio.Add(new XElement("codec", MediaInfoFormatter.FormatAudioCodec(movieFile.MediaInfo, sceneName)));
audio.Add(new XElement("language", movieFile.MediaInfo.AudioLanguages));
streamDetails.Add(audio);
if (movieFile.MediaInfo.Subtitles != null && movieFile.MediaInfo.Subtitles.Length > 0)
{
var subtitle = new XElement("subtitle");
subtitle.Add(new XElement("language", movieFile.MediaInfo.Subtitles));
streamDetails.Add(subtitle);
}
2014-01-22 05:22:09 +00:00
fileInfo.Add(streamDetails);
details.Add(fileInfo);
}
doc.Add(details);
doc.Save(xw);
2014-01-22 05:22:09 +00:00
xmlResult += doc.ToString();
xmlResult += Environment.NewLine;
2014-01-22 05:22:09 +00:00
}
var metadataFileName = GetMovieMetadataFilename(movieFile.RelativePath);
if (Settings.UseMovieNfo)
2014-04-30 23:39:54 +00:00
{
metadataFileName = "movie.nfo";
2014-04-30 23:39:54 +00:00
}
return new MetadataFileResult(metadataFileName, xmlResult.Trim(Environment.NewLine.ToCharArray()));
2014-01-22 05:22:09 +00:00
}
public override List<ImageFileResult> MovieImages(Movie movie)
2014-01-22 05:22:09 +00:00
{
if (!Settings.MovieImages)
2014-04-30 23:39:54 +00:00
{
return new List<ImageFileResult>();
}
return ProcessMovieImages(movie).ToList();
2014-04-30 23:39:54 +00:00
}
private IEnumerable<ImageFileResult> ProcessMovieImages(Movie movie)
2014-04-30 23:39:54 +00:00
{
foreach (var image in movie.Images)
{
var source = _mediaCoverService.GetCoverPath(movie.Id, image.CoverType);
var destination = image.CoverType.ToString().ToLowerInvariant() + Path.GetExtension(source);
yield return new ImageFileResult(destination, source);
}
2014-04-30 23:39:54 +00:00
}
private string GetMovieMetadataFilename(string movieFilePath)
2014-04-30 23:39:54 +00:00
{
return Path.ChangeExtension(movieFilePath, "nfo");
2014-04-30 23:39:54 +00:00
}
private bool GetExistingWatchedStatus(Movie movie, string movieFilePath)
{
var fullPath = Path.Combine(movie.Path, GetMovieMetadataFilename(movieFilePath));
if (!_diskProvider.FileExists(fullPath))
{
return false;
}
var fileContent = _diskProvider.ReadAllText(fullPath);
return Regex.IsMatch(fileContent, "<watched>true</watched>");
}
2014-01-22 05:22:09 +00:00
}
}