2015-01-02 06:12:58 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Encoder
|
|
|
|
|
{
|
|
|
|
|
public class VideoEncoder : BaseEncoder
|
|
|
|
|
{
|
2015-03-30 16:16:34 +00:00
|
|
|
|
public VideoEncoder(MediaEncoder mediaEncoder, ILogger logger, IServerConfigurationManager configurationManager, IFileSystem fileSystem, IIsoManager isoManager, ILibraryManager libraryManager, ISessionManager sessionManager, ISubtitleEncoder subtitleEncoder, IMediaSourceManager mediaSourceManager) : base(mediaEncoder, logger, configurationManager, fileSystem, isoManager, libraryManager, sessionManager, subtitleEncoder, mediaSourceManager)
|
2015-01-02 06:12:58 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override string GetCommandLineArguments(EncodingJob state)
|
|
|
|
|
{
|
|
|
|
|
// Get the output codec name
|
|
|
|
|
var videoCodec = state.OutputVideoCodec;
|
|
|
|
|
|
|
|
|
|
var format = string.Empty;
|
|
|
|
|
var keyFrame = string.Empty;
|
|
|
|
|
|
2015-03-23 04:08:06 +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
|
|
|
|
{
|
|
|
|
|
format = " -f mp4 -movflags frag_keyframe+empty_moov";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var threads = GetNumberOfThreads(state, string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
var inputModifier = GetInputModifier(state);
|
|
|
|
|
|
|
|
|
|
return string.Format("{0} {1}{2} {3} {4} -map_metadata -1 -threads {5} {6}{7} -y \"{8}\"",
|
|
|
|
|
inputModifier,
|
|
|
|
|
GetInputArgument(state),
|
|
|
|
|
keyFrame,
|
|
|
|
|
GetMapArgs(state),
|
|
|
|
|
GetVideoArguments(state, videoCodec),
|
|
|
|
|
threads,
|
|
|
|
|
GetAudioArguments(state),
|
|
|
|
|
format,
|
|
|
|
|
state.OutputFilePath
|
|
|
|
|
).Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets video arguments to pass to ffmpeg
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <param name="codec">The video codec.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private string GetVideoArguments(EncodingJob state, string codec)
|
|
|
|
|
{
|
|
|
|
|
var args = "-codec:v:0 " + codec;
|
|
|
|
|
|
|
|
|
|
if (state.EnableMpegtsM2TsMode)
|
|
|
|
|
{
|
|
|
|
|
args += " -mpegts_m2ts_mode 1";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// See if we can save come cpu cycles by avoiding encoding
|
|
|
|
|
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return state.VideoStream != null && IsH264(state.VideoStream) && string.Equals(state.Options.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) ?
|
|
|
|
|
args + " -bsf:v h264_mp4toannexb" :
|
|
|
|
|
args;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 20:55:31 +00:00
|
|
|
|
if (state.Options.Context == EncodingContext.Streaming)
|
|
|
|
|
{
|
|
|
|
|
var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})",
|
|
|
|
|
5.ToString(UsCulture));
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
2015-03-27 20:55:31 +00:00
|
|
|
|
args += keyFrameArg;
|
|
|
|
|
}
|
2015-01-02 06:12:58 +00:00
|
|
|
|
|
|
|
|
|
var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream;
|
|
|
|
|
|
|
|
|
|
// Add resolution params, if specified
|
|
|
|
|
if (!hasGraphicalSubs)
|
|
|
|
|
{
|
|
|
|
|
args += GetOutputSizeParam(state, codec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var qualityParam = GetVideoQualityParam(state, codec, false);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(qualityParam))
|
|
|
|
|
{
|
|
|
|
|
args += " " + qualityParam.Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is for internal graphical subs
|
|
|
|
|
if (hasGraphicalSubs)
|
|
|
|
|
{
|
|
|
|
|
args += GetGraphicalSubtitleParam(state, codec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
var codec = state.OutputAudioCodec;
|
|
|
|
|
|
|
|
|
|
var args = "-codec:a:0 " + codec;
|
|
|
|
|
|
|
|
|
|
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args += " " + GetAudioFilterParam(state, false);
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
2015-01-02 06:12:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|