2013-02-27 04:19:05 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using System;
|
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")]
|
|
|
|
|
[Route("/Videos/{Id}/stream", "GET")]
|
|
|
|
|
public class GetVideoStream : StreamRequest
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2013-02-27 04:19:05 +00:00
|
|
|
|
/// Initializes a new instance of the <see cref="BaseProgressiveStreamingService" /> class.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-27 04:19:05 +00:00
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
public VideoService(IServerApplicationPaths appPaths)
|
|
|
|
|
: base(appPaths)
|
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)
|
|
|
|
|
{
|
|
|
|
|
return ProcessRequest(request);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
var probeSize = ServerKernel.FFMpegManager.GetProbeSizeArgument(video.VideoType, video.IsoType);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
// Get the output codec name
|
2013-02-27 04:19:05 +00:00
|
|
|
|
var videoCodec = GetVideoCodec(state.Request);
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Format("{0} {1} -i {2}{3} -threads 0 {4} {5}{6} {7} \"{8}\"",
|
|
|
|
|
probeSize,
|
2013-02-27 04:19:05 +00:00
|
|
|
|
GetFastSeekCommandLineParameter(state.Request),
|
|
|
|
|
GetInputArgument(video, state.IsoMount),
|
|
|
|
|
GetSlowSeekCommandLineParameter(state.Request),
|
|
|
|
|
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-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;
|
|
|
|
|
|
2013-02-27 04:19:05 +00:00
|
|
|
|
var request = state.Request;
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
// If we're encoding video, add additional params
|
|
|
|
|
if (!videoCodec.Equals("copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
// Add resolution params, if specified
|
2013-02-27 04:19:05 +00:00
|
|
|
|
if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-27 04:19:05 +00:00
|
|
|
|
args += GetOutputSizeParam(state, videoCodec);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-27 04:19:05 +00:00
|
|
|
|
if (request.Framerate.HasValue)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-27 04:19:05 +00:00
|
|
|
|
args += string.Format(" -r {0}", request.Framerate.Value);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the audio bitrate
|
2013-02-27 04:19:05 +00:00
|
|
|
|
var qualityParam = GetVideoQualityParam(request, videoCodec);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(qualityParam))
|
|
|
|
|
{
|
|
|
|
|
args += " " + qualityParam;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-27 04:19:05 +00:00
|
|
|
|
else if (IsH264(state.VideoStream))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
args += " -bsf h264_mp4toannexb";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
var args = "-acodec " + codec;
|
|
|
|
|
|
|
|
|
|
// If we're encoding audio, add additional params
|
|
|
|
|
if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
// Add the number of audio channels
|
2013-02-27 04:19:05 +00:00
|
|
|
|
var channels = GetNumAudioChannelsParam(request, state.AudioStream);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
if (channels.HasValue)
|
|
|
|
|
{
|
|
|
|
|
args += " -ac " + channels.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-27 04:19:05 +00:00
|
|
|
|
if (request.AudioSampleRate.HasValue)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-27 04:19:05 +00:00
|
|
|
|
args += " -ar " + request.AudioSampleRate.Value;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-27 04:19:05 +00:00
|
|
|
|
if (request.AudioBitRate.HasValue)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-27 04:19:05 +00:00
|
|
|
|
args += " -ab " + request.AudioBitRate.Value;
|
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-02-27 04:19:05 +00:00
|
|
|
|
private string GetVideoQualityParam(StreamRequest request, string videoCodec)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|