-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoMetadata.brs
415 lines (299 loc) · 11.4 KB
/
VideoMetadata.brs
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
'*****************************************************************
'** Emby Roku Client - Video Metadata
'*****************************************************************
'**********************************************************
'** Get Video Details
'**********************************************************
Function getVideoMetadata(videoId As String) As Object
' URL
url = GetServerBaseUrl() + "/Users/" + HttpEncode(getGlobalVar("user").Id) + "/Items/" + HttpEncode(videoId)
' Prepare Request
request = HttpRequest(url)
request.ContentType("json")
request.AddAuthorization()
' Execute Request
response = request.GetToStringWithTimeout(10)
if response <> invalid
fixedResponse = normalizeJson(response)
i = ParseJSON(fixedResponse)
' Get Image Sizes
if i.Type = "Episode"
imageStyle = "rounded-rect-16x9-generic"
else
imageStyle = "movie"
end if
return getMetadataFromServerItem(i, 0, imageStyle, "springboard")
else
Debug("Failed to Get Video Metadata")
end if
return invalid
End Function
'**********************************************************
'** addVideoDisplayInfo
'**********************************************************
Sub addVideoDisplayInfo(metaData as Object, item as Object)
mediaStreams = invalid
if item.MediaSources <> invalid and item.MediaSources.Count() > 0 then
mediaStreams = item.MediaSources[0].MediaStreams
end if
if mediaStreams = invalid then mediaStreams = item.MediaStreams
' Can't continue at this point
if mediaStreams = invalid then return
foundVideo = false
for each stream in mediaStreams
if stream.Type = "Video" And foundVideo = false
foundVideo = true
' Determine Full 1080p
if firstOf(stream.Height, 0) >= 1080
metaData.FullHD = true
end if
' Determine Frame Rate
if stream.RealFrameRate <> invalid
if stream.RealFrameRate >= 29
metaData.FrameRate = 30
else
metaData.FrameRate = 24
end if
else if stream.AverageFrameRate <> invalid
if stream.RealFrameRate >= 29
metaData.FrameRate = 30
else
metaData.FrameRate = 24
end if
end if
else if stream.Type = "Audio"
channels = firstOf(stream.Channels, 2)
if channels > 5
metaData.AudioFormat = "dolby-digital"
end if
end if
end for
End Sub
Function getStreamInfo(mediaSource as Object, options as Object) as Object
audioStream = getMediaStream(mediaSource.MediaStreams, "Audio", options.AudioStreamIndex, mediaSource.DefaultAudioStreamIndex)
videoStream = getMediaStream(mediaSource.MediaStreams, "Video", invalid, invalid)
subtitleStream = getMediaStream(mediaSource.MediaStreams, "Subtitle", options.SubtitleStreamIndex, mediaSource.DefaultSubtitleStreamIndex)
streamInfo = {
MediaSource: mediaSource,
VideoStream: videoStream,
AudioStream: audioStream,
SubtitleStream: subtitleStream,
LiveStreamId: mediaSource.LiveStreamId,
CanSeek: mediaSource.RunTimeTicks <> "" And mediaSource.RunTimeTicks <> invalid
}
if audioStream <> invalid then
streamInfo.AudioStreamIndex = audioStream.Index
else
streamInfo.AudioStreamIndex = mediaSource.DefaultAudioStreamIndex
end if
if subtitleStream <> invalid then
streamInfo.SubtitleStreamIndex = subtitleStream.Index
else
streamInfo.SubtitleStreamIndex = mediaSource.DefaultSubtitleStreamIndex
end if
if mediaSource.enableDirectPlay = true then
streamInfo.PlayMethod = "DirectPlay"
streamInfo.Bitrate = mediaSource.Bitrate
else if mediaSource.SupportsDirectStream = true then
streamInfo.PlayMethod = "DirectStream"
streamInfo.Bitrate = mediaSource.Bitrate
else
streamInfo.PlayMethod = "Transcode"
maxVideoBitrate = firstOf(RegRead("prefVideoQuality"), "3200")
maxVideoBitrate = maxVideoBitrate.ToInt()
streamInfo.Bitrate = maxVideoBitrate * 1000
end if
return streamInfo
End Function
Function getMediaStream(mediaStreams, streamType, optionIndex, defaultIndex) as Object
if optionIndex <> invalid then
for each stream in mediaStreams
if stream.Index = optionIndex and stream.Type = streamType then return stream
end for
end if
if defaultIndex <> invalid then
for each stream in mediaStreams
if stream.Index = defaultIndex and stream.Type = streamType then return stream
end for
end if
' We have to return something
if streamType = "Video" or streamType = "Audio" then
for each stream in mediaStreams
if stream.Type = streamType then return stream
end for
end if
return invalid
End Function
'**********************************************************
'** reportPlayback
'**********************************************************
Sub reportPlayback(id As String, mediaType as String, action As String, playMethod as String, isPaused as Boolean, canSeek as Boolean, position as Integer, mediaSourceId as String, playSessionId = invalid, liveStreamId = invalid, audioStreamIndex = invalid, subtitleStreamIndex = invalid)
' Format Position Seconds into Ticks
positionTicks = invalid
if position <> invalid
positionTicks = itostr(position) + "0000000"
end if
url = ""
if action = "start"
' URL
url = GetServerBaseUrl() + "/Sessions/Playing"
Debug ("Reporting playback start")
else if action = "progress"
' URL
url = GetServerBaseUrl() + "/Sessions/Playing/Progress"
else if action = "stop"
' URL
url = GetServerBaseUrl() + "/Sessions/Playing/Stopped"
Debug ("Reporting playback stopped")
end if
url = url + "?itemId=" + id
if positionTicks <> invalid
url = url + "&PositionTicks=" + tostr(positionTicks)
end if
url = url + "&isPaused=" + tostr(isPaused)
url = url + "&canSeek=" + tostr(canSeek)
url = url + "&PlayMethod=" + playMethod
url = url + "&QueueableMediaTypes=" + mediaType
url = url + "&MediaSourceId=" + tostr(mediaSourceId)
if playSessionId <> invalid
url = url + "&PlaySessionId=" + tostr(playSessionId)
end if
if liveStreamId <> invalid
url = url + "&LiveStreamId=" + tostr(liveStreamId)
end if
if audioStreamIndex <> invalid
url = url + "&AudioStreamIndex=" + tostr(audioStreamIndex)
end if
if subtitleStreamIndex <> invalid
url = url + "&SubtitleStreamIndex=" + tostr(subtitleStreamIndex)
end if
if action <> "progress" then
Debug("Reporting playback to " + url)
end if
' Prepare Request
request = HttpRequest(url)
request.AddAuthorization()
context = CreateObject("roAssociativeArray")
GetViewController().StartRequest(request.Http, invalid, context, "", "post")
End Sub
'**********************************************************
'** Post Manual Watched Status
'**********************************************************
Function postWatchedStatus(videoId As String, markWatched As Boolean) As Boolean
' URL
url = GetServerBaseUrl() + "/Users/" + HttpEncode(getGlobalVar("user").Id) + "/PlayedItems/" + HttpEncode(videoId)
' Prepare Request
request = HttpRequest(url)
request.AddAuthorization()
' If marking as unwatched
if Not markWatched
request.SetRequest("DELETE")
end if
' Execute Request
response = request.PostFromStringWithTimeout("", 5)
if response <> invalid
Debug("Mark Played/Unplayed")
return true
else
Debug("Failed to Post Manual Watched Status")
end if
return false
End Function
'**********************************************************
'** Post Favorite Status
'**********************************************************
Function postFavoriteStatus(videoId As String, markFavorite As Boolean) As Boolean
' URL
url = GetServerBaseUrl() + "/Users/" + HttpEncode(getGlobalVar("user").Id) + "/FavoriteItems/" + HttpEncode(videoId)
' Prepare Request
request = HttpRequest(url)
request.AddAuthorization()
' If marking as un-favorite
if Not markFavorite
request.SetRequest("DELETE")
end if
' Execute Request
response = request.PostFromStringWithTimeout("", 5)
if response <> invalid
Debug("Add/Remove Favorite")
return true
else
Debug("Failed to Post Favorite Status")
end if
return false
End Function
'**********************************************************
'** Get Local Trailers
'**********************************************************
Function getLocalTrailers(videoId As String) As Object
' URL
url = GetServerBaseUrl() + "/Users/" + HttpEncode(getGlobalVar("user").Id) + "/Items/" + HttpEncode(videoId) + "/LocalTrailers"
return getSpecialFeaturesFromUrl(url)
End Function
'**********************************************************
'** Get Special Features
'**********************************************************
Function getSpecialFeatures(videoId As String) As Object
' URL
url = GetServerBaseUrl() + "/Users/" + HttpEncode(getGlobalVar("user").Id) + "/Items/" + HttpEncode(videoId) + "/SpecialFeatures"
return getSpecialFeaturesFromUrl(url)
End Function
Function getSpecialFeaturesFromUrl(url As String) As Object
' Prepare Request
request = HttpRequest(url)
request.ContentType("json")
request.AddAuthorization()
' Execute Request
response = request.GetToStringWithTimeout(10)
if response <> invalid
fixedResponse = normalizeJson(response)
contentList = CreateObject("roArray", 25, true)
jsonObj = ParseJSON(fixedResponse)
if jsonObj = invalid
Debug("Error while parsing JSON response")
return invalid
end if
for each i in jsonObj
metaData = getMetadataFromServerItem(i, 0, "flat-episodic-16x9")
contentList.push( metaData )
end for
return contentList
end if
return invalid
End Function
'**********************************************************
'** Get Video Intros
'**********************************************************
Function getVideoIntros(videoId As String) As Object
' URL
url = GetServerBaseUrl() + "/Users/" + HttpEncode(getGlobalVar("user").Id) + "/Items/" + HttpEncode(videoId) + "/Intros"
' Prepare Request
request = HttpRequest(url)
request.ContentType("json")
request.AddAuthorization()
' Execute Request
response = request.GetToStringWithTimeout(10)
if response <> invalid
return parseItemsResponse(response, 0, "flat-episodic-16x9")
end if
return invalid
End Function
'**********************************************************
'** Get Additional Parts
'**********************************************************
Function getAdditionalParts(videoId As String) As Object
' URL
url = GetServerBaseUrl() + "/Videos/" + HttpEncode(videoId) + "/AdditionalParts?UserId=" + HttpEncode(getGlobalVar("user").Id)
' Prepare Request
request = HttpRequest(url)
request.ContentType("json")
request.AddAuthorization()
' Execute Request
response = request.GetToStringWithTimeout(10)
if response <> invalid
return parseItemsResponse(response, 0, "flat-episodic-16x9")
else
createDialog("Response Error!", "Failed to Get Additional Parts", "OK", true)
end if
return invalid
End Function