2014-04-04 00:08:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.MediaFiles.MediaInfo;
|
|
|
|
|
using NzbDrone.Core.Qualities;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles.EpisodeImport
|
|
|
|
|
{
|
2015-01-27 05:57:07 +00:00
|
|
|
|
public interface IDetectSample
|
2014-04-04 00:08:51 +00:00
|
|
|
|
{
|
2016-02-10 22:46:50 +00:00
|
|
|
|
bool IsSample(Series series, QualityModel quality, string path, long size, bool isSpecial);
|
2014-04-04 00:08:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 05:57:07 +00:00
|
|
|
|
public class DetectSample : IDetectSample
|
2014-04-04 00:08:51 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IVideoFileInfoReader _videoFileInfoReader;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
private static List<Quality> _largeSampleSizeQualities = new List<Quality> { Quality.HDTV1080p, Quality.WEBDL1080p, Quality.Bluray1080p };
|
|
|
|
|
|
2015-01-27 05:57:07 +00:00
|
|
|
|
public DetectSample(IVideoFileInfoReader videoFileInfoReader, Logger logger)
|
2014-04-04 00:08:51 +00:00
|
|
|
|
{
|
|
|
|
|
_videoFileInfoReader = videoFileInfoReader;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
|
public static long SampleSizeLimit => 70.Megabytes();
|
2014-04-04 00:08:51 +00:00
|
|
|
|
|
2016-02-10 22:46:50 +00:00
|
|
|
|
public bool IsSample(Series series, QualityModel quality, string path, long size, bool isSpecial)
|
2014-04-04 00:08:51 +00:00
|
|
|
|
{
|
2016-02-10 22:46:50 +00:00
|
|
|
|
if (isSpecial)
|
2014-04-04 00:08:51 +00:00
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Special, skipping sample check");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var extension = Path.GetExtension(path);
|
|
|
|
|
|
|
|
|
|
if (extension != null && extension.Equals(".flv", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Skipping sample check for .flv file");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-01 18:14:54 +00:00
|
|
|
|
if (extension != null && extension.Equals(".strm", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Skipping sample check for .strm file");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-04 00:08:51 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var runTime = _videoFileInfoReader.GetRunTime(path);
|
2014-11-18 01:07:27 +00:00
|
|
|
|
var minimumRuntime = GetMinimumAllowedRuntime(series);
|
2014-04-04 00:08:51 +00:00
|
|
|
|
|
|
|
|
|
if (runTime.TotalMinutes.Equals(0))
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("[{0}] has a runtime of 0, is it a valid video file?", path);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-18 01:07:27 +00:00
|
|
|
|
if (runTime.TotalSeconds < minimumRuntime)
|
2014-04-04 00:08:51 +00:00
|
|
|
|
{
|
2015-03-01 21:34:39 +00:00
|
|
|
|
_logger.Debug("[{0}] appears to be a sample. Runtime: {1} seconds. Expected at least: {2} seconds", path, runTime, minimumRuntime);
|
2014-04-04 00:08:51 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (DllNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Falling back to file size detection");
|
|
|
|
|
|
|
|
|
|
return CheckSize(size, quality);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Runtime is over 90 seconds");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckSize(long size, QualityModel quality)
|
|
|
|
|
{
|
|
|
|
|
if (_largeSampleSizeQualities.Contains(quality.Quality))
|
|
|
|
|
{
|
|
|
|
|
if (size < SampleSizeLimit * 2)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("1080p file is less than sample limit");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (size < SampleSizeLimit)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("File is less than sample limit");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-11-18 01:07:27 +00:00
|
|
|
|
|
|
|
|
|
private int GetMinimumAllowedRuntime(Series series)
|
|
|
|
|
{
|
|
|
|
|
//Webisodes - 90 seconds
|
2016-06-25 20:50:16 +00:00
|
|
|
|
if (series.Runtime <= 10)
|
2014-11-18 01:07:27 +00:00
|
|
|
|
{
|
|
|
|
|
return 90;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//30 minute episodes - 5 minutes
|
|
|
|
|
if (series.Runtime <= 30)
|
|
|
|
|
{
|
|
|
|
|
return 300;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//60 minute episodes - 10 minutes
|
|
|
|
|
return 600;
|
|
|
|
|
}
|
2014-04-04 00:08:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|