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
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Api.HttpHandlers
{
/// <summary>
/// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
/// </summary>
class VideoHandler : BaseMediaHandler<Video>
{
/// <summary>
/// We can output these files directly, but we can't encode them
/// </summary>
protected override IEnumerable<string> UnsupportedOutputEncodingFormats
{
get
{
return new string[] { "mp4", "wmv", "3gp", "avi", "ogv", "mov", "m4v", "mkv" };
}
}
protected override bool RequiresConversion()
{
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
// For now we won't allow these to pass through.
// Later we'll add some intelligence to allow it when possible
if (currentFormat.Equals("mp4", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("m4v", StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (base.RequiresConversion())
{
return true;
}
AudioStream audio = LibraryItem.AudioStreams.FirstOrDefault();
if (audio != null)
{
// If the number of channels is greater than our desired channels, we need to transcode
if (AudioChannels.HasValue && AudioChannels.Value < audio.Channels)
{
return true;
}
}
// Yay
return false;
}
/// <summary>
/// Translates the file extension to the format param that follows "-f" on the ffmpeg command line
/// </summary>
private string GetFFMpegOutputFormat(string outputFormat)
{
if (outputFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase))
{
return "matroska";
}
else if (outputFormat.Equals("ts", StringComparison.OrdinalIgnoreCase))
{
return "mpegts";
}
else if (outputFormat.Equals("ogv", StringComparison.OrdinalIgnoreCase))
{
return "ogg";
}
return outputFormat;
}
/// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
protected override string GetCommandLineArguments()
{
List<string> audioTranscodeParams = new List<string>();
string outputFormat = GetConversionOutputFormat();
return string.Format("-i \"{0}\" -threads 0 {1} {2} -f {3} -",
LibraryItem.Path,
GetVideoArguments(outputFormat),
GetAudioArguments(outputFormat),
GetFFMpegOutputFormat(outputFormat)
);
}
private string GetVideoArguments(string outputFormat)
{
string codec = GetVideoCodec(outputFormat);
string args = "-vcodec " + codec;
return args;
}
private string GetAudioArguments(string outputFormat)
{
AudioStream audioStream = LibraryItem.AudioStreams.FirstOrDefault();
if (audioStream == null)
{
return string.Empty;
}
string codec = GetAudioCodec(audioStream, outputFormat);
string args = "-acodec " + codec;
if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels);
if (channels.HasValue)
{
args += " -ac " + channels.Value;
}
int? sampleRate = GetSampleRateParam(audioStream.SampleRate);
if (sampleRate.HasValue)
{
args += " -ar " + sampleRate.Value;
}
}
return args;
}
private string GetVideoCodec(string outputFormat)
{
if (outputFormat.Equals("webm"))
{
// Per webm specification, it must be vpx
return "libvpx";
}
else if (outputFormat.Equals("asf"))
{
return "wmv2";
}
return "libx264";
}
private string GetAudioCodec(AudioStream audioStream, string outputFormat)
{
if (outputFormat.Equals("webm"))
{
// Per webm specification, it must be vorbis
return "libvorbis";
}
// See if we can just copy the stream
if (HasBasicAudio(audioStream))
{
return "copy";
}
return "libvo_aacenc";
}
private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels)
{
if (libraryItemChannels > 2 && audioCodec.Equals("libvo_aacenc"))
{
// libvo_aacenc currently only supports two channel output
return 2;
}
return GetNumAudioChannelsParam(libraryItemChannels);
}
private bool HasBasicAudio(AudioStream audio)
{
int maxChannels = AudioChannels ?? 2;
if (audio.Channels > maxChannels)
{
return false;
}
if (audio.AudioFormat.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
{
return true;
}
if (audio.AudioFormat.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
{
return true;
}
if (audio.AudioFormat.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
{
return true;
}
return false;
}
}
}
|