1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using System;
using MediaBrowser.Controller.Library;
using ServiceStack.ServiceHost;
namespace MediaBrowser.Api.Playback.Progressive
{
/// <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>
/// Class VideoService
/// </summary>
public class VideoService : BaseProgressiveStreamingService
{
public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager)
: base(appPaths, userManager, libraryManager, isoManager)
{
}
/// <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>
/// Gets the command line arguments.
/// </summary>
/// <param name="outputPath">The output path.</param>
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
protected override string GetCommandLineArguments(string outputPath, StreamState state)
{
var video = (Video)state.Item;
var probeSize = Kernel.Instance.FFMpegManager.GetProbeSizeArgument(video.VideoType, video.IsoType);
// Get the output codec name
var videoCodec = GetVideoCodec(state.Request);
var graphicalSubtitleParam = string.Empty;
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))
{
graphicalSubtitleParam = GetInternalGraphicalSubtitleParam(state, videoCodec);
}
}
return string.Format("{0} {1} -i {2}{3} -threads 0 {4} {5}{6} {7} \"{8}\"",
probeSize,
GetFastSeekCommandLineParameter(state.Request),
GetInputArgument(video, state.IsoMount),
GetSlowSeekCommandLineParameter(state.Request),
GetMapArgs(state),
GetVideoArguments(state, videoCodec),
graphicalSubtitleParam,
GetAudioArguments(state),
outputPath
).Trim();
}
/// <summary>
/// Gets video arguments to pass to ffmpeg
/// </summary>
/// <param name="state">The state.</param>
/// <param name="videoCodec">The video codec.</param>
/// <returns>System.String.</returns>
private string GetVideoArguments(StreamState state, string videoCodec)
{
var args = "-vcodec " + videoCodec;
var request = state.Request;
// If we're encoding video, add additional params
if (!videoCodec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
// Add resolution params, if specified
if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
{
args += GetOutputSizeParam(state, videoCodec);
}
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;
}
}
else if (IsH264(state.VideoStream))
{
args += " -bsf h264_mp4toannexb";
}
return args;
}
/// <summary>
/// Gets audio arguments to pass to ffmpeg
/// </summary>
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
private string GetAudioArguments(StreamState state)
{
// If the video doesn't have an audio stream, return a default.
if (state.AudioStream == null)
{
return string.Empty;
}
var request = state.Request;
// Get the output codec name
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
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;
}
}
return args;
}
/// <summary>
/// Gets the video bitrate to specify on the command line
/// </summary>
/// <param name="request">The request.</param>
/// <param name="videoCodec">The video codec.</param>
/// <returns>System.String.</returns>
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";
}
if (request.VideoBitRate.HasValue)
{
args += " -b:v " + request.VideoBitRate;
}
return args.Trim();
}
}
}
|