jellyfin-server/MediaBrowser.Api/Playback/Progressive/VideoService.cs

272 lines
9.3 KiB
C#
Raw Normal View History

2013-03-13 02:56:13 +00:00
using System.IO;
using MediaBrowser.Common.IO;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities;
using System;
using MediaBrowser.Controller.Library;
2013-02-27 04:44:41 +00:00
using ServiceStack.ServiceHost;
2013-02-21 01:33:05 +00:00
2013-02-27 04:19:05 +00:00
namespace MediaBrowser.Api.Playback.Progressive
2013-02-21 01:33:05 +00:00
{
2013-02-27 04:44:41 +00:00
/// <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")]
2013-03-11 14:54:08 +00:00
[Route("/Videos/{Id}/stream.m2ts", "GET")]
2013-02-27 04:44:41 +00:00
[Route("/Videos/{Id}/stream", "GET")]
2013-03-13 03:57:54 +00:00
[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.m2ts", "HEAD")]
[Route("/Videos/{Id}/stream", "HEAD")]
2013-03-09 02:34:54 +00:00
[ServiceStack.ServiceHost.Api(Description = "Gets a video stream")]
public class GetVideoStream : VideoStreamRequest
2013-02-27 04:44:41 +00:00
{
}
2013-03-07 17:39:21 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
2013-02-27 04:19:05 +00:00
/// Class VideoService
2013-02-21 01:33:05 +00:00
/// </summary>
2013-02-27 04:19:05 +00:00
public class VideoService : BaseProgressiveStreamingService
2013-02-21 01:33:05 +00:00
{
2013-03-07 17:39:21 +00:00
public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager)
2013-03-04 05:43:06 +00:00
: base(appPaths, userManager, libraryManager, isoManager)
2013-02-21 01:33:05 +00:00
{
}
2013-02-27 04:44:41 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetVideoStream request)
{
2013-03-13 03:57:54 +00:00
return ProcessRequest(request, false);
2013-02-27 04:44:41 +00:00
}
2013-03-07 17:39:21 +00:00
2013-03-13 03:57:54 +00:00
public object Head(GetVideoStream request)
{
return ProcessRequest(request, true);
}
2013-02-21 01:33:05 +00:00
/// <summary>
2013-02-27 04:19:05 +00:00
/// Gets the command line arguments.
2013-02-21 01:33:05 +00:00
/// </summary>
/// <param name="outputPath">The output path.</param>
2013-02-27 04:19:05 +00:00
/// <param name="state">The state.</param>
2013-02-21 01:33:05 +00:00
/// <returns>System.String.</returns>
2013-02-27 04:19:05 +00:00
protected override string GetCommandLineArguments(string outputPath, StreamState state)
2013-02-21 01:33:05 +00:00
{
2013-02-27 04:19:05 +00:00
var video = (Video)state.Item;
2013-03-04 05:43:06 +00:00
var probeSize = Kernel.Instance.FFMpegManager.GetProbeSizeArgument(video.VideoType, video.IsoType);
2013-02-21 01:33:05 +00:00
// Get the output codec name
2013-03-09 02:34:54 +00:00
var videoCodec = GetVideoCodec(state.VideoRequest);
2013-02-21 01:33:05 +00:00
var graphicalSubtitleParam = string.Empty;
2013-02-27 04:19:05 +00:00
if (state.SubtitleStream != null)
2013-02-21 01:33:05 +00:00
{
// This is for internal graphical subs
2013-02-27 04:19:05 +00:00
if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
2013-02-21 01:33:05 +00:00
{
2013-02-27 04:19:05 +00:00
graphicalSubtitleParam = GetInternalGraphicalSubtitleParam(state, videoCodec);
2013-02-21 01:33:05 +00:00
}
}
2013-03-07 17:39:21 +00:00
var format = string.Empty;
2013-03-13 02:56:13 +00:00
var keyFrame = string.Empty;
2013-03-07 17:39:21 +00:00
2013-03-13 02:56:13 +00:00
if (string.Equals(Path.GetExtension(outputPath), ".mp4", StringComparison.OrdinalIgnoreCase))
2013-03-07 17:39:21 +00:00
{
2013-03-13 02:56:13 +00:00
format = " -f mp4 -movflags frag_keyframe+empty_moov";
var framerate = state.VideoRequest.Framerate ??
state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate ?? 23.976;
framerate *= 2;
keyFrame = " -g " + Math.Round(framerate);
2013-03-07 17:39:21 +00:00
}
2013-03-13 02:56:13 +00:00
return string.Format("{0} {1} -i {2}{3}{4} -threads 0 {5} {6}{7} {8}{9} \"{10}\"",
2013-02-21 01:33:05 +00:00
probeSize,
2013-02-27 04:19:05 +00:00
GetFastSeekCommandLineParameter(state.Request),
GetInputArgument(video, state.IsoMount),
GetSlowSeekCommandLineParameter(state.Request),
2013-03-13 02:56:13 +00:00
keyFrame,
2013-02-27 04:19:05 +00:00
GetMapArgs(state),
GetVideoArguments(state, videoCodec),
2013-02-21 01:33:05 +00:00
graphicalSubtitleParam,
2013-02-27 04:19:05 +00:00
GetAudioArguments(state),
2013-03-07 17:39:21 +00:00
format,
2013-02-21 01:33:05 +00:00
outputPath
).Trim();
}
/// <summary>
/// Gets video arguments to pass to ffmpeg
/// </summary>
2013-02-27 04:19:05 +00:00
/// <param name="state">The state.</param>
/// <param name="videoCodec">The video codec.</param>
2013-02-21 01:33:05 +00:00
/// <returns>System.String.</returns>
2013-02-27 04:19:05 +00:00
private string GetVideoArguments(StreamState state, string videoCodec)
2013-02-21 01:33:05 +00:00
{
var args = "-vcodec " + videoCodec;
// See if we can save come cpu cycles by avoiding encoding
if (videoCodec.Equals("copy", StringComparison.OrdinalIgnoreCase))
2013-02-21 01:33:05 +00:00
{
return IsH264(state.VideoStream) ? args + " -bsf h264_mp4toannexb" : args;
}
2013-02-21 01:33:05 +00:00
const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,0),gte(t,prev_forced_t+2))";
2013-02-21 01:33:05 +00:00
args += keyFrameArg;
2013-02-21 01:33:05 +00:00
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, videoCodec);
}
2013-03-20 14:27:56 +00:00
if (request.Framerate.HasValue)
{
args += string.Format(" -r {0}", request.Framerate.Value);
}
// Add the audio bitrate
var qualityParam = GetVideoQualityParam(request, videoCodec);
if (!string.IsNullOrEmpty(qualityParam))
{
args += " " + qualityParam;
}
args += " -vsync vfr";
if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
{
args += " -profile:v " + state.VideoRequest.Profile;
2013-02-21 01:33:05 +00:00
}
if (!string.IsNullOrEmpty(state.VideoRequest.Level))
2013-02-21 01:33:05 +00:00
{
args += " -level 3 " + state.VideoRequest.Level;
2013-02-21 01:33:05 +00:00
}
return args;
}
/// <summary>
/// Gets audio arguments to pass to ffmpeg
/// </summary>
2013-02-27 04:19:05 +00:00
/// <param name="state">The state.</param>
2013-02-21 01:33:05 +00:00
/// <returns>System.String.</returns>
2013-02-27 04:19:05 +00:00
private string GetAudioArguments(StreamState state)
2013-02-21 01:33:05 +00:00
{
// If the video doesn't have an audio stream, return a default.
2013-02-27 04:19:05 +00:00
if (state.AudioStream == null)
2013-02-21 01:33:05 +00:00
{
return string.Empty;
}
2013-02-27 04:19:05 +00:00
var request = state.Request;
2013-02-21 01:33:05 +00:00
// Get the output codec name
2013-02-27 04:19:05 +00:00
var codec = GetAudioCodec(request);
2013-02-21 01:33:05 +00:00
2013-03-24 22:37:20 +00:00
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
return "-acodec copy";
}
2013-02-21 01:33:05 +00:00
var args = "-acodec " + codec;
2013-03-24 22:37:20 +00:00
// Add the number of audio channels
var channels = GetNumAudioChannelsParam(request, state.AudioStream);
if (channels.HasValue)
2013-02-21 01:33:05 +00:00
{
2013-03-24 22:37:20 +00:00
args += " -ac " + channels.Value;
}
2013-02-21 01:33:05 +00:00
2013-03-24 22:37:20 +00:00
if (request.AudioSampleRate.HasValue)
{
args += " -ar " + request.AudioSampleRate.Value;
}
2013-03-24 22:37:20 +00:00
if (request.AudioBitRate.HasValue)
{
args += " -ab " + request.AudioBitRate.Value;
}
2013-02-21 01:33:05 +00:00
2013-03-24 22:37:20 +00:00
var volParam = string.Empty;
2013-02-21 01:33:05 +00:00
2013-03-24 22:37:20 +00:00
// 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";
2013-02-21 01:33:05 +00:00
}
2013-03-24 22:45:53 +00:00
args += string.Format(" -af \"aresample=async=1000{0}\"", volParam);
2013-03-24 22:37:20 +00:00
2013-02-21 01:33:05 +00:00
return args;
}
/// <summary>
/// Gets the video bitrate to specify on the command line
/// </summary>
2013-02-27 04:19:05 +00:00
/// <param name="request">The request.</param>
2013-02-21 01:33:05 +00:00
/// <param name="videoCodec">The video codec.</param>
/// <returns>System.String.</returns>
2013-03-09 02:34:54 +00:00
private string GetVideoQualityParam(VideoStreamRequest request, string videoCodec)
2013-02-21 01:33:05 +00:00
{
var args = string.Empty;
// webm
if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
{
2013-03-27 12:42:06 +00:00
args = "-quality realtime -profile:v 1 -slices 4";
2013-02-21 01:33:05 +00:00
}
// asf/wmv
else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
{
args = "-g 100 -qmax 15";
}
else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
{
args = "-preset superfast";
}
2013-02-27 04:19:05 +00:00
if (request.VideoBitRate.HasValue)
2013-02-21 01:33:05 +00:00
{
2013-02-27 04:19:05 +00:00
args += " -b:v " + request.VideoBitRate;
2013-02-21 01:33:05 +00:00
}
return args.Trim();
}
}
}