diff options
Diffstat (limited to 'MediaBrowser.MediaEncoding')
9 files changed, 1668 insertions, 137 deletions
diff --git a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs index 78ac92f25..c30ceb62d 100644 --- a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs @@ -70,10 +70,7 @@ namespace MediaBrowser.MediaEncoding.Encoder encodingJob.OutputFilePath = GetOutputFilePath(encodingJob); Directory.CreateDirectory(Path.GetDirectoryName(encodingJob.OutputFilePath)); - if (options.Context == EncodingContext.Static && encodingJob.IsInputVideo) - { - encodingJob.ReadInputAtNativeFramerate = true; - } + encodingJob.ReadInputAtNativeFramerate = options.ReadInputAtNativeFramerate; await AcquireResources(encodingJob, cancellationToken).ConfigureAwait(false); @@ -305,19 +302,7 @@ namespace MediaBrowser.MediaEncoding.Encoder /// <returns>System.Int32.</returns> protected int GetNumberOfThreads(EncodingJob job, bool isWebm) { - // Only need one thread for sync - if (job.Options.Context == EncodingContext.Static) - { - return 1; - } - - if (isWebm) - { - // Recommended per docs - return Math.Max(Environment.ProcessorCount - 1, 2); - } - - return 0; + return job.Options.CpuCoreLimit ?? 0; } protected EncodingQuality GetQualitySetting() diff --git a/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs b/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs index 8d8201074..ea3cc6d89 100644 --- a/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs +++ b/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs @@ -59,7 +59,7 @@ namespace MediaBrowser.MediaEncoding.Encoder state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase); - var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(request.ItemId, false, cancellationToken).ConfigureAwait(false); + var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(request.ItemId, null, false, new[] { MediaType.Audio, MediaType.Video }, cancellationToken).ConfigureAwait(false); var mediaSource = string.IsNullOrEmpty(request.MediaSourceId) ? mediaSources.First() @@ -124,10 +124,14 @@ namespace MediaBrowser.MediaEncoding.Encoder state.InputContainer = mediaSource.Container; state.InputFileSize = mediaSource.Size; state.InputBitrate = mediaSource.Bitrate; - state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate; state.RunTimeTicks = mediaSource.RunTimeTicks; state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; + if (mediaSource.ReadAtNativeFramerate) + { + state.ReadInputAtNativeFramerate = true; + } + if (mediaSource.VideoType.HasValue) { state.VideoType = mediaSource.VideoType.Value; @@ -148,7 +152,6 @@ namespace MediaBrowser.MediaEncoding.Encoder state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; state.InputBitrate = mediaSource.Bitrate; state.InputFileSize = mediaSource.Size; - state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate; if (state.ReadInputAtNativeFramerate || mediaSource.Protocol == MediaProtocol.File && string.Equals(mediaSource.Container, "wtv", StringComparison.OrdinalIgnoreCase)) diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index 425889807..d076a5b6b 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -5,12 +5,15 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Controller.LiveTv; using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.Session; +using MediaBrowser.MediaEncoding.Probing; +using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Entities; using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; using MediaBrowser.Model.MediaInfo; using MediaBrowser.Model.Serialization; using System; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; @@ -72,6 +75,8 @@ namespace MediaBrowser.MediaEncoding.Encoder protected readonly Func<ISubtitleEncoder> SubtitleEncoder; protected readonly Func<IMediaSourceManager> MediaSourceManager; + private readonly List<ProcessWrapper> _runningProcesses = new List<ProcessWrapper>(); + public MediaEncoder(ILogger logger, IJsonSerializer jsonSerializer, string ffMpegPath, string ffProbePath, string version, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ILiveTvManager liveTvManager, IIsoManager isoManager, ILibraryManager libraryManager, IChannelManager channelManager, ISessionManager sessionManager, Func<ISubtitleEncoder> subtitleEncoder, Func<IMediaSourceManager> mediaSourceManager) { _logger = logger; @@ -102,16 +107,19 @@ namespace MediaBrowser.MediaEncoding.Encoder /// <summary> /// Gets the media info. /// </summary> - /// <param name="inputFiles">The input files.</param> - /// <param name="protocol">The protocol.</param> - /// <param name="isAudio">if set to <c>true</c> [is audio].</param> + /// <param name="request">The request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> - public Task<InternalMediaInfoResult> GetMediaInfo(string[] inputFiles, MediaProtocol protocol, bool isAudio, - CancellationToken cancellationToken) + public Task<Model.MediaInfo.MediaInfo> GetMediaInfo(MediaInfoRequest request, CancellationToken cancellationToken) { - return GetMediaInfoInternal(GetInputArgument(inputFiles, protocol), !isAudio, - GetProbeSizeArgument(inputFiles, protocol), cancellationToken); + var extractChapters = request.MediaType == DlnaProfileType.Video && request.ExtractChapters; + + var inputFiles = MediaEncoderHelpers.GetInputArgument(request.InputPath, request.Protocol, request.MountedIso, request.PlayableStreamFileNames); + + var extractKeyFrameInterval = request.ExtractKeyFrameInterval && request.Protocol == MediaProtocol.File && request.VideoType == VideoType.VideoFile; + + return GetMediaInfoInternal(GetInputArgument(inputFiles, request.Protocol), request.InputPath, request.Protocol, extractChapters, extractKeyFrameInterval, + GetProbeSizeArgument(inputFiles, request.Protocol), request.MediaType == DlnaProfileType.Audio, cancellationToken); } /// <summary> @@ -141,13 +149,22 @@ namespace MediaBrowser.MediaEncoding.Encoder /// Gets the media info internal. /// </summary> /// <param name="inputPath">The input path.</param> + /// <param name="primaryPath">The primary path.</param> + /// <param name="protocol">The protocol.</param> /// <param name="extractChapters">if set to <c>true</c> [extract chapters].</param> + /// <param name="extractKeyFrameInterval">if set to <c>true</c> [extract key frame interval].</param> /// <param name="probeSizeArgument">The probe size argument.</param> + /// <param name="isAudio">if set to <c>true</c> [is audio].</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task{MediaInfoResult}.</returns> /// <exception cref="System.ApplicationException"></exception> - private async Task<InternalMediaInfoResult> GetMediaInfoInternal(string inputPath, bool extractChapters, + private async Task<Model.MediaInfo.MediaInfo> GetMediaInfoInternal(string inputPath, + string primaryPath, + MediaProtocol protocol, + bool extractChapters, + bool extractKeyFrameInterval, string probeSizeArgument, + bool isAudio, CancellationToken cancellationToken) { var args = extractChapters @@ -164,6 +181,7 @@ namespace MediaBrowser.MediaEncoding.Encoder // Must consume both or ffmpeg may hang due to deadlocks. See comments below. RedirectStandardOutput = true, RedirectStandardError = true, + RedirectStandardInput = true, FileName = FFProbePath, Arguments = string.Format(args, probeSizeArgument, inputPath).Trim(), @@ -177,15 +195,13 @@ namespace MediaBrowser.MediaEncoding.Encoder _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments); - process.Exited += ProcessExited; - await _ffProbeResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); - InternalMediaInfoResult result; + var processWrapper = new ProcessWrapper(process, this); try { - process.Start(); + StartProcess(processWrapper); } catch (Exception ex) { @@ -200,19 +216,57 @@ namespace MediaBrowser.MediaEncoding.Encoder { process.BeginErrorReadLine(); - result = _jsonSerializer.DeserializeFromStream<InternalMediaInfoResult>(process.StandardOutput.BaseStream); + var result = _jsonSerializer.DeserializeFromStream<InternalMediaInfoResult>(process.StandardOutput.BaseStream); + + if (result != null) + { + if (result.streams != null) + { + // Normalize aspect ratio if invalid + foreach (var stream in result.streams) + { + if (string.Equals(stream.display_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase)) + { + stream.display_aspect_ratio = string.Empty; + } + if (string.Equals(stream.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase)) + { + stream.sample_aspect_ratio = string.Empty; + } + } + } + + var mediaInfo = new ProbeResultNormalizer(_logger, FileSystem).GetMediaInfo(result, isAudio, primaryPath, protocol); + + if (extractKeyFrameInterval && mediaInfo.RunTimeTicks.HasValue) + { + foreach (var stream in mediaInfo.MediaStreams) + { + if (stream.Type == MediaStreamType.Video && string.Equals(stream.Codec, "h264", StringComparison.OrdinalIgnoreCase)) + { + try + { + //stream.KeyFrames = await GetKeyFrames(inputPath, stream.Index, cancellationToken) + // .ConfigureAwait(false); + } + catch (OperationCanceledException) + { + + } + catch (Exception ex) + { + _logger.ErrorException("Error getting key frame interval", ex); + } + } + } + } + + return mediaInfo; + } } catch { - // Hate having to do this - try - { - process.Kill(); - } - catch (Exception ex1) - { - _logger.ErrorException("Error killing ffprobe", ex1); - } + StopProcess(processWrapper, 100, true); throw; } @@ -221,30 +275,102 @@ namespace MediaBrowser.MediaEncoding.Encoder _ffProbeResourcePool.Release(); } - if (result == null) + throw new ApplicationException(string.Format("FFProbe failed for {0}", inputPath)); + } + + private async Task<List<int>> GetKeyFrames(string inputPath, int videoStreamIndex, CancellationToken cancellationToken) + { + const string args = "-i {0} -select_streams v:{1} -show_frames -show_entries frame=pkt_dts,key_frame -print_format compact"; + + var process = new Process { - throw new ApplicationException(string.Format("FFProbe failed for {0}", inputPath)); + 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 = FFProbePath, + Arguments = string.Format(args, inputPath, videoStreamIndex.ToString(CultureInfo.InvariantCulture)).Trim(), + + WindowStyle = ProcessWindowStyle.Hidden, + ErrorDialog = false + }, + + EnableRaisingEvents = true + }; + + _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments); + + var processWrapper = new ProcessWrapper(process, this); + + StartProcess(processWrapper); + + var lines = new List<int>(); + + try + { + process.BeginErrorReadLine(); + + await StartReadingOutput(process.StandardOutput.BaseStream, lines, 120000, cancellationToken).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + if (cancellationToken.IsCancellationRequested) + { + throw; + } + } + finally + { + StopProcess(processWrapper, 100, true); } - cancellationToken.ThrowIfCancellationRequested(); + return lines; + } - if (result.streams != null) + private async Task StartReadingOutput(Stream source, List<int> lines, int timeoutMs, CancellationToken cancellationToken) + { + try { - // Normalize aspect ratio if invalid - foreach (var stream in result.streams) + using (var reader = new StreamReader(source)) { - if (string.Equals(stream.display_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase)) + while (!reader.EndOfStream) { - stream.display_aspect_ratio = string.Empty; - } - if (string.Equals(stream.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase)) - { - stream.sample_aspect_ratio = string.Empty; + cancellationToken.ThrowIfCancellationRequested(); + + var line = await reader.ReadLineAsync().ConfigureAwait(false); + + var values = (line ?? string.Empty).Split('|') + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Select(i => i.Split('=')) + .Where(i => i.Length == 2) + .ToDictionary(i => i[0], i => i[1]); + + string pktDts; + int frameMs; + if (values.TryGetValue("pkt_dts", out pktDts) && int.TryParse(pktDts, NumberStyles.Any, CultureInfo.InvariantCulture, out frameMs)) + { + string keyFrame; + if (values.TryGetValue("key_frame", out keyFrame) && string.Equals(keyFrame, "1", StringComparison.OrdinalIgnoreCase)) + { + lines.Add(frameMs); + } + } } } } - - return result; + catch (OperationCanceledException) + { + throw; + } + catch (Exception ex) + { + _logger.ErrorException("Error reading ffprobe output", ex); + } } /// <summary> @@ -252,16 +378,6 @@ namespace MediaBrowser.MediaEncoding.Encoder /// </summary> protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); - /// <summary> - /// Processes the exited. - /// </summary> - /// <param name="sender">The sender.</param> - /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> - private void ProcessExited(object sender, EventArgs e) - { - ((Process)sender).Dispose(); - } - public Task<Stream> ExtractAudioImage(string path, CancellationToken cancellationToken) { return ExtractImage(new[] { path }, MediaProtocol.File, true, null, null, cancellationToken); @@ -286,6 +402,10 @@ namespace MediaBrowser.MediaEncoding.Encoder { return await ExtractImageInternal(inputArgument, protocol, threedFormat, offset, true, resourcePool, cancellationToken).ConfigureAwait(false); } + catch (ArgumentException) + { + throw; + } catch { _logger.Error("I-frame image extraction failed, will attempt standard way. Input: {0}", inputArgument); @@ -368,41 +488,37 @@ namespace MediaBrowser.MediaEncoding.Encoder await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); - process.Start(); + var processWrapper = new ProcessWrapper(process, this); + bool ranToCompletion; var memoryStream = new MemoryStream(); + try + { + StartProcess(processWrapper); + #pragma warning disable 4014 - // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback - process.StandardOutput.BaseStream.CopyToAsync(memoryStream); + // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback + process.StandardOutput.BaseStream.CopyToAsync(memoryStream); #pragma warning restore 4014 - // MUST read both stdout and stderr asynchronously or a deadlock may occurr - process.BeginErrorReadLine(); - - var ranToCompletion = process.WaitForExit(10000); - - if (!ranToCompletion) - { - try - { - _logger.Info("Killing ffmpeg process"); + // MUST read both stdout and stderr asynchronously or a deadlock may occurr + process.BeginErrorReadLine(); - process.StandardInput.WriteLine("q"); + ranToCompletion = process.WaitForExit(10000); - process.WaitForExit(1000); - } - catch (Exception ex) + if (!ranToCompletion) { - _logger.ErrorException("Error killing process", ex); + StopProcess(processWrapper, 1000, false); } - } - resourcePool.Release(); - - var exitCode = ranToCompletion ? process.ExitCode : -1; + } + finally + { + resourcePool.Release(); + } - process.Dispose(); + var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1; if (exitCode == -1 || memoryStream.Length == 0) { @@ -419,31 +535,6 @@ namespace MediaBrowser.MediaEncoding.Encoder return memoryStream; } - public Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - /// <summary> - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// </summary> - public void Dispose() - { - Dispose(true); - } - - /// <summary> - /// Releases unmanaged and - optionally - managed resources. - /// </summary> - /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> - protected virtual void Dispose(bool dispose) - { - if (dispose) - { - _videoImageResourcePool.Dispose(); - } - } - public string GetTimeParameter(long ticks) { var time = TimeSpan.FromTicks(ticks); @@ -508,11 +599,13 @@ namespace MediaBrowser.MediaEncoding.Encoder await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); - bool ranToCompletion; + bool ranToCompletion = false; + + var processWrapper = new ProcessWrapper(process, this); try { - process.Start(); + StartProcess(processWrapper); // Need to give ffmpeg enough time to make all the thumbnails, which could be a while, // but we still need to detect if the process hangs. @@ -521,33 +614,26 @@ namespace MediaBrowser.MediaEncoding.Encoder bool isResponsive = true; int lastCount = 0; - while (isResponsive && !process.WaitForExit(30000)) + while (isResponsive) { + if (process.WaitForExit(30000)) + { + ranToCompletion = true; + break; + } + cancellationToken.ThrowIfCancellationRequested(); - int jpegCount = Directory.GetFiles(targetDirectory) + var jpegCount = Directory.GetFiles(targetDirectory) .Count(i => string.Equals(Path.GetExtension(i), ".jpg", StringComparison.OrdinalIgnoreCase)); isResponsive = (jpegCount > lastCount); lastCount = jpegCount; } - ranToCompletion = process.HasExited; - if (!ranToCompletion) { - try - { - _logger.Info("Killing ffmpeg process"); - - process.StandardInput.WriteLine("q"); - - process.WaitForExit(1000); - } - catch (Exception ex) - { - _logger.ErrorException("Error killing process", ex); - } + StopProcess(processWrapper, 1000, false); } } finally @@ -555,9 +641,7 @@ namespace MediaBrowser.MediaEncoding.Encoder resourcePool.Release(); } - var exitCode = ranToCompletion ? process.ExitCode : -1; - - process.Dispose(); + var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1; if (exitCode == -1) { @@ -608,5 +692,122 @@ namespace MediaBrowser.MediaEncoding.Encoder return job.OutputFilePath; } + + private void StartProcess(ProcessWrapper process) + { + process.Process.Start(); + + lock (_runningProcesses) + { + _runningProcesses.Add(process); + } + } + private void StopProcess(ProcessWrapper process, int waitTimeMs, bool enableForceKill) + { + try + { + _logger.Info("Killing ffmpeg process"); + + try + { + process.Process.StandardInput.WriteLine("q"); + } + catch (Exception) + { + _logger.Error("Error sending q command to process"); + } + + try + { + if (process.Process.WaitForExit(waitTimeMs)) + { + return; + } + } + catch (Exception ex) + { + _logger.Error("Error in WaitForExit", ex); + } + + if (enableForceKill) + { + process.Process.Kill(); + } + } + catch (Exception ex) + { + _logger.ErrorException("Error killing process", ex); + } + } + + private void StopProcesses() + { + List<ProcessWrapper> proceses; + lock (_runningProcesses) + { + proceses = _runningProcesses.ToList(); + } + _runningProcesses.Clear(); + + foreach (var process in proceses) + { + if (!process.HasExited) + { + StopProcess(process, 500, true); + } + } + } + + /// <summary> + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// </summary> + public void Dispose() + { + Dispose(true); + } + + /// <summary> + /// Releases unmanaged and - optionally - managed resources. + /// </summary> + /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> + protected virtual void Dispose(bool dispose) + { + if (dispose) + { + _videoImageResourcePool.Dispose(); + StopProcesses(); + } + } + + private class ProcessWrapper + { + public readonly Process Process; + public bool HasExited; + public int? ExitCode; + private readonly MediaEncoder _mediaEncoder; + + public ProcessWrapper(Process process, MediaEncoder mediaEncoder) + { + Process = process; + this._mediaEncoder = mediaEncoder; + Process.Exited += Process_Exited; + } + + void Process_Exited(object sender, EventArgs e) + { + var process = (Process)sender; + + HasExited = true; + + ExitCode = process.ExitCode; + + lock (_mediaEncoder._runningProcesses) + { + _mediaEncoder._runningProcesses.Remove(this); + } + + process.Dispose(); + } + } } } diff --git a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj index 72dc0feac..ad561c484 100644 --- a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj +++ b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj @@ -68,6 +68,9 @@ <Compile Include="Encoder\JobLogger.cs" /> <Compile Include="Encoder\MediaEncoder.cs" /> <Compile Include="Encoder\VideoEncoder.cs" /> + <Compile Include="Probing\FFProbeHelpers.cs" /> + <Compile Include="Probing\InternalMediaInfoResult.cs" /> + <Compile Include="Probing\ProbeResultNormalizer.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Subtitles\ISubtitleParser.cs" /> <Compile Include="Subtitles\ISubtitleWriter.cs" /> @@ -91,6 +94,10 @@ <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> @@ -99,7 +106,9 @@ <ItemGroup> <None Include="packages.config" /> </ItemGroup> - <ItemGroup /> + <ItemGroup> + <EmbeddedResource Include="Probing\whitelist.txt" /> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. diff --git a/MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs b/MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs new file mode 100644 index 000000000..859f38950 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs @@ -0,0 +1,118 @@ +using MediaBrowser.Controller.MediaEncoding; +using System; +using System.Collections.Generic; + +namespace MediaBrowser.MediaEncoding.Probing +{ + public static class FFProbeHelpers + { + /// <summary> + /// Normalizes the FF probe result. + /// </summary> + /// <param name="result">The result.</param> + public static void NormalizeFFProbeResult(InternalMediaInfoResult result) + { + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (result.format != null && result.format.tags != null) + { + result.format.tags = ConvertDictionaryToCaseInSensitive(result.format.tags); + } + + if (result.streams != null) + { + // Convert all dictionaries to case insensitive + foreach (var stream in result.streams) + { + if (stream.tags != null) + { + stream.tags = ConvertDictionaryToCaseInSensitive(stream.tags); + } + + if (stream.disposition != null) + { + stream.disposition = ConvertDictionaryToCaseInSensitive(stream.disposition); + } + } + } + } + + /// <summary> + /// Gets a string from an FFProbeResult tags dictionary + /// </summary> + /// <param name="tags">The tags.</param> + /// <param name="key">The key.</param> + /// <returns>System.String.</returns> + public static string GetDictionaryValue(Dictionary<string, string> tags, string key) + { + if (tags == null) + { + return null; + } + + string val; + + tags.TryGetValue(key, out val); + return val; + } + + /// <summary> + /// Gets an int from an FFProbeResult tags dictionary + /// </summary> + /// <param name="tags">The tags.</param> + /// <param name="key">The key.</param> + /// <returns>System.Nullable{System.Int32}.</returns> + public static int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key) + { + var val = GetDictionaryValue(tags, key); + + if (!string.IsNullOrEmpty(val)) + { + int i; + + if (int.TryParse(val, out i)) + { + return i; + } + } + + return null; + } + + /// <summary> + /// Gets a DateTime from an FFProbeResult tags dictionary + /// </summary> + /// <param name="tags">The tags.</param> + /// <param name="key">The key.</param> + /// <returns>System.Nullable{DateTime}.</returns> + public static DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key) + { + var val = GetDictionaryValue(tags, key); + + if (!string.IsNullOrEmpty(val)) + { + DateTime i; + + if (DateTime.TryParse(val, out i)) + { + return i.ToUniversalTime(); + } + } + + return null; + } + + /// <summary> + /// Converts a dictionary to case insensitive + /// </summary> + /// <param name="dict">The dict.</param> + /// <returns>Dictionary{System.StringSystem.String}.</returns> + private static Dictionary<string, string> ConvertDictionaryToCaseInSensitive(Dictionary<string, string> dict) + { + return new Dictionary<string, string>(dict, StringComparer.OrdinalIgnoreCase); + } + } +} diff --git a/MediaBrowser.MediaEncoding/Probing/InternalMediaInfoResult.cs b/MediaBrowser.MediaEncoding/Probing/InternalMediaInfoResult.cs new file mode 100644 index 000000000..9b95e83e7 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Probing/InternalMediaInfoResult.cs @@ -0,0 +1,327 @@ +using System.Collections.Generic; + +namespace MediaBrowser.MediaEncoding.Probing +{ + /// <summary> + /// Class MediaInfoResult + /// </summary> + public class InternalMediaInfoResult + { + /// <summary> + /// Gets or sets the streams. + /// </summary> + /// <value>The streams.</value> + public MediaStreamInfo[] streams { get; set; } + + /// <summary> + /// Gets or sets the format. + /// </summary> + /// <value>The format.</value> + public MediaFormatInfo format { get; set; } + + /// <summary> + /// Gets or sets the chapters. + /// </summary> + /// <value>The chapters.</value> + public MediaChapter[] Chapters { get; set; } + } + + public class MediaChapter + { + public int id { get; set; } + public string time_base { get; set; } + public long start { get; set; } + public string start_time { get; set; } + public long end { get; set; } + public string end_time { get; set; } + public Dictionary<string, string> tags { get; set; } + } + + /// <summary> + /// Represents a stream within the output + /// </summary> + public class MediaStreamInfo + { + /// <summary> + /// Gets or sets the index. + /// </summary> + /// <value>The index.</value> + public int index { get; set; } + + /// <summary> + /// Gets or sets the profile. + /// </summary> + /// <value>The profile.</value> + public string profile { get; set; } + + /// <summary> + /// Gets or sets the codec_name. + /// </summary> + /// <value>The codec_name.</value> + public string codec_name { get; set; } + + /// <summary> + /// Gets or sets the codec_long_name. + /// </summary> + /// <value>The codec_long_name.</value> + public string codec_long_name { get; set; } + + /// <summary> + /// Gets or sets the codec_type. + /// </summary> + /// <value>The codec_type.</value> + public string codec_type { get; set; } + + /// <summary> + /// Gets or sets the sample_rate. + /// </summary> + /// <value>The sample_rate.</value> + public string sample_rate { get; set; } + + /// <summary> + /// Gets or sets the channels. + /// </summary> + /// <value>The channels.</value> + public int channels { get; set; } + + /// <summary> + /// Gets or sets the channel_layout. + /// </summary> + /// <value>The channel_layout.</value> + public string channel_layout { get; set; } + + /// <summary> + /// Gets or sets the avg_frame_rate. + /// </summary> + /// <value>The avg_frame_rate.</value> + public string avg_frame_rate { get; set; } + + /// <summary> + /// Gets or sets the duration. + /// </summary> + /// <value>The duration.</value> + public string duration { get; set; } + + /// <summary> + /// Gets or sets the bit_rate. + /// </summary> + /// <value>The bit_rate.</value> + public string bit_rate { get; set; } + + /// <summary> + /// Gets or sets the width. + /// </summary> + /// <value>The width.</value> + public int width { get; set; } + + /// <summary> + /// Gets or sets the height. + /// </summary> + /// <value>The height.</value> + public int height { get; set; } + + /// <summary> + /// Gets or sets the display_aspect_ratio. + /// </summary> + /// <value>The display_aspect_ratio.</value> + public string display_aspect_ratio { get; set; } + + /// <summary> + /// Gets or sets the tags. + /// </summary> + /// <value>The tags.</value> + public Dictionary<string, string> tags { get; set; } + + /// <summary> + /// Gets or sets the bits_per_sample. + /// </summary> + /// <value>The bits_per_sample.</value> + public int bits_per_sample { get; set; } + + /// <summary> + /// Gets or sets the r_frame_rate. + /// </summary> + /// <value>The r_frame_rate.</value> + public string r_frame_rate { get; set; } + + /// <summary> + /// Gets or sets the has_b_frames. + /// </summary> + /// <value>The has_b_frames.</value> + public int has_b_frames { get; set; } + + /// <summary> + /// Gets or sets the sample_aspect_ratio. + /// </summary> + /// <value>The sample_aspect_ratio.</value> + public string sample_aspect_ratio { get; set; } + + /// <summary> + /// Gets or sets the pix_fmt. + /// </summary> + /// <value>The pix_fmt.</value> + public string pix_fmt { get; set; } + + /// <summary> + /// Gets or sets the level. + /// </summary> + /// <value>The level.</value> + public int level { get; set; } + + /// <summary> + /// Gets or sets the time_base. + /// </summary> + /// <value>The time_base.</value> + public string time_base { get; set; } + + /// <summary> + /// Gets or sets the start_time. + /// </summary> + /// <value>The start_time.</value> + public string start_time { get; set; } + + /// <summary> + /// Gets or sets the codec_time_base. + /// </summary> + /// <value>The codec_time_base.</value> + public string codec_time_base { get; set; } + + /// <summary> + /// Gets or sets the codec_tag. + /// </summary> + /// <value>The codec_tag.</value> + public string codec_tag { get; set; } + + /// <summary> + /// Gets or sets the codec_tag_string. + /// </summary> + /// <value>The codec_tag_string.</value> + public string codec_tag_string { get; set; } + + /// <summary> + /// Gets or sets the sample_fmt. + /// </summary> + /// <value>The sample_fmt.</value> + public string sample_fmt { get; set; } + + /// <summary> + /// Gets or sets the dmix_mode. + /// </summary> + /// <value>The dmix_mode.</value> + public string dmix_mode { get; set; } + + /// <summary> + /// Gets or sets the start_pts. + /// </summary> + /// <value>The start_pts.</value> + public string start_pts { get; set; } + + /// <summary> + /// Gets or sets the is_avc. + /// </summary> + /// <value>The is_avc.</value> + public string is_avc { get; set; } + + /// <summary> + /// Gets or sets the nal_length_size. + /// </summary> + /// <value>The nal_length_size.</value> + public string nal_length_size { get; set; } + + /// <summary> + /// Gets or sets the ltrt_cmixlev. + /// </summary> + /// <value>The ltrt_cmixlev.</value> + public string ltrt_cmixlev { get; set; } + + /// <summary> + /// Gets or sets the ltrt_surmixlev. + /// </summary> + /// <value>The ltrt_surmixlev.</value> + public string ltrt_surmixlev { get; set; } + + /// <summary> + /// Gets or sets the loro_cmixlev. + /// </summary> + /// <value>The loro_cmixlev.</value> + public string loro_cmixlev { get; set; } + + /// <summary> + /// Gets or sets the loro_surmixlev. + /// </summary> + /// <value>The loro_surmixlev.</value> + public string loro_surmixlev { get; set; } + + /// <summary> + /// Gets or sets the disposition. + /// </summary> + /// <value>The disposition.</value> + public Dictionary<string, string> disposition { get; set; } + } + + /// <summary> + /// Class MediaFormat + /// </summary> + public class MediaFormatInfo + { + /// <summary> + /// Gets or sets the filename. + /// </summary> + /// <value>The filename.</value> + public string filename { get; set; } + + /// <summary> + /// Gets or sets the nb_streams. + /// </summary> + /// <value>The nb_streams.</value> + public int nb_streams { get; set; } + + /// <summary> + /// Gets or sets the format_name. + /// </summary> + /// <value>The format_name.</value> + public string format_name { get; set; } + + /// <summary> + /// Gets or sets the format_long_name. + /// </summary> + /// <value>The format_long_name.</value> + public string format_long_name { get; set; } + + /// <summary> + /// Gets or sets the start_time. + /// </summary> + /// <value>The start_time.</value> + public string start_time { get; set; } + + /// <summary> + /// Gets or sets the duration. + /// </summary> + /// <value>The duration.</value> + public string duration { get; set; } + + /// <summary> + /// Gets or sets the size. + /// </summary> + /// <value>The size.</value> + public string size { get; set; } + + /// <summary> + /// Gets or sets the bit_rate. + /// </summary> + /// <value>The bit_rate.</value> + public string bit_rate { get; set; } + + /// <summary> + /// Gets or sets the probe_score. + /// </summary> + /// <value>The probe_score.</value> + public int probe_score { get; set; } + + /// <summary> + /// Gets or sets the tags. + /// </summary> + /// <value>The tags.</value> + public Dictionary<string, string> tags { get; set; } + } +} diff --git a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs new file mode 100644 index 000000000..267ff875a --- /dev/null +++ b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs @@ -0,0 +1,887 @@ +using MediaBrowser.Common.IO; +using MediaBrowser.MediaInfo; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Extensions; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; + +namespace MediaBrowser.MediaEncoding.Probing +{ + public class ProbeResultNormalizer + { + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + private readonly ILogger _logger; + private readonly IFileSystem _fileSystem; + + public ProbeResultNormalizer(ILogger logger, IFileSystem fileSystem) + { + _logger = logger; + _fileSystem = fileSystem; + } + + public Model.MediaInfo.MediaInfo GetMediaInfo(InternalMediaInfoResult data, bool isAudio, string path, MediaProtocol protocol) + { + var info = new Model.MediaInfo.MediaInfo + { + Path = path, + Protocol = protocol + }; + + FFProbeHelpers.NormalizeFFProbeResult(data); + SetSize(data, info); + + var internalStreams = data.streams ?? new MediaStreamInfo[] { }; + + info.MediaStreams = internalStreams.Select(s => GetMediaStream(s, data.format)) + .Where(i => i != null) + .ToList(); + + if (data.format != null) + { + info.Container = data.format.format_name; + + if (!string.IsNullOrEmpty(data.format.bit_rate)) + { + info.Bitrate = int.Parse(data.format.bit_rate, _usCulture); + } + } + + if (isAudio) + { + SetAudioRuntimeTicks(data, info); + + if (data.format != null && data.format.tags != null) + { + SetAudioInfoFromTags(info, data.format.tags); + } + } + else + { + if (data.format != null && !string.IsNullOrEmpty(data.format.duration)) + { + info.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(data.format.duration, _usCulture)).Ticks; + } + + FetchWtvInfo(info, data); + + if (data.Chapters != null) + { + info.Chapters = data.Chapters.Select(GetChapterInfo).ToList(); + } + + ExtractTimestamp(info); + + var videoStream = info.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video); + + if (videoStream != null) + { + UpdateFromMediaInfo(info, videoStream); + } + } + + return info; + } + + /// <summary> + /// Converts ffprobe stream info to our MediaStream class + /// </summary> + /// <param name="streamInfo">The stream info.</param> + /// <param name="formatInfo">The format info.</param> + /// <returns>MediaStream.</returns> + private MediaStream GetMediaStream(MediaStreamInfo streamInfo, MediaFormatInfo formatInfo) + { + var stream = new MediaStream + { + Codec = streamInfo.codec_name, + Profile = streamInfo.profile, + Level = streamInfo.level, + Index = streamInfo.index, + PixelFormat = streamInfo.pix_fmt + }; + + if (streamInfo.tags != null) + { + stream.Language = GetDictionaryValue(streamInfo.tags, "language"); + } + + if (string.Equals(streamInfo.codec_type, "audio", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = MediaStreamType.Audio; + + stream.Channels = streamInfo.channels; + + if (!string.IsNullOrEmpty(streamInfo.sample_rate)) + { + stream.SampleRate = int.Parse(streamInfo.sample_rate, _usCulture); + } + + stream.ChannelLayout = ParseChannelLayout(streamInfo.channel_layout); + } + else if (string.Equals(streamInfo.codec_type, "subtitle", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = MediaStreamType.Subtitle; + } + else if (string.Equals(streamInfo.codec_type, "video", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = (streamInfo.codec_name ?? string.Empty).IndexOf("mjpeg", StringComparison.OrdinalIgnoreCase) != -1 + ? MediaStreamType.EmbeddedImage + : MediaStreamType.Video; + + stream.Width = streamInfo.width; + stream.Height = streamInfo.height; + stream.AspectRatio = GetAspectRatio(streamInfo); + + stream.AverageFrameRate = GetFrameRate(streamInfo.avg_frame_rate); + stream.RealFrameRate = GetFrameRate(streamInfo.r_frame_rate); + + stream.BitDepth = GetBitDepth(stream.PixelFormat); + + //stream.IsAnamorphic = string.Equals(streamInfo.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase) || + // string.Equals(stream.AspectRatio, "2.35:1", StringComparison.OrdinalIgnoreCase) || + // string.Equals(stream.AspectRatio, "2.40:1", StringComparison.OrdinalIgnoreCase); + + stream.IsAnamorphic = string.Equals(streamInfo.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase); + } + else + { + return null; + } + + // Get stream bitrate + var bitrate = 0; + + if (!string.IsNullOrEmpty(streamInfo.bit_rate)) + { + bitrate = int.Parse(streamInfo.bit_rate, _usCulture); + } + else if (formatInfo != null && !string.IsNullOrEmpty(formatInfo.bit_rate) && stream.Type == MediaStreamType.Video) + { + // If the stream info doesn't have a bitrate get the value from the media format info + bitrate = int.Parse(formatInfo.bit_rate, _usCulture); + } + + if (bitrate > 0) + { + stream.BitRate = bitrate; + } + + if (streamInfo.disposition != null) + { + var isDefault = GetDictionaryValue(streamInfo.disposition, "default"); + var isForced = GetDictionaryValue(streamInfo.disposition, "forced"); + + stream.IsDefault = string.Equals(isDefault, "1", StringComparison.OrdinalIgnoreCase); + + stream.IsForced = string.Equals(isForced, "1", StringComparison.OrdinalIgnoreCase); + } + + return stream; + } + + private int? GetBitDepth(string pixelFormat) + { + var eightBit = new List<string> + { + "yuv420p", + "yuv411p", + "yuvj420p", + "uyyvyy411", + "nv12", + "nv21", + "rgb444le", + "rgb444be", + "bgr444le", + "bgr444be", + "yuvj411p" + }; + + if (!string.IsNullOrEmpty(pixelFormat)) + { + if (eightBit.Contains(pixelFormat, StringComparer.OrdinalIgnoreCase)) + { + return 8; + } + } + + return null; + } + + /// <summary> + /// Gets a string from an FFProbeResult tags dictionary + /// </summary> + /// <param name="tags">The tags.</param> + /// <param name="key">The key.</param> + /// <returns>System.String.</returns> + private string GetDictionaryValue(Dictionary<string, string> tags, string key) + { + if (tags == null) + { + return null; + } + + string val; + + tags.TryGetValue(key, out val); + return val; + } + + private string ParseChannelLayout(string input) + { + if (string.IsNullOrEmpty(input)) + { + return input; + } + + return input.Split('(').FirstOrDefault(); + } + + private string GetAspectRatio(MediaStreamInfo info) + { + var original = info.display_aspect_ratio; + + int height; + int width; + + var parts = (original ?? string.Empty).Split(':'); + if (!(parts.Length == 2 && + int.TryParse(parts[0], NumberStyles.Any, _usCulture, out width) && + int.TryParse(parts[1], NumberStyles.Any, _usCulture, out height) && + width > 0 && + height > 0)) + { + width = info.width; + height = info.height; + } + + if (width > 0 && height > 0) + { + double ratio = width; + ratio /= height; + + if (IsClose(ratio, 1.777777778, .03)) + { + return "16:9"; + } + + if (IsClose(ratio, 1.3333333333, .05)) + { + return "4:3"; + } + + if (IsClose(ratio, 1.41)) + { + return "1.41:1"; + } + + if (IsClose(ratio, 1.5)) + { + return "1.5:1"; + } + + if (IsClose(ratio, 1.6)) + { + return "1.6:1"; + } + + if (IsClose(ratio, 1.66666666667)) + { + return "5:3"; + } + + if (IsClose(ratio, 1.85, .02)) + { + return "1.85:1"; + } + + if (IsClose(ratio, 2.35, .025)) + { + return "2.35:1"; + } + + if (IsClose(ratio, 2.4, .025)) + { + return "2.40:1"; + } + } + + return original; + } + + private bool IsClose(double d1, double d2, double variance = .005) + { + return Math.Abs(d1 - d2) <= variance; + } + + /// <summary> + /// Gets a frame rate from a string value in ffprobe output + /// This could be a number or in the format of 2997/125. + /// </summary> + /// <param name="value">The value.</param> + /// <returns>System.Nullable{System.Single}.</returns> + private float? GetFrameRate(string value) + { + if (!string.IsNullOrEmpty(value)) + { + var parts = value.Split('/'); + + float result; + + if (parts.Length == 2) + { + result = float.Parse(parts[0], _usCulture) / float.Parse(parts[1], _usCulture); + } + else + { + result = float.Parse(parts[0], _usCulture); + } + + return float.IsNaN(result) ? (float?)null : result; + } + + return null; + } + + private void SetAudioRuntimeTicks(InternalMediaInfoResult result, Model.MediaInfo.MediaInfo data) + { + if (result.streams != null) + { + // Get the first audio stream + var stream = result.streams.FirstOrDefault(s => string.Equals(s.codec_type, "audio", StringComparison.OrdinalIgnoreCase)); + + if (stream != null) + { + // Get duration from stream properties + var duration = stream.duration; + + // If it's not there go into format properties + if (string.IsNullOrEmpty(duration)) + { + duration = result.format.duration; + } + + // If we got something, parse it + if (!string.IsNullOrEmpty(duration)) + { + data.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration, _usCulture)).Ticks; + } + } + } + } + + private void SetSize(InternalMediaInfoResult data, Model.MediaInfo.MediaInfo info) + { + if (data.format != null) + { + if (!string.IsNullOrEmpty(data.format.size)) + { + info.Size = long.Parse(data.format.size, _usCulture); + } + else + { + info.Size = null; + } + } + } + + private void SetAudioInfoFromTags(Model.MediaInfo.MediaInfo audio, Dictionary<string, string> tags) + { + var title = FFProbeHelpers.GetDictionaryValue(tags, "title"); + + // Only set Name if title was found in the dictionary + if (!string.IsNullOrEmpty(title)) + { + audio.Title = title; + } + + var composer = FFProbeHelpers.GetDictionaryValue(tags, "composer"); + + if (!string.IsNullOrWhiteSpace(composer)) + { + foreach (var person in Split(composer, false)) + { + audio.People.Add(new BaseItemPerson { Name = person, Type = PersonType.Composer }); + } + } + + audio.Album = FFProbeHelpers.GetDictionaryValue(tags, "album"); + + var artists = FFProbeHelpers.GetDictionaryValue(tags, "artists"); + + if (!string.IsNullOrWhiteSpace(artists)) + { + audio.Artists = artists.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + else + { + var artist = FFProbeHelpers.GetDictionaryValue(tags, "artist"); + if (string.IsNullOrWhiteSpace(artist)) + { + audio.Artists.Clear(); + } + else + { + audio.Artists = SplitArtists(artist) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + } + + var albumArtist = FFProbeHelpers.GetDictionaryValue(tags, "albumartist"); + if (string.IsNullOrWhiteSpace(albumArtist)) + { + albumArtist = FFProbeHelpers.GetDictionaryValue(tags, "album artist"); + } + if (string.IsNullOrWhiteSpace(albumArtist)) + { + albumArtist = FFProbeHelpers.GetDictionaryValue(tags, "album_artist"); + } + + if (string.IsNullOrWhiteSpace(albumArtist)) + { + audio.AlbumArtists = new List<string>(); + } + else + { + audio.AlbumArtists = SplitArtists(albumArtist) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + + } + + // Track number + audio.IndexNumber = GetDictionaryDiscValue(tags, "track"); + + // Disc number + audio.ParentIndexNumber = GetDictionaryDiscValue(tags, "disc"); + + audio.ProductionYear = FFProbeHelpers.GetDictionaryNumericValue(tags, "date"); + + // Several different forms of retaildate + audio.PremiereDate = FFProbeHelpers.GetDictionaryDateTime(tags, "retaildate") ?? + FFProbeHelpers.GetDictionaryDateTime(tags, "retail date") ?? + FFProbeHelpers.GetDictionaryDateTime(tags, "retail_date") ?? + FFProbeHelpers.GetDictionaryDateTime(tags, "date"); + + // If we don't have a ProductionYear try and get it from PremiereDate + if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue) + { + audio.ProductionYear = audio.PremiereDate.Value.ToLocalTime().Year; + } + + FetchGenres(audio, tags); + + // There's several values in tags may or may not be present + FetchStudios(audio, tags, "organization"); + FetchStudios(audio, tags, "ensemble"); + FetchStudios(audio, tags, "publisher"); + + // These support mulitple values, but for now we only store the first. + audio.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Album Artist Id"))); + audio.SetProviderId(MetadataProviders.MusicBrainzArtist, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Artist Id"))); + + audio.SetProviderId(MetadataProviders.MusicBrainzAlbum, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Album Id"))); + audio.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Release Group Id"))); + audio.SetProviderId(MetadataProviders.MusicBrainzTrack, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Release Track Id"))); + } + + private string GetMultipleMusicBrainzId(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return null; + } + + return value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries) + .Select(i => i.Trim()) + .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i)); + } + + private readonly char[] _nameDelimiters = { '/', '|', ';', '\\' }; + + /// <summary> + /// Splits the specified val. + /// </summary> + /// <param name="val">The val.</param> + /// <param name="allowCommaDelimiter">if set to <c>true</c> [allow comma delimiter].</param> + /// <returns>System.String[][].</returns> + private IEnumerable<string> Split(string val, bool allowCommaDelimiter) + { + // Only use the comma as a delimeter if there are no slashes or pipes. + // We want to be careful not to split names that have commas in them + var delimeter = !allowCommaDelimiter || _nameDelimiters.Any(i => val.IndexOf(i) != -1) ? + _nameDelimiters : + new[] { ',' }; + + return val.Split(delimeter, StringSplitOptions.RemoveEmptyEntries) + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Select(i => i.Trim()); + } + + private const string ArtistReplaceValue = " | "; + + private IEnumerable<string> SplitArtists(string val) + { + val = val.Replace(" featuring ", ArtistReplaceValue, StringComparison.OrdinalIgnoreCase) + .Replace(" feat. ", ArtistReplaceValue, StringComparison.OrdinalIgnoreCase); + + var artistsFound = new List<string>(); + + foreach (var whitelistArtist in GetSplitWhitelist()) + { + var originalVal = val; + val = val.Replace(whitelistArtist, "|", StringComparison.OrdinalIgnoreCase); + + if (!string.Equals(originalVal, val, StringComparison.OrdinalIgnoreCase)) + { + artistsFound.Add(whitelistArtist); + } + } + + // Only use the comma as a delimeter if there are no slashes or pipes. + // We want to be careful not to split names that have commas in them + var delimeter = _nameDelimiters; + + var artists = val.Split(delimeter, StringSplitOptions.RemoveEmptyEntries) + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Select(i => i.Trim()); + + artistsFound.AddRange(artists); + return artistsFound; + } + + + private List<string> _splitWhiteList = null; + + private IEnumerable<string> GetSplitWhitelist() + { + if (_splitWhiteList == null) + { + var file = GetType().Namespace + ".whitelist.txt"; + + using (var stream = GetType().Assembly.GetManifestResourceStream(file)) + { + using (var reader = new StreamReader(stream)) + { + var list = new List<string>(); + + while (!reader.EndOfStream) + { + var val = reader.ReadLine(); + + if (!string.IsNullOrWhiteSpace(val)) + { + list.Add(val); + } + } + + _splitWhiteList = list; + } + } + } + + return _splitWhiteList; + } + + /// <summary> + /// Gets the studios from the tags collection + /// </summary> + /// <param name="audio">The audio.</param> + /// <param name="tags">The tags.</param> + /// <param name="tagName">Name of the tag.</param> + private void FetchStudios(Model.MediaInfo.MediaInfo audio, Dictionary<string, string> tags, string tagName) + { + var val = FFProbeHelpers.GetDictionaryValue(tags, tagName); + + if (!string.IsNullOrEmpty(val)) + { + var studios = Split(val, true); + + foreach (var studio in studios) + { + // Sometimes the artist name is listed here, account for that + if (audio.Artists.Contains(studio, StringComparer.OrdinalIgnoreCase)) + { + continue; + } + if (audio.AlbumArtists.Contains(studio, StringComparer.OrdinalIgnoreCase)) + { + continue; + } + + audio.Studios.Add(studio); + } + + audio.Studios = audio.Studios + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + } + + /// <summary> + /// Gets the genres from the tags collection + /// </summary> + /// <param name="info">The information.</param> + /// <param name="tags">The tags.</param> + private void FetchGenres(Model.MediaInfo.MediaInfo info, Dictionary<string, string> tags) + { + var val = FFProbeHelpers.GetDictionaryValue(tags, "genre"); + + if (!string.IsNullOrEmpty(val)) + { + foreach (var genre in Split(val, true)) + { + info.Genres.Add(genre); + } + + info.Genres = info.Genres + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + } + } + + /// <summary> + /// Gets the disc number, which is sometimes can be in the form of '1', or '1/3' + /// </summary> + /// <param name="tags">The tags.</param> + /// <param name="tagName">Name of the tag.</param> + /// <returns>System.Nullable{System.Int32}.</returns> + private int? GetDictionaryDiscValue(Dictionary<string, string> tags, string tagName) + { + var disc = FFProbeHelpers.GetDictionaryValue(tags, tagName); + + if (!string.IsNullOrEmpty(disc)) + { + disc = disc.Split('/')[0]; + + int num; + + if (int.TryParse(disc, out num)) + { + return num; + } + } + + return null; + } + + private ChapterInfo GetChapterInfo(MediaChapter chapter) + { + var info = new ChapterInfo(); + + if (chapter.tags != null) + { + string name; + if (chapter.tags.TryGetValue("title", out name)) + { + info.Name = name; + } + } + + // Limit accuracy to milliseconds to match xml saving + var secondsString = chapter.start_time; + double seconds; + + if (double.TryParse(secondsString, NumberStyles.Any, CultureInfo.InvariantCulture, out seconds)) + { + var ms = Math.Round(TimeSpan.FromSeconds(seconds).TotalMilliseconds); + info.StartPositionTicks = TimeSpan.FromMilliseconds(ms).Ticks; + } + + return info; + } + + private const int MaxSubtitleDescriptionExtractionLength = 100; // When extracting subtitles, the maximum length to consider (to avoid invalid filenames) + + private void FetchWtvInfo(Model.MediaInfo.MediaInfo video, InternalMediaInfoResult data) + { + if (data.format == null || data.format.tags == null) + { + return; + } + + var genres = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/Genre"); + + if (!string.IsNullOrWhiteSpace(genres)) + { + //genres = FFProbeHelpers.GetDictionaryValue(data.format.tags, "genre"); + } + + if (!string.IsNullOrWhiteSpace(genres)) + { + video.Genres = genres.Split(new[] { ';', '/', ',' }, StringSplitOptions.RemoveEmptyEntries) + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Select(i => i.Trim()) + .ToList(); + } + + var officialRating = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/ParentalRating"); + + if (!string.IsNullOrWhiteSpace(officialRating)) + { + video.OfficialRating = officialRating; + } + + var people = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/MediaCredits"); + + if (!string.IsNullOrEmpty(people)) + { + video.People = people.Split(new[] { ';', '/' }, StringSplitOptions.RemoveEmptyEntries) + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Select(i => new BaseItemPerson { Name = i.Trim(), Type = PersonType.Actor }) + .ToList(); + } + + var year = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/OriginalReleaseTime"); + if (!string.IsNullOrWhiteSpace(year)) + { + int val; + + if (int.TryParse(year, NumberStyles.Integer, _usCulture, out val)) + { + video.ProductionYear = val; + } + } + + var premiereDateString = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/MediaOriginalBroadcastDateTime"); + if (!string.IsNullOrWhiteSpace(premiereDateString)) + { + DateTime val; + + // Credit to MCEBuddy: https://mcebuddy2x.codeplex.com/ + // DateTime is reported along with timezone info (typically Z i.e. UTC hence assume None) + if (DateTime.TryParse(year, null, DateTimeStyles.None, out val)) + { + video.PremiereDate = val.ToUniversalTime(); + } + } + + var description = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/SubTitleDescription"); + + var subTitle = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/SubTitle"); + + // For below code, credit to MCEBuddy: https://mcebuddy2x.codeplex.com/ + + // Sometimes for TV Shows the Subtitle field is empty and the subtitle description contains the subtitle, extract if possible. See ticket https://mcebuddy2x.codeplex.com/workitem/1910 + // The format is -> EPISODE/TOTAL_EPISODES_IN_SEASON. SUBTITLE: DESCRIPTION + // OR -> COMMENT. SUBTITLE: DESCRIPTION + // e.g. -> 4/13. The Doctor's Wife: Science fiction drama. When he follows a Time Lord distress signal, the Doctor puts Amy, Rory and his beloved TARDIS in grave danger. Also in HD. [AD,S] + // e.g. -> CBeebies Bedtime Hour. The Mystery: Animated adventures of two friends who live on an island in the middle of the big city. Some of Abney and Teal's favourite objects are missing. [S] + if (String.IsNullOrWhiteSpace(subTitle) && !String.IsNullOrWhiteSpace(description) && description.Substring(0, Math.Min(description.Length, MaxSubtitleDescriptionExtractionLength)).Contains(":")) // Check within the Subtitle size limit, otherwise from description it can get too long creating an invalid filename + { + string[] parts = description.Split(':'); + if (parts.Length > 0) + { + string subtitle = parts[0]; + try + { + if (subtitle.Contains("/")) // It contains a episode number and season number + { + string[] numbers = subtitle.Split(' '); + video.IndexNumber = int.Parse(numbers[0].Replace(".", "").Split('/')[0]); + int totalEpisodesInSeason = int.Parse(numbers[0].Replace(".", "").Split('/')[1]); + + description = String.Join(" ", numbers, 1, numbers.Length - 1).Trim(); // Skip the first, concatenate the rest, clean up spaces and save it + } + else + throw new Exception(); // Switch to default parsing + } + catch // Default parsing + { + if (subtitle.Contains(".")) // skip the comment, keep the subtitle + description = String.Join(".", subtitle.Split('.'), 1, subtitle.Split('.').Length - 1).Trim(); // skip the first + else + description = subtitle.Trim(); // Clean up whitespaces and save it + } + } + } + + if (!string.IsNullOrWhiteSpace(description)) + { + video.Overview = description; + } + } + + private void ExtractTimestamp(Model.MediaInfo.MediaInfo video) + { + if (video.VideoType == VideoType.VideoFile) + { + if (string.Equals(video.Container, "mpeg2ts", StringComparison.OrdinalIgnoreCase) || + string.Equals(video.Container, "m2ts", StringComparison.OrdinalIgnoreCase) || + string.Equals(video.Container, "ts", StringComparison.OrdinalIgnoreCase)) + { + try + { + video.Timestamp = GetMpegTimestamp(video.Path); + + _logger.Debug("Video has {0} timestamp", video.Timestamp); + } + catch (Exception ex) + { + _logger.ErrorException("Error extracting timestamp info from {0}", ex, video.Path); + video.Timestamp = null; + } + } + } + } + + private TransportStreamTimestamp GetMpegTimestamp(string path) + { + var packetBuffer = new byte['Å']; + + using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) + { + fs.Read(packetBuffer, 0, packetBuffer.Length); + } + + if (packetBuffer[0] == 71) + { + return TransportStreamTimestamp.None; + } + + if ((packetBuffer[4] == 71) && (packetBuffer['Ä'] == 71)) + { + if ((packetBuffer[0] == 0) && (packetBuffer[1] == 0) && (packetBuffer[2] == 0) && (packetBuffer[3] == 0)) + { + return TransportStreamTimestamp.Zero; + } + + return TransportStreamTimestamp.Valid; + } + + return TransportStreamTimestamp.None; + } + + private void UpdateFromMediaInfo(MediaSourceInfo video, MediaStream videoStream) + { + if (video.VideoType == VideoType.VideoFile && video.Protocol == MediaProtocol.File) + { + if (videoStream != null) + { + try + { + 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; + } + catch (Exception ex) + { + _logger.ErrorException("Error running MediaInfo on {0}", ex, video.Path); + } + } + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Probing/whitelist.txt b/MediaBrowser.MediaEncoding/Probing/whitelist.txt new file mode 100644 index 000000000..1fd366551 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Probing/whitelist.txt @@ -0,0 +1 @@ +AC/DC
\ No newline at end of file diff --git a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs index 14d3e7284..60b70ad08 100644 --- a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs +++ b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs @@ -132,7 +132,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles int subtitleStreamIndex, CancellationToken cancellationToken) { - var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(itemId, false, cancellationToken).ConfigureAwait(false); + var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(itemId, null, false, new[] { MediaType.Audio, MediaType.Video }, cancellationToken).ConfigureAwait(false); var mediaSource = mediaSources .First(i => string.Equals(i.Id, mediaSourceId)); |
