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