2016-03-27 21:11:27 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2015-01-02 06:12:58 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
2015-03-23 04:08:06 +00:00
|
|
|
|
using MediaBrowser.Model.Dlna;
|
2015-01-02 06:12:58 +00:00
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2016-06-19 06:18:29 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2016-11-01 04:07:12 +00:00
|
|
|
|
using MediaBrowser.Model.Diagnostics;
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Encoder
|
|
|
|
|
{
|
|
|
|
|
public class VideoEncoder : BaseEncoder
|
|
|
|
|
{
|
2016-11-01 04:07:12 +00:00
|
|
|
|
public VideoEncoder(MediaEncoder mediaEncoder, ILogger logger, IServerConfigurationManager configurationManager, IFileSystem fileSystem, IIsoManager isoManager, ILibraryManager libraryManager, ISessionManager sessionManager, ISubtitleEncoder subtitleEncoder, IMediaSourceManager mediaSourceManager, IProcessFactory processFactory) : base(mediaEncoder, logger, configurationManager, fileSystem, isoManager, libraryManager, sessionManager, subtitleEncoder, mediaSourceManager, processFactory)
|
2015-01-02 06:12:58 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-19 06:18:29 +00:00
|
|
|
|
protected override async Task<string> GetCommandLineArguments(EncodingJob state)
|
2015-01-02 06:12:58 +00:00
|
|
|
|
{
|
|
|
|
|
// Get the output codec name
|
2017-02-02 16:02:01 +00:00
|
|
|
|
var encodingOptions = GetEncodingOptions();
|
|
|
|
|
var videoCodec = EncodingHelper.GetVideoEncoder(state, encodingOptions);
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
|
|
|
|
var format = string.Empty;
|
|
|
|
|
var keyFrame = string.Empty;
|
|
|
|
|
|
2016-02-06 21:36:29 +00:00
|
|
|
|
if (string.Equals(Path.GetExtension(state.OutputFilePath), ".mp4", StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
|
state.Options.Context == EncodingContext.Streaming)
|
2015-01-02 06:12:58 +00:00
|
|
|
|
{
|
2016-02-04 19:31:04 +00:00
|
|
|
|
// Comparison: https://github.com/jansmolders86/mediacenterjs/blob/master/lib/transcoding/desktop.js
|
2015-01-02 06:12:58 +00:00
|
|
|
|
format = " -f mp4 -movflags frag_keyframe+empty_moov";
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 16:02:01 +00:00
|
|
|
|
var threads = EncodingHelper.GetNumberOfThreads(state, encodingOptions, string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase));
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
2017-02-02 16:02:01 +00:00
|
|
|
|
var inputModifier = EncodingHelper.GetInputModifier(state, encodingOptions);
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
2016-06-19 06:18:29 +00:00
|
|
|
|
var videoArguments = await GetVideoArguments(state, videoCodec).ConfigureAwait(false);
|
|
|
|
|
|
2015-01-02 06:12:58 +00:00
|
|
|
|
return string.Format("{0} {1}{2} {3} {4} -map_metadata -1 -threads {5} {6}{7} -y \"{8}\"",
|
|
|
|
|
inputModifier,
|
2017-02-02 16:02:01 +00:00
|
|
|
|
EncodingHelper.GetInputArgument(state, encodingOptions),
|
2015-01-02 06:12:58 +00:00
|
|
|
|
keyFrame,
|
2017-02-02 16:02:01 +00:00
|
|
|
|
EncodingHelper.GetMapArgs(state),
|
2016-06-19 06:18:29 +00:00
|
|
|
|
videoArguments,
|
2015-01-02 06:12:58 +00:00
|
|
|
|
threads,
|
|
|
|
|
GetAudioArguments(state),
|
|
|
|
|
format,
|
|
|
|
|
state.OutputFilePath
|
|
|
|
|
).Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets video arguments to pass to ffmpeg
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
2016-02-04 19:31:04 +00:00
|
|
|
|
/// <param name="videoCodec">The video codec.</param>
|
2015-01-02 06:12:58 +00:00
|
|
|
|
/// <returns>System.String.</returns>
|
2016-06-19 06:18:29 +00:00
|
|
|
|
private async Task<string> GetVideoArguments(EncodingJob state, string videoCodec)
|
2015-01-02 06:12:58 +00:00
|
|
|
|
{
|
2016-02-04 19:31:04 +00:00
|
|
|
|
var args = "-codec:v:0 " + videoCodec;
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
|
|
|
|
if (state.EnableMpegtsM2TsMode)
|
|
|
|
|
{
|
|
|
|
|
args += " -mpegts_m2ts_mode 1";
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 19:31:04 +00:00
|
|
|
|
var isOutputMkv = string.Equals(state.Options.OutputContainer, "mkv", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
if (state.RunTimeTicks.HasValue)
|
2015-01-02 06:12:58 +00:00
|
|
|
|
{
|
2016-02-04 19:31:04 +00:00
|
|
|
|
//args += " -copyts -avoid_negative_ts disabled -start_at_zero";
|
2015-01-02 06:12:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 19:31:04 +00:00
|
|
|
|
if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase))
|
2015-03-27 20:55:31 +00:00
|
|
|
|
{
|
2017-02-02 16:02:01 +00:00
|
|
|
|
if (state.VideoStream != null && EncodingHelper.IsH264(state.VideoStream) && string.Equals(state.Options.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
|
2016-02-04 19:31:04 +00:00
|
|
|
|
{
|
|
|
|
|
args += " -bsf:v h264_mp4toannexb";
|
|
|
|
|
}
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
2016-02-04 19:31:04 +00:00
|
|
|
|
return args;
|
2015-03-27 20:55:31 +00:00
|
|
|
|
}
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
2016-08-07 20:13:30 +00:00
|
|
|
|
var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"",
|
2016-02-04 19:31:04 +00:00
|
|
|
|
5.ToString(UsCulture));
|
|
|
|
|
|
|
|
|
|
args += keyFrameArg;
|
|
|
|
|
|
2016-03-07 18:50:58 +00:00
|
|
|
|
var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode;
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
|
|
|
|
// Add resolution params, if specified
|
|
|
|
|
if (!hasGraphicalSubs)
|
|
|
|
|
{
|
2017-02-02 16:02:01 +00:00
|
|
|
|
args += EncodingHelper.GetOutputSizeParam(state, videoCodec);
|
2015-01-02 06:12:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 16:02:01 +00:00
|
|
|
|
var qualityParam = EncodingHelper.GetVideoQualityParam(state, videoCodec, GetEncodingOptions(), "superfast");
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(qualityParam))
|
|
|
|
|
{
|
|
|
|
|
args += " " + qualityParam.Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is for internal graphical subs
|
|
|
|
|
if (hasGraphicalSubs)
|
|
|
|
|
{
|
2017-02-02 16:02:01 +00:00
|
|
|
|
args += EncodingHelper.GetGraphicalSubtitleParam(state, videoCodec);
|
2015-01-02 06:12:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets audio arguments to pass to ffmpeg
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private string GetAudioArguments(EncodingJob state)
|
|
|
|
|
{
|
|
|
|
|
// If the video doesn't have an audio stream, return a default.
|
|
|
|
|
if (state.AudioStream == null && state.VideoStream != null)
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the output codec name
|
2017-02-02 16:02:01 +00:00
|
|
|
|
var codec = EncodingHelper.GetAudioEncoder(state);
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
|
|
|
|
var args = "-codec:a:0 " + codec;
|
|
|
|
|
|
2016-02-04 19:31:04 +00:00
|
|
|
|
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
|
2015-01-02 06:12:58 +00:00
|
|
|
|
{
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the number of audio channels
|
|
|
|
|
var channels = state.OutputAudioChannels;
|
|
|
|
|
|
|
|
|
|
if (channels.HasValue)
|
|
|
|
|
{
|
|
|
|
|
args += " -ac " + channels.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bitrate = state.OutputAudioBitrate;
|
|
|
|
|
|
|
|
|
|
if (bitrate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
args += " -ab " + bitrate.Value.ToString(UsCulture);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 16:02:01 +00:00
|
|
|
|
args += " " + EncodingHelper.GetAudioFilterParam(state, GetEncodingOptions(), false);
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override string GetOutputFileExtension(EncodingJob state)
|
|
|
|
|
{
|
|
|
|
|
var ext = base.GetOutputFileExtension(state);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(ext))
|
|
|
|
|
{
|
|
|
|
|
return ext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var videoCodec = state.Options.VideoCodec;
|
|
|
|
|
|
|
|
|
|
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return ".ts";
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(videoCodec, "theora", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return ".ogv";
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(videoCodec, "vpx", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return ".webm";
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return ".asf";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-01-05 06:00:13 +00:00
|
|
|
|
|
|
|
|
|
protected override bool IsVideoEncoder
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
2016-11-01 04:07:12 +00:00
|
|
|
|
|
2015-01-02 06:12:58 +00:00
|
|
|
|
}
|
2016-08-07 20:13:30 +00:00
|
|
|
|
}
|