aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.MediaEncoding
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.MediaEncoding')
-rw-r--r--MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs189
-rw-r--r--MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj4
-rw-r--r--MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs36
3 files changed, 190 insertions, 39 deletions
diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
index c8361ea04..1dc1a4215 100644
--- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
+++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
@@ -262,6 +262,18 @@ namespace MediaBrowser.MediaEncoding.Encoder
var mediaInfo = new ProbeResultNormalizer(_logger, FileSystem).GetMediaInfo(result, videoType, isAudio, primaryPath, protocol);
+ var videoStream = mediaInfo.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
+
+ if (videoStream != null)
+ {
+ var isInterlaced = await DetectInterlaced(mediaInfo, videoStream, inputPath, probeSizeArgument).ConfigureAwait(false);
+
+ if (isInterlaced)
+ {
+ videoStream.IsInterlaced = true;
+ }
+ }
+
if (extractKeyFrameInterval && mediaInfo.RunTimeTicks.HasValue)
{
if (ConfigurationManager.Configuration.EnableVideoFrameByFrameAnalysis && mediaInfo.Size.HasValue)
@@ -304,6 +316,183 @@ namespace MediaBrowser.MediaEncoding.Encoder
throw new ApplicationException(string.Format("FFProbe failed for {0}", inputPath));
}
+ private async Task<bool> DetectInterlaced(MediaSourceInfo video, MediaStream videoStream, string inputPath, string probeSizeArgument)
+ {
+ if (video.Protocol != MediaProtocol.File)
+ {
+ return false;
+ }
+
+ var formats = (video.Container ?? string.Empty).Split(',').ToList();
+
+ // Take a shortcut and limit this to containers that are likely to have interlaced content
+ if (!formats.Contains("ts", StringComparer.OrdinalIgnoreCase) &&
+ !formats.Contains("mpegts", StringComparer.OrdinalIgnoreCase) &&
+ !formats.Contains("wtv", StringComparer.OrdinalIgnoreCase))
+ {
+ return false;
+ }
+
+ var args = "{0} -i {1} -map 0:v:{2} -filter:v idet -frames:v 500 -an -f null /dev/null";
+
+ var process = new Process
+ {
+ StartInfo = new ProcessStartInfo
+ {
+ CreateNoWindow = true,
+ UseShellExecute = false,
+
+ // Must consume both or ffmpeg may hang due to deadlocks. See comments below.
+ RedirectStandardOutput = true,
+ RedirectStandardError = true,
+ RedirectStandardInput = true,
+ FileName = FFMpegPath,
+ Arguments = string.Format(args, probeSizeArgument, inputPath, videoStream.Index.ToString(CultureInfo.InvariantCulture)).Trim(),
+
+ WindowStyle = ProcessWindowStyle.Hidden,
+ ErrorDialog = false
+ },
+
+ EnableRaisingEvents = true
+ };
+
+ _logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
+ var idetFoundInterlaced = false;
+
+ using (var processWrapper = new ProcessWrapper(process, this, _logger))
+ {
+ try
+ {
+ StartProcess(processWrapper);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error starting ffprobe", ex);
+
+ throw;
+ }
+
+ try
+ {
+ process.BeginOutputReadLine();
+
+ using (var reader = new StreamReader(process.StandardError.BaseStream))
+ {
+ while (!reader.EndOfStream)
+ {
+ var line = await reader.ReadLineAsync().ConfigureAwait(false);
+
+ if (line.StartsWith("[Parsed_idet", StringComparison.OrdinalIgnoreCase))
+ {
+ var idetResult = AnalyzeIdetResult(line);
+
+ if (idetResult.HasValue)
+ {
+ if (!idetResult.Value)
+ {
+ return false;
+ }
+
+ idetFoundInterlaced = true;
+ }
+ }
+ }
+ }
+
+ }
+ catch
+ {
+ StopProcess(processWrapper, 100, true);
+
+ throw;
+ }
+ }
+
+ return idetFoundInterlaced;
+ }
+
+ private bool? AnalyzeIdetResult(string line)
+ {
+ // As you can see, the filter only guessed one frame as progressive.
+ // Results like this are pretty typical. So if less than 30% of the detections are in the "Undetermined" category, then I only consider the video to be interlaced if at least 65% of the identified frames are in either the TFF or BFF category.
+ // In this case (310 + 311)/(622) = 99.8% which is well over the 65% metric. I may refine that number with more testing but I honestly do not believe I will need to.
+ // http://awel.domblogger.net/videoTranscode/interlace.html
+ var index = line.IndexOf("detection:", StringComparison.OrdinalIgnoreCase);
+
+ if (index == -1)
+ {
+ return null;
+ }
+
+ line = line.Substring(index).Trim();
+ var parts = line.Split(' ').Where(i => !string.IsNullOrWhiteSpace(i)).Select(i => i.Trim()).ToList();
+
+ if (parts.Count < 2)
+ {
+ return null;
+ }
+ double tff = 0;
+ double bff = 0;
+ double progressive = 0;
+ double undetermined = 0;
+ double total = 0;
+
+ for (var i = 0; i < parts.Count - 1; i++)
+ {
+ var part = parts[i];
+
+ if (string.Equals(part, "tff:", StringComparison.OrdinalIgnoreCase))
+ {
+ tff = GetNextPart(parts, i);
+ total += tff;
+ }
+ else if (string.Equals(part, "bff:", StringComparison.OrdinalIgnoreCase))
+ {
+ bff = GetNextPart(parts, i);
+ total += tff;
+ }
+ else if (string.Equals(part, "progressive:", StringComparison.OrdinalIgnoreCase))
+ {
+ progressive = GetNextPart(parts, i);
+ total += progressive;
+ }
+ else if (string.Equals(part, "undetermined:", StringComparison.OrdinalIgnoreCase))
+ {
+ undetermined = GetNextPart(parts, i);
+ total += undetermined;
+ }
+ }
+
+ if (total == 0)
+ {
+ return null;
+ }
+
+ if ((undetermined / total) >= .3)
+ {
+ return false;
+ }
+
+ if (((tff + bff) / total) >= .65)
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ private int GetNextPart(List<string> parts, int index)
+ {
+ var next = parts[index + 1];
+
+ int value;
+ if (int.TryParse(next, NumberStyles.Any, CultureInfo.InvariantCulture, out value))
+ {
+ return value;
+ }
+ return 0;
+ }
+
private bool EnableKeyframeExtraction(MediaSourceInfo mediaSource, MediaStream videoStream)
{
if (videoStream.Type == MediaStreamType.Video && string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase) &&
diff --git a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj
index 1f74994e5..df5ab4651 100644
--- a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj
+++ b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj
@@ -99,10 +99,6 @@
<Project>{17e1f4e6-8abd-4fe5-9ecf-43d4b6087ba2}</Project>
<Name>MediaBrowser.Controller</Name>
</ProjectReference>
- <ProjectReference Include="..\MediaBrowser.MediaInfo\MediaBrowser.MediaInfo.csproj">
- <Project>{6e4145e4-c6d4-4e4d-94f2-87188db6e239}</Project>
- <Name>MediaBrowser.MediaInfo</Name>
- </ProjectReference>
<ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj">
<Project>{7eeeb4bb-f3e8-48fc-b4c5-70f0fff8329b}</Project>
<Name>MediaBrowser.Model</Name>
diff --git a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
index d4df19af2..d9fda220d 100644
--- a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
+++ b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
@@ -1,5 +1,4 @@
using MediaBrowser.Common.IO;
-using MediaBrowser.MediaInfo;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
@@ -26,7 +25,7 @@ namespace MediaBrowser.MediaEncoding.Probing
_fileSystem = fileSystem;
}
- public Model.MediaInfo.MediaInfo GetMediaInfo(InternalMediaInfoResult data, VideoType videoType, bool isAudio, string path, MediaProtocol protocol)
+ public MediaInfo GetMediaInfo(InternalMediaInfoResult data, VideoType videoType, bool isAudio, string path, MediaProtocol protocol)
{
var info = new Model.MediaInfo.MediaInfo
{
@@ -104,13 +103,6 @@ namespace MediaBrowser.MediaEncoding.Probing
}
ExtractTimestamp(info);
-
- var videoStream = info.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
-
- if (videoStream != null && videoType == VideoType.VideoFile)
- {
- UpdateFromMediaInfo(info, videoStream);
- }
}
return info;
@@ -933,31 +925,5 @@ namespace MediaBrowser.MediaEncoding.Probing
return TransportStreamTimestamp.None;
}
-
- private void UpdateFromMediaInfo(MediaSourceInfo video, MediaStream videoStream)
- {
- if (video.Protocol == MediaProtocol.File && videoStream != null)
- {
- try
- {
- _logger.Debug("Running MediaInfo against {0}", video.Path);
-
- var result = new MediaInfoLib().GetVideoInfo(video.Path);
-
- videoStream.IsCabac = result.IsCabac ?? videoStream.IsCabac;
- videoStream.IsInterlaced = result.IsInterlaced ?? videoStream.IsInterlaced;
- videoStream.BitDepth = result.BitDepth ?? videoStream.BitDepth;
- videoStream.RefFrames = result.RefFrames ?? videoStream.RefFrames;
- }
- catch (TypeLoadException)
- {
- // This is non-essential. Don't spam the log
- }
- catch (Exception ex)
- {
- _logger.ErrorException("Error running MediaInfo on {0}", ex, video.Path);
- }
- }
- }
}
}