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