Skip to content

Commit

Permalink
update interlaced detection
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePulverenti committed Jan 14, 2016
1 parent 883071f commit 88a4c17
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 42 deletions.
3 changes: 2 additions & 1 deletion MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public interface IMediaEncoder
/// Extracts the audio image.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="imageStreamIndex">Index of the image stream.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{Stream}.</returns>
Task<Stream> ExtractAudioImage(string path, CancellationToken cancellationToken);
Task<Stream> ExtractAudioImage(string path, int? imageStreamIndex, CancellationToken cancellationToken);

/// <summary>
/// Extracts the video image.
Expand Down
1 change: 0 additions & 1 deletion MediaBrowser.Controller/MediaEncoding/MediaInfoRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class MediaInfoRequest
public IIsoMount MountedIso { get; set; }
public VideoType VideoType { get; set; }
public List<string> PlayableStreamFileNames { get; set; }
public bool ExtractKeyFrameInterval { get; set; }

public MediaInfoRequest()
{
Expand Down
2 changes: 1 addition & 1 deletion MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ private string GetVideoCodec(EncodingJobOptions request)
{
return "libx264";
}
if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase))
if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase) || string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase))
{
return "libx265";
}
Expand Down
73 changes: 43 additions & 30 deletions MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,17 @@ public string GetProbeSizeArgument(string[] inputFiles, MediaProtocol protocol)

var mediaInfo = new ProbeResultNormalizer(_logger, FileSystem).GetMediaInfo(result, videoType, isAudio, primaryPath, protocol);

//var videoStream = mediaInfo.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
var videoStream = mediaInfo.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);

//if (videoStream != null)
//{
// var isInterlaced = await DetectInterlaced(mediaInfo, videoStream, inputPath, probeSizeArgument).ConfigureAwait(false);
if (videoStream != null)
{
var isInterlaced = await DetectInterlaced(mediaInfo, videoStream, inputPath, probeSizeArgument).ConfigureAwait(false);

// if (isInterlaced)
// {
// videoStream.IsInterlaced = true;
// }
//}
if (isInterlaced)
{
videoStream.IsInterlaced = true;
}
}

