/
GuiPlayer_Transcoding.js
240 lines (211 loc) · 9.52 KB
/
GuiPlayer_Transcoding.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Render Error 1 : Unsupported container
Render Error 2 : Unsupported video codec
Render Error 3 : Unsupported audio codec
Render Error 4 : Unsupported video resolution
Render Error 6 : Corrupt Stream
*/
var GuiPlayer_Transcoding = {
//File Information
MediaSource : null,
videoIndex : 0,
audioIndex : 0,
//Bitrate check
bitRateToUse : null,
//Boolean that conclude if all Video or All Audio elements will play without transcode
isVideo : true,
isAudio : true,
//All Video Elements
isCodec : null,
isResolution : null,
isContainer : null,
isBitRate : null,
isLevel : null,
isFrameRate : null,
isProfile : null,
maxFrameRate : null,
//All Audio elements
isAudioCodec : null,
isAudioContainer : null,
isAudioChannel : null
}
//--------------------------------------------------------------------------------------
GuiPlayer_Transcoding.start = function(showId, MediaSource,MediaSourceIndex, videoIndex, audioIndex, isFirstAudioIndex, subtitleIndex) {
//Set Class Vars
this.MediaSource = MediaSource;
this.videoIndex = videoIndex;
this.audioIndex = audioIndex;
//Get the Streams actual index
var videoStreamIndex = this.MediaSource.MediaStreams[this.videoIndex].Index;
var audioStreamIndex = this.MediaSource.MediaStreams[this.audioIndex].Index;
//Check Video & Audio Compatibility
this.checkCodec(videoIndex);
this.checkAudioCodec(audioIndex);
var streamparams = "";
var transcodeStatus = "";
//If audiocheck failed convert to AAC OR AC3 depending on setting
//If audiocheck ok convert to AAC or dont convert & leave as original codec
var fileAudioCodec = this.MediaSource.MediaStreams[this.audioIndex].Codec.toLowerCase();
var streamAudioCodec = "aac"; //Default, supported by all tv's (?)
var convertAACtoDolby = false;
if (File.getTVProperty("Dolby") && File.getTVProperty("AACtoDolby") && fileAudioCodec == "aac") {
convertAACtoDolby = true;
}
if (this.isAudio == false) {
streamAudioCodec = (File.getTVProperty("Dolby") && File.getTVProperty("AACtoDolby")) ? "ac3" : "aac";
} else {
streamAudioCodec = (File.getTVProperty("Dolby") && File.getTVProperty("AACtoDolby") && fileAudioCodec == "aac") ? "ac3" : "aac";
}
if (this.isVideo && this.isAudio && convertAACtoDolby == false) {
if (isFirstAudioIndex == true) {
transcodeStatus = "Direct Play";
streamparams = '/Stream.'+this.MediaSource.Container+'?static=true&MediaSourceId='+this.MediaSource.Id + '&api_key=' + Server.getAuthToken();
} else {
transcodeStatus = "Stream Copy - Audio Not First Track";
streamparams = '/master.m3u8?VideoStreamIndex='+videoStreamIndex+'&AudioStreamIndex='+audioStreamIndex+'&VideoCodec=copy&AudioCodec='+ streamAudioCodec + '&SegmentContainer=ts&MinSegments=2&BreakOnNonKeyFrames=True' + '&MediaSourceId='+this.MediaSource.Id + '&api_key=' + Server.getAuthToken();
}
} else if (this.isVideo == false) {
transcodeStatus = "Transcoding Audio & Video";
streamparams = '/master.m3u8?VideoStreamIndex='+videoStreamIndex+'&AudioStreamIndex='+audioStreamIndex+'&VideoCodec=h264&Profile=high&Level=41&MaxVideoBitDepth=8&MaxWidth=1920&VideoBitrate='+this.bitRateToUse+'&MaxFramerate='+this.maxFrameRate+'&AudioCodec=' + streamAudioCodec +'&AudioBitrate=360000&TranscodingMaxAudioChannels=6'+'&SegmentContainer=ts&MinSegments=2&BreakOnNonKeyFrames=True'+'&MediaSourceId='+this.MediaSource.Id + '&api_key=' + Server.getAuthToken();
} else if (this.isVideo == true && (this.isAudio == false || convertAACtoDolby == true)) {
transcodeStatus = "Transcoding Audio";
streamparams = '/master.m3u8?VideoStreamIndex='+videoStreamIndex+'&AudioStreamIndex='+audioStreamIndex+'&VideoCodec=copy&AudioCodec='+ streamAudioCodec +'&audioBitrate=360000&TranscodingMaxAudioChannels=6'+'&SegmentContainer=ts&MinSegments=2&BreakOnNonKeyFrames=True'+'&MediaSourceId='+this.MediaSource.Id + '&api_key=' + Server.getAuthToken();
}
var url = Server.getServerAddr() + '/Videos/' + showId + streamparams + '&DeviceId='+Server.getDeviceID();
FileLog.write("Video : Transcode Status : " + transcodeStatus);
FileLog.write("Video : URL : " + url);
//Return results to Versions
//MediaSourceId,Url,transcodeStatus,videoIndex,audioIndex
return [MediaSourceIndex,url,transcodeStatus,videoIndex,audioIndex,subtitleIndex];
}
GuiPlayer_Transcoding.checkCodec = function() {
var codec = this.MediaSource.MediaStreams[this.videoIndex].Codec.toLowerCase();
var codecParams = GuiPlayer_TranscodeParams.getParameters(codec,this.MediaSource.MediaStreams[this.videoIndex].Width);
this.isCodec = codecParams[0];
this.isContainer = this.checkContainer(codecParams[1]);
this.isResolution = this.checkResolution(codecParams[2]);
this.isBitRate = this.checkBitRate(codecParams[3]);
this.isFrameRate = this.checkFrameRate(codecParams[4]);
this.isLevel = this.checkLevel(codecParams[5]);
this.isProfile = this.checkProfile(codecParams[6]);
this.maxFrameRate = codecParams[4] || 30;
//Results
FileLog.write("Video : Video File Analysis Results");
FileLog.write("Video : Codec Compatibility: " + this.isCodec + " : " + this.MediaSource.MediaStreams[this.videoIndex].Codec);
FileLog.write("Video : Container Compatibility: " + this.isContainer + " : " + this.MediaSource.Container);
FileLog.write("Video : Resolution Compatibility: " + this.isResolution + " : " +this.MediaSource.MediaStreams[this.videoIndex].Width + "x" + this.MediaSource.MediaStreams[this.videoIndex].Height);
FileLog.write("Video : BitRate Compatibility: " + this.isBitRate + " : " + this.MediaSource.MediaStreams[this.videoIndex].BitRate + " : " + this.bitRateToUse);
FileLog.write("Video : FrameRate Compatibility: " + this.isFrameRate + " : " + this.MediaSource.MediaStreams[this.videoIndex].AverageFrameRate);
FileLog.write("Video : Level Compatibility: " + this.isLevel + " : " + this.MediaSource.MediaStreams[this.videoIndex].Level);
FileLog.write("Video : Profile Compatibility: " + this.isProfile + " : " + this.MediaSource.MediaStreams[this.videoIndex].Profile);
//Put it all together
if (this.isCodec && this.isContainer && this.isResolution && this.isBitRate && this.isFrameRate && this.isLevel && this.isProfile) { //
this.isVideo = true;
} else {
this.isVideo = false;
}
}
GuiPlayer_Transcoding.checkAudioCodec = function() {
var audiocodec = this.MediaSource.MediaStreams[this.audioIndex].Codec.toLowerCase();
var audiocodecParams = GuiPlayer_TranscodeParams.getAudioParameters(audiocodec);
this.isAudioCodec = audiocodecParams[0];
this.isAudioContainer = this.checkContainer(audiocodecParams[1]);
this.isAudioChannel = this.checkAudioChannels(audiocodecParams[2]);
//Results
FileLog.write("Video : Audio File Analysis Results");
FileLog.write("Video : Codec Compatibility: " + this.isAudioCodec + " : " + this.MediaSource.MediaStreams[this.audioIndex].Codec);
FileLog.write("Video : Container Compatibility: " + this.isAudioContainer + " : " + this.MediaSource.Container);
FileLog.write("Video : Channel Compatibility: " + this.isAudioChannel + " : " + this.MediaSource.MediaStreams[this.audioIndex].Channels);
//Put it all together
if (this.isAudioCodec && this.isAudioChannel) {
this.isAudio = true;
} else {
this.isAudio = false;
}
}
GuiPlayer_Transcoding.checkAudioChannels = function(maxChannels) {
if (maxChannels == null) {
return false;
} else {
if (this.MediaSource.MediaStreams[this.audioIndex].Channels <= maxChannels) {
return true;
} else {
return false;
}
}
}
GuiPlayer_Transcoding.checkResolution = function(maxResolution) {
if (maxResolution == null) {
return false;
} else if (this.MediaSource.MediaStreams[this.videoIndex].Width <= maxResolution[0] && this.MediaSource.MediaStreams[this.videoIndex].Height <= maxResolution[1]) {
return true;
} else {
return false;
}
}
GuiPlayer_Transcoding.checkContainer = function(supportedContainers) {
if (supportedContainers == null) {
return false
} else {
var isContainer = false;
for (var index = 0; index < supportedContainers.length; index++) {
if (this.MediaSource.Container.toLowerCase() == supportedContainers[index]) {
isContainer = true;
break;
}
}
return isContainer;
}
}
GuiPlayer_Transcoding.checkBitRate = function(maxBitRate) {
//Get Bitrate from Settings File
var maxBitRateSetting = File.getTVProperty("Bitrate")*1024*1024;
// MCB - Ignore bitrate in file
this.bitRateToUse = maxBitRateSetting;
if (this.MediaSource.MediaStreams[this.videoIndex].BitRate > maxBitRateSetting) {
return false;
} else {
return true;
}
}
GuiPlayer_Transcoding.checkFrameRate = function(maxFrameRate) {
if (maxFrameRate == null) {
return false;
} else if (this.MediaSource.MediaStreams[this.videoIndex].AverageFrameRate <= maxFrameRate) {
return true;
} else {
return false;
}
}
GuiPlayer_Transcoding.checkLevel = function(maxLevel) {
var level = this.MediaSource.MediaStreams[this.videoIndex].Level;
if (maxLevel == null) {
return false;
} if (maxLevel == true) {
return true;
} else {
var level = this.MediaSource.MediaStreams[this.videoIndex].Level;
level = (level < 10) ? level * 10 : level; //If only 1 long, multiply by 10 to make it correct!
if (level <= maxLevel && level >= 0) {
return true;
} else {
return false;
}
}
}
GuiPlayer_Transcoding.checkProfile = function(supportedProfiles) {
if (supportedProfiles == null) {
return false;
} if (supportedProfiles == true) {
return true;
} else {
var profile = false;
for (var index = 0; index < supportedProfiles.length; index++) {
if (this.MediaSource.MediaStreams[this.videoIndex].Profile == supportedProfiles[index]) {
profile = true;
break;
}
}
return profile;
}
}