You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Api/Playback/Progressive/VideoService.cs

275 lines
9.4 KiB

using System.IO;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using System;
using MediaBrowser.Controller.Library;
12 years ago
using ServiceStack.ServiceHost;
12 years ago
namespace MediaBrowser.Api.Playback.Progressive
{
12 years ago
/// <summary>
/// Class GetAudioStream
/// </summary>
[Route("/Videos/{Id}/stream.ts", "GET")]
[Route("/Videos/{Id}/stream.webm", "GET")]
[Route("/Videos/{Id}/stream.asf", "GET")]
[Route("/Videos/{Id}/stream.wmv", "GET")]
[Route("/Videos/{Id}/stream.ogv", "GET")]
[Route("/Videos/{Id}/stream.mp4", "GET")]
[Route("/Videos/{Id}/stream.m4v", "GET")]
[Route("/Videos/{Id}/stream.mkv", "GET")]
[Route("/Videos/{Id}/stream.mpeg", "GET")]
[Route("/Videos/{Id}/stream.avi", "GET")]
[Route("/Videos/{Id}/stream.m2ts", "GET")]
[Route("/Videos/{Id}/stream.3gp", "GET")]
[Route("/Videos/{Id}/stream.wmv", "GET")]
12 years ago
[Route("/Videos/{Id}/stream", "GET")]
[Route("/Videos/{Id}/stream.ts", "HEAD")]
[Route("/Videos/{Id}/stream.webm", "HEAD")]
[Route("/Videos/{Id}/stream.asf", "HEAD")]
[Route("/Videos/{Id}/stream.wmv", "HEAD")]
[Route("/Videos/{Id}/stream.ogv", "HEAD")]
[Route("/Videos/{Id}/stream.mp4", "HEAD")]
[Route("/Videos/{Id}/stream.m4v", "HEAD")]
[Route("/Videos/{Id}/stream.mkv", "HEAD")]
[Route("/Videos/{Id}/stream.mpeg", "HEAD")]
[Route("/Videos/{Id}/stream.avi", "HEAD")]
[Route("/Videos/{Id}/stream.3gp", "HEAD")]
[Route("/Videos/{Id}/stream.wmv", "HEAD")]
[Route("/Videos/{Id}/stream.m2ts", "HEAD")]
[Route("/Videos/{Id}/stream", "HEAD")]
[Api(Description = "Gets a video stream")]
public class GetVideoStream : VideoStreamRequest
12 years ago
{
}
/// <summary>
12 years ago
/// Class VideoService
/// </summary>
12 years ago
public class VideoService : BaseProgressiveStreamingService
{
public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder)
{
}
12 years ago
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetVideoStream request)
{
return ProcessRequest(request, false);
12 years ago
}
public object Head(GetVideoStream request)
{
return ProcessRequest(request, true);
}
/// <summary>
12 years ago
/// Gets the command line arguments.
/// </summary>
/// <param name="outputPath">The output path.</param>
12 years ago
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
12 years ago
protected override string GetCommandLineArguments(string outputPath, StreamState state)
{
12 years ago
var video = (Video)state.Item;
var probeSize = GetProbeSizeArgument(state.Item);
// Get the output codec name
var videoCodec = GetVideoCodec(state.VideoRequest);
var format = string.Empty;
var keyFrame = string.Empty;
if (string.Equals(Path.GetExtension(outputPath), ".mp4", StringComparison.OrdinalIgnoreCase))
{
format = " -f mp4 -movflags frag_keyframe+empty_moov";
}
12 years ago
var threads = videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase) ? 2 : 0;
return string.Format("{0} {1} -i {2}{3}{4} {5} {6} -threads {7} {8}{9} \"{10}\"",
probeSize,
12 years ago
GetFastSeekCommandLineParameter(state.Request),
GetInputArgument(video, state.IsoMount),
GetSlowSeekCommandLineParameter(state.Request),
keyFrame,
12 years ago
GetMapArgs(state),
GetVideoArguments(state, videoCodec),
12 years ago
threads,
12 years ago
GetAudioArguments(state),
format,
outputPath
).Trim();
}
/// <summary>
/// Gets video arguments to pass to ffmpeg
/// </summary>
12 years ago
/// <param name="state">The state.</param>
/// <param name="codec">The video codec.</param>
/// <returns>System.String.</returns>
private string GetVideoArguments(StreamState state, string codec)
{
var args = "-vcodec " + codec;
// See if we can save come cpu cycles by avoiding encoding
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
return IsH264(state.VideoStream) ? args + " -bsf h264_mp4toannexb" : args;
}
const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,0),gte(t,prev_forced_t+2))";
args += keyFrameArg;
var request = state.VideoRequest;
// Add resolution params, if specified
if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
{
args += GetOutputSizeParam(state, codec);
}
if (request.Framerate.HasValue)
{
args += string.Format(" -r {0}", request.Framerate.Value);
}
// Add the audio bitrate
var qualityParam = GetVideoQualityParam(request, codec);
if (!string.IsNullOrEmpty(qualityParam))
{
args += " " + qualityParam;
}
args += " -vsync vfr";
if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
{
args += " -profile:v " + state.VideoRequest.Profile;
}
if (!string.IsNullOrEmpty(state.VideoRequest.Level))
{
args += " -level " + state.VideoRequest.Level;
}
if (state.SubtitleStream != null)
{
// This is for internal graphical subs
if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
{
args += GetInternalGraphicalSubtitleParam(state, codec);
}
}
return args;
}
/// <summary>
/// Gets audio arguments to pass to ffmpeg
/// </summary>
12 years ago
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
12 years ago
private string GetAudioArguments(StreamState state)
{
// If the video doesn't have an audio stream, return a default.
12 years ago
if (state.AudioStream == null)
{
return string.Empty;
}
12 years ago
var request = state.Request;
// Get the output codec name
12 years ago
var codec = GetAudioCodec(request);
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
return "-acodec copy";
}
var args = "-acodec " + codec;
// Add the number of audio channels
var channels = GetNumAudioChannelsParam(request, state.AudioStream);
if (channels.HasValue)
{
args += " -ac " + channels.Value;
}
if (request.AudioSampleRate.HasValue)
{
args += " -ar " + request.AudioSampleRate.Value;
}
if (request.AudioBitRate.HasValue)
{
args += " -ab " + request.AudioBitRate.Value;
}
var volParam = string.Empty;
// Boost volume to 200% when downsampling from 6ch to 2ch
if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
{
volParam = ",volume=2.000000";
}
args += string.Format(" -af \"aresample=async=1000{0}\"", volParam);
return args;
}
/// <summary>
/// Gets the video bitrate to specify on the command line
/// </summary>
12 years ago
/// <param name="request">The request.</param>
/// <param name="videoCodec">The video codec.</param>
/// <returns>System.String.</returns>
private string GetVideoQualityParam(VideoStreamRequest request, string videoCodec)
{
var args = string.Empty;
// webm
if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
{
12 years ago
args = "-quality realtime -profile:v 0 -slices 4";
}
// asf/wmv
else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
{
args = "-g 100 -qmax 15";
}
else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
{
args = "-preset superfast";
}
else if (videoCodec.Equals("mpeg4", StringComparison.OrdinalIgnoreCase))
{
args = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
}
12 years ago
if (request.VideoBitRate.HasValue)
{
12 years ago
args += " -b:v " + request.VideoBitRate;
}
return args.Trim();
}
}
}