return mediaInfo;
}
Expand All @@ -292,19 +292,22 @@ private async Task<bool> DetectInterlaced(MediaSourceInfo video, MediaStream vid
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("vob", StringComparer.OrdinalIgnoreCase) &&
!formats.Contains("m2ts", StringComparer.OrdinalIgnoreCase) &&
!formats.Contains("ts", StringComparer.OrdinalIgnoreCase) &&
!formats.Contains("mpegts", StringComparer.OrdinalIgnoreCase) &&
!formats.Contains("wtv", StringComparer.OrdinalIgnoreCase))
// If the video codec is not some form of mpeg, then take a shortcut and limit this to containers that are likely to have interlaced content
if ((videoStream.Codec ?? string.Empty).IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) == -1)
{
return false;
var formats = (video.Container ?? string.Empty).Split(',').ToList();

if (!formats.Contains("vob", StringComparer.OrdinalIgnoreCase) &&
!formats.Contains("m2ts", StringComparer.OrdinalIgnoreCase) &&
!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 args = "{0} -i {1} -map 0:v:{2} -an -filter:v idet -frames:v 500 -an -f null /dev/null";

var process = new Process
{
Expand Down Expand Up @@ -444,7 +447,7 @@ private async Task<bool> DetectInterlaced(MediaSourceInfo video, MediaStream vid
return false;
}

if (((tff + bff) / total) >= .65)
if (((tff + bff) / total) >= .4)
{
return true;
}
Expand All @@ -469,29 +472,37 @@ private int GetNextPart(List<string> parts, int index)
/// </summary>
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");

public Task<Stream> ExtractAudioImage(string path, CancellationToken cancellationToken)
public Task<Stream> ExtractAudioImage(string path, int? imageStreamIndex, CancellationToken cancellationToken)
{
return ExtractImage(new[] { path }, MediaProtocol.File, true, null, null, cancellationToken);
return ExtractImage(new[] { path }, imageStreamIndex, MediaProtocol.File, true, null, null, cancellationToken);
}

public Task<Stream> ExtractVideoImage(string[] inputFiles, MediaProtocol protocol, Video3DFormat? threedFormat,
TimeSpan? offset, CancellationToken cancellationToken)
{
return ExtractImage(inputFiles, protocol, false, threedFormat, offset, cancellationToken);
return ExtractImage(inputFiles, null, protocol, false, threedFormat, offset, cancellationToken);
}

private async Task<Stream> ExtractImage(string[] inputFiles, MediaProtocol protocol, bool isAudio,
private async Task<Stream> ExtractImage(string[] inputFiles, int? imageStreamIndex, MediaProtocol protocol, bool isAudio,
Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken)
{
var resourcePool = isAudio ? _audioImageResourcePool : _videoImageResourcePool;

var inputArgument = GetInputArgument(inputFiles, protocol);

if (!isAudio)
if (isAudio)
{
//if (imageStreamIndex.HasValue && imageStreamIndex.Value > 0)
//{
// // It seems for audio files we need to subtract 1 (for the audio stream??)
// imageStreamIndex = imageStreamIndex.Value - 1;
//}
}
else
{
try
{
return await ExtractImageInternal(inputArgument, protocol, threedFormat, offset, true, resourcePool, cancellationToken).ConfigureAwait(false);
return await ExtractImageInternal(inputArgument, imageStreamIndex, protocol, threedFormat, offset, true, resourcePool, cancellationToken).ConfigureAwait(false);
}
catch (ArgumentException)
{
Expand All @@ -503,10 +514,10 @@ private async Task<Stream> ExtractImage(string[] inputFiles, MediaProtocol proto
}
}

return await ExtractImageInternal(inputArgument, protocol, threedFormat, offset, false, resourcePool, cancellationToken).ConfigureAwait(false);
return await ExtractImageInternal(inputArgument, imageStreamIndex, protocol, threedFormat, offset, false, resourcePool, cancellationToken).ConfigureAwait(false);
}

private async Task<Stream> ExtractImageInternal(string inputPath, MediaProtocol protocol, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
private async Task<Stream> ExtractImageInternal(string inputPath, int? imageStreamIndex, MediaProtocol protocol, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(inputPath))
{
Expand Down Expand Up @@ -540,9 +551,11 @@ private async Task<Stream> ExtractImageInternal(string inputPath, MediaProtocol
}
}

var mapArg = imageStreamIndex.HasValue ? (" -map 0:v:" + imageStreamIndex.Value.ToString(CultureInfo.InvariantCulture)) : string.Empty;

// Use ffmpeg to sample 100 (we can drop this if required using thumbnail=50 for 50 frames) frames and pick the best thumbnail. Have a fall back just in case.
var args = useIFrame ? string.Format("-i {0} -threads 1 -v quiet -vframes 1 -vf \"{2},thumbnail=30\" -f image2 \"{1}\"", inputPath, "-", vf) :
string.Format("-i {0} -threads 1 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, "-", vf);
var args = useIFrame ? string.Format("-i {0}{3} -threads 1 -v quiet -vframes 1 -vf \"{2},thumbnail=30\" -f image2 \"{1}\"", inputPath, "-", vf, mapArg) :
string.Format("-i {0}{3} -threads 1 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, "-", vf, mapArg);

var probeSize = GetProbeSizeArgument(new[] { inputPath }, protocol);

Expand Down
3 changes: 2 additions & 1 deletion MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private MediaStream GetMediaStream(bool isAudio, MediaStreamInfo streamInfo, Med
if (streamInfo.tags != null)
{
stream.Language = GetDictionaryValue(streamInfo.tags, "language");
stream.Comment = GetDictionaryValue(streamInfo.tags, "comment");
}

if (string.Equals(streamInfo.codec_type, "audio", StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -872,7 +873,7 @@ private void FetchWtvInfo(Model.MediaInfo.MediaInfo video, InternalMediaInfoResu
}
}

private void ExtractTimestamp(Model.MediaInfo.MediaInfo video)
private void ExtractTimestamp(MediaInfo video)
{
if (video.VideoType == VideoType.VideoFile)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,9 @@
<Compile Include="..\MediaBrowser.Model\MediaInfo\VideoCodec.cs">
<Link>MediaInfo\VideoCodec.cs</Link>
</Compile>
<Compile Include="..\MediaBrowser.Model\Net\EndPointInfo.cs">
<Link>Net\EndPointInfo.cs</Link>
</Compile>
<Compile Include="..\MediaBrowser.Model\Net\HttpException.cs">
<Link>Net\HttpException.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,9 @@
<Compile Include="..\MediaBrowser.Model\MediaInfo\VideoCodec.cs">
<Link>MediaInfo\VideoCodec.cs</Link>
</Compile>
<Compile Include="..\MediaBrowser.Model\Net\EndPointInfo.cs">
<Link>Net\EndPointInfo.cs</Link>
</Compile>
<Compile Include="..\MediaBrowser.Model\Net\HttpException.cs">
<Link>Net\HttpException.cs</Link>
</Compile>
Expand Down
6 changes: 6 additions & 0 deletions MediaBrowser.Model/Entities/MediaStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class MediaStream
/// <value>The language.</value>
public string Language { get; set; }

/// <summary>
/// Gets or sets the comment.
/// </summary>
/// <value>The comment.</value>
public string Comment { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this instance is interlaced.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions MediaBrowser.Model/MediaBrowser.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<Compile Include="MediaInfo\MediaProtocol.cs" />
<Compile Include="MediaInfo\SubtitleTrackEvent.cs" />
<Compile Include="MediaInfo\SubtitleTrackInfo.cs" />
<Compile Include="Net\EndPointInfo.cs" />
<Compile Include="Net\HttpResponse.cs" />
<Compile Include="Net\MimeTypes.cs" />
<Compile Include="Notifications\NotificationOption.cs" />
Expand Down
8 changes: 8 additions & 0 deletions MediaBrowser.Model/Net/EndPointInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MediaBrowser.Model.Net
{
public class EndPointInfo
{
public bool IsLocal { get; set; }
public bool IsInNetwork { get; set; }
}
}
29 changes: 21 additions & 8 deletions MediaBrowser.Providers/MediaInfo/AudioImageProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediaBrowser.Common.Extensions;
using System;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
Expand Down Expand Up @@ -43,20 +44,27 @@ public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, Canc
{
var audio = (Audio)item;

var imageStreams =
audio.GetMediaSources(false)
.Take(1)
.SelectMany(i => i.MediaStreams)
.Where(i => i.Type == MediaStreamType.EmbeddedImage)
.ToList();

// Can't extract if we didn't find a video stream in the file
if (!audio.GetMediaSources(false).Take(1).SelectMany(i => i.MediaStreams).Any(i => i.Type == MediaStreamType.EmbeddedImage))
if (imageStreams.Count == 0)
{
return Task.FromResult(new DynamicImageResponse { HasImage = false });
}

return GetImage((Audio)item, cancellationToken);
return GetImage((Audio)item, imageStreams, cancellationToken);
}

public async Task<DynamicImageResponse> GetImage(Audio item, CancellationToken cancellationToken)
public async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
{
var path = GetAudioImagePath(item);

if (!_fileSystem.FileExists(path))
if (!_fileSystem.FileExists(path))
{
var semaphore = GetLock(path);

Expand All @@ -66,11 +74,16 @@ public async Task<DynamicImageResponse> GetImage(Audio item, CancellationToken c
try
{
// Check again in case it was saved while waiting for the lock
if (!_fileSystem.FileExists(path))
if (!_fileSystem.FileExists(path))
{
_fileSystem.CreateDirectory(Path.GetDirectoryName(path));
_fileSystem.CreateDirectory(Path.GetDirectoryName(path));

var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("front", StringComparison.OrdinalIgnoreCase) != -1) ??
imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("cover", StringComparison.OrdinalIgnoreCase) != -1);

var imageStreamIndex = imageStream == null ? (int?)null : imageStream.Index;

using (var stream = await _mediaEncoder.ExtractAudioImage(item.Path, cancellationToken).ConfigureAwait(false))
using (var stream = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false))
{
using (var fileStream = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, true))
{
Expand Down

0 comments on commit 88a4c17

Please sign in to comment.