]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/formatter/video-format-utils.ts
Merge branch 'master' into release/3.3.0
[github/Chocobozzz/PeerTube.git] / server / models / video / formatter / video-format-utils.ts
1 import { uuidToShort } from '@server/helpers/uuid'
2 import { generateMagnetUri } from '@server/helpers/webtorrent'
3 import { getLocalVideoFileMetadataUrl } from '@server/lib/video-paths'
4 import { VideoFile } from '@shared/models/videos/video-file.model'
5 import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../../shared/models/activitypub/objects'
6 import { Video, VideoDetails } from '../../../../shared/models/videos'
7 import { VideoStreamingPlaylist } from '../../../../shared/models/videos/video-streaming-playlist.model'
8 import { isArray } from '../../../helpers/custom-validators/misc'
9 import {
10 MIMETYPES,
11 VIDEO_CATEGORIES,
12 VIDEO_LANGUAGES,
13 VIDEO_LICENCES,
14 VIDEO_PRIVACIES,
15 VIDEO_STATES,
16 WEBSERVER
17 } from '../../../initializers/constants'
18 import {
19 getLocalVideoCommentsActivityPubUrl,
20 getLocalVideoDislikesActivityPubUrl,
21 getLocalVideoLikesActivityPubUrl,
22 getLocalVideoSharesActivityPubUrl
23 } from '../../../lib/activitypub/url'
24 import {
25 MStreamingPlaylistRedundanciesOpt,
26 MVideo,
27 MVideoAP,
28 MVideoFile,
29 MVideoFormattable,
30 MVideoFormattableDetails
31 } from '../../../types/models'
32 import { MVideoFileRedundanciesOpt } from '../../../types/models/video/video-file'
33 import { VideoCaptionModel } from '../video-caption'
34
35 export type VideoFormattingJSONOptions = {
36 completeDescription?: boolean
37 additionalAttributes: {
38 state?: boolean
39 waitTranscoding?: boolean
40 scheduledUpdate?: boolean
41 blacklistInfo?: boolean
42 }
43 }
44
45 function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFormattingJSONOptions): Video {
46 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
47
48 const videoObject: Video = {
49 id: video.id,
50 uuid: video.uuid,
51 shortUUID: uuidToShort(video.uuid),
52
53 name: video.name,
54 category: {
55 id: video.category,
56 label: getCategoryLabel(video.category)
57 },
58 licence: {
59 id: video.licence,
60 label: getLicenceLabel(video.licence)
61 },
62 language: {
63 id: video.language,
64 label: getLanguageLabel(video.language)
65 },
66 privacy: {
67 id: video.privacy,
68 label: getPrivacyLabel(video.privacy)
69 },
70 nsfw: video.nsfw,
71
72 description: options && options.completeDescription === true
73 ? video.description
74 : video.getTruncatedDescription(),
75
76 isLocal: video.isOwned(),
77 duration: video.duration,
78 views: video.views,
79 likes: video.likes,
80 dislikes: video.dislikes,
81 thumbnailPath: video.getMiniatureStaticPath(),
82 previewPath: video.getPreviewStaticPath(),
83 embedPath: video.getEmbedStaticPath(),
84 createdAt: video.createdAt,
85 updatedAt: video.updatedAt,
86 publishedAt: video.publishedAt,
87 originallyPublishedAt: video.originallyPublishedAt,
88
89 isLive: video.isLive,
90
91 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
92 channel: video.VideoChannel.toFormattedSummaryJSON(),
93
94 userHistory: userHistory
95 ? { currentTime: userHistory.currentTime }
96 : undefined,
97
98 // Can be added by external plugins
99 pluginData: (video as any).pluginData
100 }
101
102 if (options) {
103 if (options.additionalAttributes.state === true) {
104 videoObject.state = {
105 id: video.state,
106 label: getStateLabel(video.state)
107 }
108 }
109
110 if (options.additionalAttributes.waitTranscoding === true) {
111 videoObject.waitTranscoding = video.waitTranscoding
112 }
113
114 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
115 videoObject.scheduledUpdate = {
116 updateAt: video.ScheduleVideoUpdate.updateAt,
117 privacy: video.ScheduleVideoUpdate.privacy || undefined
118 }
119 }
120
121 if (options.additionalAttributes.blacklistInfo === true) {
122 videoObject.blacklisted = !!video.VideoBlacklist
123 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
124 }
125 }
126
127 return videoObject
128 }
129
130 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
131 const formattedJson = video.toFormattedJSON({
132 additionalAttributes: {
133 scheduledUpdate: true,
134 blacklistInfo: true
135 }
136 })
137
138 const tags = video.Tags ? video.Tags.map(t => t.name) : []
139
140 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
141
142 const detailsJson = {
143 support: video.support,
144 descriptionPath: video.getDescriptionAPIPath(),
145 channel: video.VideoChannel.toFormattedJSON(),
146 account: video.VideoChannel.Account.toFormattedJSON(),
147 tags,
148 commentsEnabled: video.commentsEnabled,
149 downloadEnabled: video.downloadEnabled,
150 waitTranscoding: video.waitTranscoding,
151 state: {
152 id: video.state,
153 label: getStateLabel(video.state)
154 },
155
156 trackerUrls: video.getTrackerUrls(),
157
158 files: [],
159 streamingPlaylists
160 }
161
162 // Format and sort video files
163 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
164
165 return Object.assign(formattedJson, detailsJson)
166 }
167
168 function streamingPlaylistsModelToFormattedJSON (
169 video: MVideoFormattableDetails,
170 playlists: MStreamingPlaylistRedundanciesOpt[]
171 ): VideoStreamingPlaylist[] {
172 if (isArray(playlists) === false) return []
173
174 return playlists
175 .map(playlist => {
176 const redundancies = isArray(playlist.RedundancyVideos)
177 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
178 : []
179
180 const files = videoFilesModelToFormattedJSON(video, playlist.VideoFiles)
181
182 return {
183 id: playlist.id,
184 type: playlist.type,
185 playlistUrl: playlist.playlistUrl,
186 segmentsSha256Url: playlist.segmentsSha256Url,
187 redundancies,
188 files
189 }
190 })
191 }
192
193 function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
194 if (fileA.resolution < fileB.resolution) return 1
195 if (fileA.resolution === fileB.resolution) return 0
196 return -1
197 }
198
199 function videoFilesModelToFormattedJSON (
200 video: MVideoFormattableDetails,
201 videoFiles: MVideoFileRedundanciesOpt[],
202 includeMagnet = true
203 ): VideoFile[] {
204 const trackerUrls = includeMagnet
205 ? video.getTrackerUrls()
206 : []
207
208 return [ ...videoFiles ]
209 .filter(f => !f.isLive())
210 .sort(sortByResolutionDesc)
211 .map(videoFile => {
212 return {
213 resolution: {
214 id: videoFile.resolution,
215 label: videoFile.resolution === 0 ? 'Audio' : `${videoFile.resolution}p`
216 },
217
218 magnetUri: includeMagnet && videoFile.hasTorrent()
219 ? generateMagnetUri(video, videoFile, trackerUrls)
220 : undefined,
221
222 size: videoFile.size,
223 fps: videoFile.fps,
224
225 torrentUrl: videoFile.getTorrentUrl(),
226 torrentDownloadUrl: videoFile.getTorrentDownloadUrl(),
227
228 fileUrl: videoFile.getFileUrl(video),
229 fileDownloadUrl: videoFile.getFileDownloadUrl(video),
230
231 metadataUrl: videoFile.metadataUrl ?? getLocalVideoFileMetadataUrl(video, videoFile)
232 } as VideoFile
233 })
234 }
235
236 function addVideoFilesInAPAcc (
237 acc: ActivityUrlObject[] | ActivityTagObject[],
238 video: MVideo,
239 files: MVideoFile[]
240 ) {
241 const trackerUrls = video.getTrackerUrls()
242
243 const sortedFiles = [ ...files ]
244 .filter(f => !f.isLive())
245 .sort(sortByResolutionDesc)
246
247 for (const file of sortedFiles) {
248 acc.push({
249 type: 'Link',
250 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
251 href: file.getFileUrl(video),
252 height: file.resolution,
253 size: file.size,
254 fps: file.fps
255 })
256
257 acc.push({
258 type: 'Link',
259 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
260 mediaType: 'application/json' as 'application/json',
261 href: getLocalVideoFileMetadataUrl(video, file),
262 height: file.resolution,
263 fps: file.fps
264 })
265
266 if (file.hasTorrent()) {
267 acc.push({
268 type: 'Link',
269 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
270 href: file.getTorrentUrl(),
271 height: file.resolution
272 })
273
274 acc.push({
275 type: 'Link',
276 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
277 href: generateMagnetUri(video, file, trackerUrls),
278 height: file.resolution
279 })
280 }
281 }
282 }
283
284 function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
285 if (!video.Tags) video.Tags = []
286
287 const tag = video.Tags.map(t => ({
288 type: 'Hashtag' as 'Hashtag',
289 name: t.name
290 }))
291
292 let language
293 if (video.language) {
294 language = {
295 identifier: video.language,
296 name: getLanguageLabel(video.language)
297 }
298 }
299
300 let category
301 if (video.category) {
302 category = {
303 identifier: video.category + '',
304 name: getCategoryLabel(video.category)
305 }
306 }
307
308 let licence
309 if (video.licence) {
310 licence = {
311 identifier: video.licence + '',
312 name: getLicenceLabel(video.licence)
313 }
314 }
315
316 const url: ActivityUrlObject[] = [
317 // HTML url should be the first element in the array so Mastodon correctly displays the embed
318 {
319 type: 'Link',
320 mediaType: 'text/html',
321 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
322 }
323 ]
324
325 addVideoFilesInAPAcc(url, video, video.VideoFiles || [])
326
327 for (const playlist of (video.VideoStreamingPlaylists || [])) {
328 const tag = playlist.p2pMediaLoaderInfohashes
329 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
330 tag.push({
331 type: 'Link',
332 name: 'sha256',
333 mediaType: 'application/json' as 'application/json',
334 href: playlist.segmentsSha256Url
335 })
336
337 addVideoFilesInAPAcc(tag, video, playlist.VideoFiles || [])
338
339 url.push({
340 type: 'Link',
341 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
342 href: playlist.playlistUrl,
343 tag
344 })
345 }
346
347 for (const trackerUrl of video.getTrackerUrls()) {
348 const rel2 = trackerUrl.startsWith('http')
349 ? 'http'
350 : 'websocket'
351
352 url.push({
353 type: 'Link',
354 name: `tracker-${rel2}`,
355 rel: [ 'tracker', rel2 ],
356 href: trackerUrl
357 })
358 }
359
360 const subtitleLanguage = []
361 for (const caption of video.VideoCaptions) {
362 subtitleLanguage.push({
363 identifier: caption.language,
364 name: VideoCaptionModel.getLanguageLabel(caption.language),
365 url: caption.getFileUrl(video)
366 })
367 }
368
369 const icons = [ video.getMiniature(), video.getPreview() ]
370
371 return {
372 type: 'Video' as 'Video',
373 id: video.url,
374 name: video.name,
375 duration: getActivityStreamDuration(video.duration),
376 uuid: video.uuid,
377 tag,
378 category,
379 licence,
380 language,
381 views: video.views,
382 sensitive: video.nsfw,
383 waitTranscoding: video.waitTranscoding,
384 isLiveBroadcast: video.isLive,
385
386 liveSaveReplay: video.isLive
387 ? video.VideoLive.saveReplay
388 : null,
389
390 permanentLive: video.isLive
391 ? video.VideoLive.permanentLive
392 : null,
393
394 state: video.state,
395 commentsEnabled: video.commentsEnabled,
396 downloadEnabled: video.downloadEnabled,
397 published: video.publishedAt.toISOString(),
398
399 originallyPublishedAt: video.originallyPublishedAt
400 ? video.originallyPublishedAt.toISOString()
401 : null,
402
403 updated: video.updatedAt.toISOString(),
404 mediaType: 'text/markdown',
405 content: video.description,
406 support: video.support,
407 subtitleLanguage,
408 icon: icons.map(i => ({
409 type: 'Image',
410 url: i.getFileUrl(video),
411 mediaType: 'image/jpeg',
412 width: i.width,
413 height: i.height
414 })),
415 url,
416 likes: getLocalVideoLikesActivityPubUrl(video),
417 dislikes: getLocalVideoDislikesActivityPubUrl(video),
418 shares: getLocalVideoSharesActivityPubUrl(video),
419 comments: getLocalVideoCommentsActivityPubUrl(video),
420 attributedTo: [
421 {
422 type: 'Person',
423 id: video.VideoChannel.Account.Actor.url
424 },
425 {
426 type: 'Group',
427 id: video.VideoChannel.Actor.url
428 }
429 ]
430 }
431 }
432
433 function getActivityStreamDuration (duration: number) {
434 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
435 return 'PT' + duration + 'S'
436 }
437
438 function getCategoryLabel (id: number) {
439 return VIDEO_CATEGORIES[id] || 'Misc'
440 }
441
442 function getLicenceLabel (id: number) {
443 return VIDEO_LICENCES[id] || 'Unknown'
444 }
445
446 function getLanguageLabel (id: string) {
447 return VIDEO_LANGUAGES[id] || 'Unknown'
448 }
449
450 function getPrivacyLabel (id: number) {
451 return VIDEO_PRIVACIES[id] || 'Unknown'
452 }
453
454 function getStateLabel (id: number) {
455 return VIDEO_STATES[id] || 'Unknown'
456 }
457
458 export {
459 videoModelToFormattedJSON,
460 videoModelToFormattedDetailsJSON,
461 videoFilesModelToFormattedJSON,
462 videoModelToActivityPubObject,
463 getActivityStreamDuration,
464
465 getCategoryLabel,
466 getLicenceLabel,
467 getLanguageLabel,
468 getPrivacyLabel,
469 getStateLabel
470 }