jellyfin/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs

306 lines
12 KiB
C#
Raw Normal View History

2015-05-15 15:46:20 +00:00
using MediaBrowser.Common.Configuration;
2015-01-02 06:12:58 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
2015-05-15 15:46:20 +00:00
using MediaBrowser.Model.Configuration;
2015-01-02 06:12:58 +00:00
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Dto;
2015-01-02 06:12:58 +00:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.MediaInfo;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.MediaEncoding.Encoder
{
public class EncodingJobFactory
{
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
2015-03-08 04:44:31 +00:00
private readonly IMediaSourceManager _mediaSourceManager;
2015-05-15 15:46:20 +00:00
private readonly IConfigurationManager _config;
2017-03-19 06:10:11 +00:00
private readonly IMediaEncoder _mediaEncoder;
2015-01-02 06:12:58 +00:00
protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
2017-03-19 06:10:11 +00:00
public EncodingJobFactory(ILogger logger, ILibraryManager libraryManager, IMediaSourceManager mediaSourceManager, IConfigurationManager config, IMediaEncoder mediaEncoder)
2015-01-02 06:12:58 +00:00
{
_logger = logger;
_libraryManager = libraryManager;
2015-03-08 04:44:31 +00:00
_mediaSourceManager = mediaSourceManager;
2015-05-15 15:46:20 +00:00
_config = config;
2017-03-19 06:10:11 +00:00
_mediaEncoder = mediaEncoder;
2015-01-02 06:12:58 +00:00
}
public async Task<EncodingJob> CreateJob(EncodingJobOptions options, EncodingHelper encodingHelper, bool isVideoRequest, IProgress<double> progress, CancellationToken cancellationToken)
2015-01-02 06:12:58 +00:00
{
var request = options;
if (string.IsNullOrEmpty(request.AudioCodec))
{
request.AudioCodec = InferAudioCodec(request.OutputContainer);
}
var state = new EncodingJob(_logger, _mediaSourceManager)
2015-01-02 06:12:58 +00:00
{
Options = options,
IsVideoRequest = isVideoRequest,
Progress = progress
};
if (!string.IsNullOrWhiteSpace(request.VideoCodec))
{
state.SupportedVideoCodecs = request.VideoCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
request.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault();
}
2015-01-02 06:12:58 +00:00
if (!string.IsNullOrWhiteSpace(request.AudioCodec))
{
state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault();
}
2017-03-19 06:10:11 +00:00
if (!string.IsNullOrWhiteSpace(request.SubtitleCodec))
{
state.SupportedSubtitleCodecs = request.SubtitleCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
request.SubtitleCodec = state.SupportedSubtitleCodecs.FirstOrDefault(i => _mediaEncoder.CanEncodeToSubtitleCodec(i))
?? state.SupportedSubtitleCodecs.FirstOrDefault();
}
2015-01-02 06:12:58 +00:00
var item = _libraryManager.GetItemById(request.ItemId);
state.ItemType = item.GetType().Name;
state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
2015-03-07 22:43:53 +00:00
var primaryImage = item.GetImageInfo(ImageType.Primary, 0) ??
item.Parents.Select(i => i.GetImageInfo(ImageType.Primary, 0)).FirstOrDefault(i => i != null);
if (primaryImage != null)
{
state.AlbumCoverPath = primaryImage.Path;
}
2015-04-09 05:20:23 +00:00
var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(request.ItemId, null, false, new[] { MediaType.Audio, MediaType.Video }, cancellationToken).ConfigureAwait(false);
2015-01-02 06:12:58 +00:00
var mediaSource = string.IsNullOrEmpty(request.MediaSourceId)
? mediaSources.First()
: mediaSources.First(i => string.Equals(i.Id, request.MediaSourceId));
2015-01-02 06:12:58 +00:00
var videoRequest = state.Options;
2015-01-02 06:12:58 +00:00
encodingHelper.AttachMediaSourceInfo(state, mediaSource, null);
//var container = Path.GetExtension(state.RequestedUrl);
//if (string.IsNullOrEmpty(container))
//{
// container = request.Static ?
// state.InputContainer :
// (Path.GetExtension(GetOutputFilePath(state)) ?? string.Empty).TrimStart('.');
//}
//state.OutputContainer = (container ?? string.Empty).TrimStart('.');
state.OutputAudioBitrate = encodingHelper.GetAudioBitrateParam(state.Options, state.AudioStream);
2015-01-02 06:12:58 +00:00
state.OutputAudioSampleRate = request.AudioSampleRate;
state.OutputAudioCodec = state.Options.AudioCodec;
2015-01-02 06:12:58 +00:00
state.OutputAudioChannels = encodingHelper.GetNumAudioChannelsParam(state.Options, state.AudioStream, state.OutputAudioCodec);
2015-01-02 06:12:58 +00:00
if (videoRequest != null)
2015-01-02 06:12:58 +00:00
{
state.OutputVideoCodec = state.Options.VideoCodec;
state.OutputVideoBitrate = encodingHelper.GetVideoBitrateParamValue(state.Options, state.VideoStream, state.OutputVideoCodec);
2015-01-02 06:12:58 +00:00
if (state.OutputVideoBitrate.HasValue)
{
var resolution = ResolutionNormalizer.Normalize(
state.VideoStream == null ? (int?)null : state.VideoStream.BitRate,
state.OutputVideoBitrate.Value,
state.VideoStream == null ? null : state.VideoStream.Codec,
2015-01-02 06:12:58 +00:00
state.OutputVideoCodec,
videoRequest.MaxWidth,
videoRequest.MaxHeight);
2015-01-02 06:12:58 +00:00
videoRequest.MaxWidth = resolution.MaxWidth;
videoRequest.MaxHeight = resolution.MaxHeight;
2015-01-02 06:12:58 +00:00
}
}
ApplyDeviceProfileSettings(state);
if (videoRequest != null)
{
encodingHelper.TryStreamCopy(state);
}
//state.OutputFilePath = GetOutputFilePath(state);
return state;
}
2015-05-15 15:46:20 +00:00
protected EncodingOptions GetEncodingOptions()
{
return _config.GetConfiguration<EncodingOptions>("encoding");
}
2015-01-02 06:12:58 +00:00
/// <summary>
/// Infers the video codec.
/// </summary>
/// <param name="container">The container.</param>
/// <returns>System.Nullable{VideoCodecs}.</returns>
private static string InferVideoCodec(string container)
{
var ext = "." + (container ?? string.Empty);
if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "wmv";
}
if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "vpx";
}
if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "theora";
}
if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "h264";
}
return "copy";
}
private string InferAudioCodec(string container)
{
var ext = "." + (container ?? string.Empty);
if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "mp3";
}
if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "aac";
}
if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "wma";
}
if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "vorbis";
}
if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "vorbis";
}
if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "vorbis";
}
if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "vorbis";
}
if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
2015-01-02 06:12:58 +00:00
{
return "vorbis";
}
return "copy";
}
/// <summary>
/// Determines whether the specified stream is H264.
/// </summary>
/// <param name="stream">The stream.</param>
/// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
protected bool IsH264(MediaStream stream)
{
var codec = stream.Codec ?? string.Empty;
return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
}
private static int GetVideoProfileScore(string profile)
{
var list = new List<string>
{
"Constrained Baseline",
"Baseline",
"Extended",
"Main",
"High",
"Progressive High",
"Constrained High"
};
return Array.FindIndex(list.ToArray(), t => string.Equals(t, profile, StringComparison.OrdinalIgnoreCase));
}
private void ApplyDeviceProfileSettings(EncodingJob state)
{
var profile = state.Options.DeviceProfile;
if (profile == null)
{
// Don't use settings from the default profile.
// Only use a specific profile if it was requested.
return;
}
var audioCodec = state.ActualOutputAudioCodec;
var videoCodec = state.ActualOutputVideoCodec;
var outputContainer = state.Options.OutputContainer;
var mediaProfile = state.IsVideoRequest ?
2017-05-13 19:31:25 +00:00
profile.GetAudioMediaProfile(outputContainer, audioCodec, state.OutputAudioChannels, state.OutputAudioBitrate, state.OutputAudioSampleRate) :
2015-01-02 06:12:58 +00:00
profile.GetVideoMediaProfile(outputContainer,
audioCodec,
videoCodec,
state.OutputWidth,
state.OutputHeight,
state.TargetVideoBitDepth,
state.OutputVideoBitrate,
state.TargetVideoProfile,
state.TargetVideoLevel,
state.TargetFramerate,
state.TargetPacketLength,
state.TargetTimestamp,
state.IsTargetAnamorphic,
state.TargetRefFrames,
state.TargetVideoStreamCount,
2015-10-19 16:05:03 +00:00
state.TargetAudioStreamCount,
2016-10-03 06:28:45 +00:00
state.TargetVideoCodecTag,
2016-12-14 20:58:55 +00:00
state.IsTargetAVC);
2015-01-02 06:12:58 +00:00
if (mediaProfile != null)
{
state.MimeType = mediaProfile.MimeType;
}
var transcodingProfile = state.IsVideoRequest ?
profile.GetAudioTranscodingProfile(outputContainer, audioCodec) :
profile.GetVideoTranscodingProfile(outputContainer, audioCodec, videoCodec);
if (transcodingProfile != null)
{
state.EstimateContentLength = transcodingProfile.EstimateContentLength;
state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
state.Options.CopyTimestamps = transcodingProfile.CopyTimestamps;
2015-01-02 06:12:58 +00:00
}
}
}
}