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

214 lines
7.3 KiB

12 years ago
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", "GET")]
public class GetVideoStream : StreamRequest
{
}
/// <summary>
12 years ago
/// Class VideoService
/// </summary>
12 years ago
public class VideoService : BaseProgressiveStreamingService
{
/// <summary>
12 years ago
/// Initializes a new instance of the <see cref="BaseProgressiveStreamingService" /> class.
/// </summary>
12 years ago
/// <param name="appPaths">The app paths.</param>
public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager)
: base(appPaths, userManager, libraryManager)
{
}
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);
}
/// <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 = ServerKernel.FFMpegManager.GetProbeSizeArgument(video.VideoType, video.IsoType);
// Get the output codec name
12 years ago
var videoCodec = GetVideoCodec(state.Request);
var graphicalSubtitleParam = string.Empty;
12 years ago
if (state.SubtitleStream != null)
{
// This is for internal graphical subs
12 years ago
if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
{
12 years ago
graphicalSubtitleParam = GetInternalGraphicalSubtitleParam(state, videoCodec);
}
}
return string.Format("{0} {1} -i {2}{3} -threads 0 {4} {5}{6} {7} \"{8}\"",
probeSize,
12 years ago
GetFastSeekCommandLineParameter(state.Request),
GetInputArgument(video, state.IsoMount),
GetSlowSeekCommandLineParameter(state.Request),
GetMapArgs(state),
GetVideoArguments(state, videoCodec),
graphicalSubtitleParam,
12 years ago
GetAudioArguments(state),
outputPath
).Trim();
}
/// <summary>
/// Gets video arguments to pass to ffmpeg
/// </summary>
12 years ago
/// <param name="state">The state.</param>
/// <param name="videoCodec">The video codec.</param>
/// <returns>System.String.</returns>
12 years ago
private string GetVideoArguments(StreamState state, string videoCodec)
{
var args = "-vcodec " + videoCodec;
12 years ago
var request = state.Request;
// If we're encoding video, add additional params
if (!videoCodec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
// Add resolution params, if specified
12 years ago
if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
{
12 years ago
args += GetOutputSizeParam(state, videoCodec);
}
12 years ago
if (request.Framerate.HasValue)
{
12 years ago
args += string.Format(" -r {0}", request.Framerate.Value);
}
// Add the audio bitrate
12 years ago
var qualityParam = GetVideoQualityParam(request, videoCodec);
if (!string.IsNullOrEmpty(qualityParam))
{
args += " " + qualityParam;
}
}
12 years ago
else if (IsH264(state.VideoStream))
{
args += " -bsf h264_mp4toannexb";
}
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);
var args = "-acodec " + codec;
// If we're encoding audio, add additional params
if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
// Add the number of audio channels
12 years ago
var channels = GetNumAudioChannelsParam(request, state.AudioStream);
if (channels.HasValue)
{
args += " -ac " + channels.Value;
}
12 years ago
if (request.AudioSampleRate.HasValue)
{
12 years ago
args += " -ar " + request.AudioSampleRate.Value;
}
12 years ago
if (request.AudioBitRate.HasValue)
{
12 years ago
args += " -ab " + request.AudioBitRate.Value;
}
}
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>
12 years ago
private string GetVideoQualityParam(StreamRequest request, string videoCodec)
{
var args = string.Empty;
// webm
if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
{
args = "-g 120 -cpu-used 1 -lag-in-frames 16 -deadline realtime -slices 4 -vprofile 0";
}
// asf/wmv
else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
{
args = "-g 100 -qmax 15";
}
else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
{
args = "-preset superfast";
}
12 years ago
if (request.VideoBitRate.HasValue)
{
12 years ago
args += " -b:v " + request.VideoBitRate;
}
return args.Trim();
}
}
}