]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video.ts
Implement captions/subtitles
[github/Chocobozzz/PeerTube.git] / server / models / video / video.ts
1 import * as Bluebird from 'bluebird'
2 import { map, maxBy } from 'lodash'
3 import * as magnetUtil from 'magnet-uri'
4 import * as parseTorrent from 'parse-torrent'
5 import { extname, join } from 'path'
6 import * as Sequelize from 'sequelize'
7 import {
8 AllowNull,
9 BeforeDestroy,
10 BelongsTo,
11 BelongsToMany,
12 Column,
13 CreatedAt,
14 DataType,
15 Default,
16 ForeignKey,
17 HasMany,
18 HasOne,
19 IFindOptions,
20 Is,
21 IsInt,
22 IsUUID,
23 Min,
24 Model,
25 Scopes,
26 Table,
27 UpdatedAt
28 } from 'sequelize-typescript'
29 import { VideoPrivacy, VideoResolution, VideoState } from '../../../shared'
30 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
31 import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
32 import { VideoFilter } from '../../../shared/models/videos/video-query.type'
33 import {
34 copyFilePromise,
35 createTorrentPromise,
36 peertubeTruncate,
37 renamePromise,
38 statPromise,
39 unlinkPromise,
40 writeFilePromise
41 } from '../../helpers/core-utils'
42 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
43 import { isBooleanValid } from '../../helpers/custom-validators/misc'
44 import {
45 isVideoCategoryValid,
46 isVideoDescriptionValid,
47 isVideoDurationValid,
48 isVideoLanguageValid,
49 isVideoLicenceValid,
50 isVideoNameValid,
51 isVideoPrivacyValid,
52 isVideoStateValid,
53 isVideoSupportValid
54 } from '../../helpers/custom-validators/videos'
55 import { generateImageFromVideoFile, getVideoFileFPS, getVideoFileResolution, transcode } from '../../helpers/ffmpeg-utils'
56 import { logger } from '../../helpers/logger'
57 import { getServerActor } from '../../helpers/utils'
58 import {
59 API_VERSION,
60 CONFIG,
61 CONSTRAINTS_FIELDS,
62 PREVIEWS_SIZE,
63 REMOTE_SCHEME,
64 STATIC_DOWNLOAD_PATHS,
65 STATIC_PATHS,
66 THUMBNAILS_SIZE,
67 VIDEO_CATEGORIES,
68 VIDEO_EXT_MIMETYPE,
69 VIDEO_LANGUAGES,
70 VIDEO_LICENCES,
71 VIDEO_PRIVACIES,
72 VIDEO_STATES
73 } from '../../initializers'
74 import {
75 getVideoCommentsActivityPubUrl,
76 getVideoDislikesActivityPubUrl,
77 getVideoLikesActivityPubUrl,
78 getVideoSharesActivityPubUrl
79 } from '../../lib/activitypub'
80 import { sendDeleteVideo } from '../../lib/activitypub/send'
81 import { AccountModel } from '../account/account'
82 import { AccountVideoRateModel } from '../account/account-video-rate'
83 import { ActorModel } from '../activitypub/actor'
84 import { AvatarModel } from '../avatar/avatar'
85 import { ServerModel } from '../server/server'
86 import { getSort, throwIfNotValid } from '../utils'
87 import { TagModel } from './tag'
88 import { VideoAbuseModel } from './video-abuse'
89 import { VideoChannelModel } from './video-channel'
90 import { VideoCommentModel } from './video-comment'
91 import { VideoFileModel } from './video-file'
92 import { VideoShareModel } from './video-share'
93 import { VideoTagModel } from './video-tag'
94 import { ScheduleVideoUpdateModel } from './schedule-video-update'
95 import { VideoCaptionModel } from './video-caption'
96
97 export enum ScopeNames {
98 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
99 WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS',
100 WITH_TAGS = 'WITH_TAGS',
101 WITH_FILES = 'WITH_FILES',
102 WITH_SCHEDULED_UPDATE = 'WITH_SCHEDULED_UPDATE'
103 }
104
105 @Scopes({
106 [ScopeNames.AVAILABLE_FOR_LIST]: (options: {
107 actorId: number,
108 hideNSFW: boolean,
109 filter?: VideoFilter,
110 category?: number,
111 withFiles?: boolean,
112 accountId?: number,
113 videoChannelId?: number
114 }) => {
115 const accountInclude = {
116 attributes: [ 'id', 'name' ],
117 model: AccountModel.unscoped(),
118 required: true,
119 where: {},
120 include: [
121 {
122 attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
123 model: ActorModel.unscoped(),
124 required: true,
125 where: VideoModel.buildActorWhereWithFilter(options.filter),
126 include: [
127 {
128 attributes: [ 'host' ],
129 model: ServerModel.unscoped(),
130 required: false
131 },
132 {
133 model: AvatarModel.unscoped(),
134 required: false
135 }
136 ]
137 }
138 ]
139 }
140
141 const videoChannelInclude = {
142 attributes: [ 'name', 'description', 'id' ],
143 model: VideoChannelModel.unscoped(),
144 required: true,
145 where: {},
146 include: [
147 {
148 attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
149 model: ActorModel.unscoped(),
150 required: true,
151 include: [
152 {
153 attributes: [ 'host' ],
154 model: ServerModel.unscoped(),
155 required: false
156 },
157 {
158 model: AvatarModel.unscoped(),
159 required: false
160 }
161 ]
162 },
163 accountInclude
164 ]
165 }
166
167 // Force actorId to be a number to avoid SQL injections
168 const actorIdNumber = parseInt(options.actorId.toString(), 10)
169 const query: IFindOptions<VideoModel> = {
170 where: {
171 id: {
172 [Sequelize.Op.notIn]: Sequelize.literal(
173 '(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
174 ),
175 [ Sequelize.Op.in ]: Sequelize.literal(
176 '(' +
177 'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
178 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
179 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
180 ' UNION ' +
181 'SELECT "video"."id" AS "id" FROM "video" ' +
182 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
183 'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
184 'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
185 'LEFT JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
186 'WHERE "actor"."serverId" IS NULL OR "actorFollow"."actorId" = ' + actorIdNumber +
187 ')'
188 )
189 },
190 // Always list public videos
191 privacy: VideoPrivacy.PUBLIC,
192 // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding
193 [ Sequelize.Op.or ]: [
194 {
195 state: VideoState.PUBLISHED
196 },
197 {
198 [ Sequelize.Op.and ]: {
199 state: VideoState.TO_TRANSCODE,
200 waitTranscoding: false
201 }
202 }
203 ]
204 },
205 include: [ videoChannelInclude ]
206 }
207
208 if (options.withFiles === true) {
209 query.include.push({
210 model: VideoFileModel.unscoped(),
211 required: true
212 })
213 }
214
215 // Hide nsfw videos?
216 if (options.hideNSFW === true) {
217 query.where['nsfw'] = false
218 }
219
220 if (options.category) {
221 query.where['category'] = options.category
222 }
223
224 if (options.accountId) {
225 accountInclude.where = {
226 id: options.accountId
227 }
228 }
229
230 if (options.videoChannelId) {
231 videoChannelInclude.where = {
232 id: options.videoChannelId
233 }
234 }
235
236 return query
237 },
238 [ScopeNames.WITH_ACCOUNT_DETAILS]: {
239 include: [
240 {
241 model: () => VideoChannelModel.unscoped(),
242 required: true,
243 include: [
244 {
245 attributes: {
246 exclude: [ 'privateKey', 'publicKey' ]
247 },
248 model: () => ActorModel.unscoped(),
249 required: true,
250 include: [
251 {
252 attributes: [ 'host' ],
253 model: () => ServerModel.unscoped(),
254 required: false
255 },
256 {
257 model: () => AvatarModel.unscoped(),
258 required: false
259 }
260 ]
261 },
262 {
263 model: () => AccountModel.unscoped(),
264 required: true,
265 include: [
266 {
267 model: () => ActorModel.unscoped(),
268 attributes: {
269 exclude: [ 'privateKey', 'publicKey' ]
270 },
271 required: true,
272 include: [
273 {
274 attributes: [ 'host' ],
275 model: () => ServerModel.unscoped(),
276 required: false
277 },
278 {
279 model: () => AvatarModel.unscoped(),
280 required: false
281 }
282 ]
283 }
284 ]
285 }
286 ]
287 }
288 ]
289 },
290 [ScopeNames.WITH_TAGS]: {
291 include: [ () => TagModel ]
292 },
293 [ScopeNames.WITH_FILES]: {
294 include: [
295 {
296 model: () => VideoFileModel.unscoped(),
297 required: true
298 }
299 ]
300 },
301 [ScopeNames.WITH_SCHEDULED_UPDATE]: {
302 include: [
303 {
304 model: () => ScheduleVideoUpdateModel.unscoped(),
305 required: false
306 }
307 ]
308 }
309 })
310 @Table({
311 tableName: 'video',
312 indexes: [
313 {
314 fields: [ 'name' ]
315 },
316 {
317 fields: [ 'createdAt' ]
318 },
319 {
320 fields: [ 'duration' ]
321 },
322 {
323 fields: [ 'views' ]
324 },
325 {
326 fields: [ 'likes' ]
327 },
328 {
329 fields: [ 'uuid' ]
330 },
331 {
332 fields: [ 'channelId' ]
333 },
334 {
335 fields: [ 'id', 'privacy', 'state', 'waitTranscoding' ]
336 },
337 {
338 fields: [ 'url'],
339 unique: true
340 }
341 ]
342 })
343 export class VideoModel extends Model<VideoModel> {
344
345 @AllowNull(false)
346 @Default(DataType.UUIDV4)
347 @IsUUID(4)
348 @Column(DataType.UUID)
349 uuid: string
350
351 @AllowNull(false)
352 @Is('VideoName', value => throwIfNotValid(value, isVideoNameValid, 'name'))
353 @Column
354 name: string
355
356 @AllowNull(true)
357 @Default(null)
358 @Is('VideoCategory', value => throwIfNotValid(value, isVideoCategoryValid, 'category'))
359 @Column
360 category: number
361
362 @AllowNull(true)
363 @Default(null)
364 @Is('VideoLicence', value => throwIfNotValid(value, isVideoLicenceValid, 'licence'))
365 @Column
366 licence: number
367
368 @AllowNull(true)
369 @Default(null)
370 @Is('VideoLanguage', value => throwIfNotValid(value, isVideoLanguageValid, 'language'))
371 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.LANGUAGE.max))
372 language: string
373
374 @AllowNull(false)
375 @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
376 @Column
377 privacy: number
378
379 @AllowNull(false)
380 @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean'))
381 @Column
382 nsfw: boolean
383
384 @AllowNull(true)
385 @Default(null)
386 @Is('VideoDescription', value => throwIfNotValid(value, isVideoDescriptionValid, 'description'))
387 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max))
388 description: string
389
390 @AllowNull(true)
391 @Default(null)
392 @Is('VideoSupport', value => throwIfNotValid(value, isVideoSupportValid, 'support'))
393 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.SUPPORT.max))
394 support: string
395
396 @AllowNull(false)
397 @Is('VideoDuration', value => throwIfNotValid(value, isVideoDurationValid, 'duration'))
398 @Column
399 duration: number
400
401 @AllowNull(false)
402 @Default(0)
403 @IsInt
404 @Min(0)
405 @Column
406 views: number
407
408 @AllowNull(false)
409 @Default(0)
410 @IsInt
411 @Min(0)
412 @Column
413 likes: number
414
415 @AllowNull(false)
416 @Default(0)
417 @IsInt
418 @Min(0)
419 @Column
420 dislikes: number
421
422 @AllowNull(false)
423 @Column
424 remote: boolean
425
426 @AllowNull(false)
427 @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
428 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
429 url: string
430
431 @AllowNull(false)
432 @Column
433 commentsEnabled: boolean
434
435 @AllowNull(false)
436 @Column
437 waitTranscoding: boolean
438
439 @AllowNull(false)
440 @Default(null)
441 @Is('VideoState', value => throwIfNotValid(value, isVideoStateValid, 'state'))
442 @Column
443 state: VideoState
444
445 @CreatedAt
446 createdAt: Date
447
448 @UpdatedAt
449 updatedAt: Date
450
451 @AllowNull(false)
452 @Default(Sequelize.NOW)
453 @Column
454 publishedAt: Date
455
456 @ForeignKey(() => VideoChannelModel)
457 @Column
458 channelId: number
459
460 @BelongsTo(() => VideoChannelModel, {
461 foreignKey: {
462 allowNull: true
463 },
464 hooks: true
465 })
466 VideoChannel: VideoChannelModel
467
468 @BelongsToMany(() => TagModel, {
469 foreignKey: 'videoId',
470 through: () => VideoTagModel,
471 onDelete: 'CASCADE'
472 })
473 Tags: TagModel[]
474
475 @HasMany(() => VideoAbuseModel, {
476 foreignKey: {
477 name: 'videoId',
478 allowNull: false
479 },
480 onDelete: 'cascade'
481 })
482 VideoAbuses: VideoAbuseModel[]
483
484 @HasMany(() => VideoFileModel, {
485 foreignKey: {
486 name: 'videoId',
487 allowNull: false
488 },
489 onDelete: 'cascade'
490 })
491 VideoFiles: VideoFileModel[]
492
493 @HasMany(() => VideoShareModel, {
494 foreignKey: {
495 name: 'videoId',
496 allowNull: false
497 },
498 onDelete: 'cascade'
499 })
500 VideoShares: VideoShareModel[]
501
502 @HasMany(() => AccountVideoRateModel, {
503 foreignKey: {
504 name: 'videoId',
505 allowNull: false
506 },
507 onDelete: 'cascade'
508 })
509 AccountVideoRates: AccountVideoRateModel[]
510
511 @HasMany(() => VideoCommentModel, {
512 foreignKey: {
513 name: 'videoId',
514 allowNull: false
515 },
516 onDelete: 'cascade',
517 hooks: true
518 })
519 VideoComments: VideoCommentModel[]
520
521 @HasOne(() => ScheduleVideoUpdateModel, {
522 foreignKey: {
523 name: 'videoId',
524 allowNull: false
525 },
526 onDelete: 'cascade'
527 })
528 ScheduleVideoUpdate: ScheduleVideoUpdateModel
529
530 @HasMany(() => VideoCaptionModel, {
531 foreignKey: {
532 name: 'videoId',
533 allowNull: false
534 },
535 onDelete: 'cascade',
536 hooks: true,
537 ['separate' as any]: true
538 })
539 VideoCaptions: VideoCaptionModel[]
540
541 @BeforeDestroy
542 static async sendDelete (instance: VideoModel, options) {
543 if (instance.isOwned()) {
544 if (!instance.VideoChannel) {
545 instance.VideoChannel = await instance.$get('VideoChannel', {
546 include: [
547 {
548 model: AccountModel,
549 include: [ ActorModel ]
550 }
551 ],
552 transaction: options.transaction
553 }) as VideoChannelModel
554 }
555
556 logger.debug('Sending delete of video %s.', instance.url)
557
558 return sendDeleteVideo(instance, options.transaction)
559 }
560
561 return undefined
562 }
563
564 @BeforeDestroy
565 static async removeFiles (instance: VideoModel) {
566 const tasks: Promise<any>[] = []
567
568 logger.debug('Removing files of video %s.', instance.url)
569
570 tasks.push(instance.removeThumbnail())
571
572 if (instance.isOwned()) {
573 if (!Array.isArray(instance.VideoFiles)) {
574 instance.VideoFiles = await instance.$get('VideoFiles') as VideoFileModel[]
575 }
576
577 tasks.push(instance.removePreview())
578
579 // Remove physical files and torrents
580 instance.VideoFiles.forEach(file => {
581 tasks.push(instance.removeFile(file))
582 tasks.push(instance.removeTorrent(file))
583 })
584 }
585
586 // Do not wait video deletion because we could be in a transaction
587 Promise.all(tasks)
588 .catch(err => {
589 logger.error('Some errors when removing files of video %s in after destroy hook.', instance.uuid, { err })
590 })
591
592 return undefined
593 }
594
595 static list () {
596 return VideoModel.scope(ScopeNames.WITH_FILES).findAll()
597 }
598
599 static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
600 function getRawQuery (select: string) {
601 const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
602 'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
603 'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' +
604 'WHERE "Account"."actorId" = ' + actorId
605 const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
606 'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
607 'WHERE "VideoShare"."actorId" = ' + actorId
608
609 return `(${queryVideo}) UNION (${queryVideoShare})`
610 }
611
612 const rawQuery = getRawQuery('"Video"."id"')
613 const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
614
615 const query = {
616 distinct: true,
617 offset: start,
618 limit: count,
619 order: getSort('createdAt', [ 'Tags', 'name', 'ASC' ]),
620 where: {
621 id: {
622 [Sequelize.Op.in]: Sequelize.literal('(' + rawQuery + ')')
623 },
624 [Sequelize.Op.or]: [
625 { privacy: VideoPrivacy.PUBLIC },
626 { privacy: VideoPrivacy.UNLISTED }
627 ]
628 },
629 include: [
630 {
631 attributes: [ 'language' ],
632 model: VideoCaptionModel.unscoped(),
633 required: false
634 },
635 {
636 attributes: [ 'id', 'url' ],
637 model: VideoShareModel.unscoped(),
638 required: false,
639 // We only want videos shared by this actor
640 where: {
641 [Sequelize.Op.and]: [
642 {
643 id: {
644 [Sequelize.Op.not]: null
645 }
646 },
647 {
648 actorId
649 }
650 ]
651 },
652 include: [
653 {
654 attributes: [ 'id', 'url' ],
655 model: ActorModel.unscoped()
656 }
657 ]
658 },
659 {
660 model: VideoChannelModel.unscoped(),
661 required: true,
662 include: [
663 {
664 attributes: [ 'name' ],
665 model: AccountModel.unscoped(),
666 required: true,
667 include: [
668 {
669 attributes: [ 'id', 'url', 'followersUrl' ],
670 model: ActorModel.unscoped(),
671 required: true
672 }
673 ]
674 },
675 {
676 attributes: [ 'id', 'url', 'followersUrl' ],
677 model: ActorModel.unscoped(),
678 required: true
679 }
680 ]
681 },
682 VideoFileModel,
683 TagModel
684 ]
685 }
686
687 return Bluebird.all([
688 // FIXME: typing issue
689 VideoModel.findAll(query as any),
690 VideoModel.sequelize.query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
691 ]).then(([ rows, totals ]) => {
692 // totals: totalVideos + totalVideoShares
693 let totalVideos = 0
694 let totalVideoShares = 0
695 if (totals[0]) totalVideos = parseInt(totals[0].total, 10)
696 if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10)
697
698 const total = totalVideos + totalVideoShares
699 return {
700 data: rows,
701 total: total
702 }
703 })
704 }
705
706 static listUserVideosForApi (accountId: number, start: number, count: number, sort: string, hideNSFW: boolean, withFiles = false) {
707 const query: IFindOptions<VideoModel> = {
708 offset: start,
709 limit: count,
710 order: getSort(sort),
711 include: [
712 {
713 model: VideoChannelModel,
714 required: true,
715 include: [
716 {
717 model: AccountModel,
718 where: {
719 id: accountId
720 },
721 required: true
722 }
723 ]
724 },
725 {
726 model: ScheduleVideoUpdateModel,
727 required: false
728 }
729 ]
730 }
731
732 if (withFiles === true) {
733 query.include.push({
734 model: VideoFileModel.unscoped(),
735 required: true
736 })
737 }
738
739 if (hideNSFW === true) {
740 query.where = {
741 nsfw: false
742 }
743 }
744
745 return VideoModel.findAndCountAll(query).then(({ rows, count }) => {
746 return {
747 data: rows,
748 total: count
749 }
750 })
751 }
752
753 static async listForApi (options: {
754 start: number,
755 count: number,
756 sort: string,
757 hideNSFW: boolean,
758 withFiles: boolean,
759 category?: number,
760 filter?: VideoFilter,
761 accountId?: number,
762 videoChannelId?: number
763 }) {
764 const query = {
765 offset: options.start,
766 limit: options.count,
767 order: getSort(options.sort)
768 }
769
770 const serverActor = await getServerActor()
771 const scopes = {
772 method: [
773 ScopeNames.AVAILABLE_FOR_LIST, {
774 actorId: serverActor.id,
775 hideNSFW: options.hideNSFW,
776 category: options.category,
777 filter: options.filter,
778 withFiles: options.withFiles,
779 accountId: options.accountId,
780 videoChannelId: options.videoChannelId
781 }
782 ]
783 }
784
785 return VideoModel.scope(scopes)
786 .findAndCountAll(query)
787 .then(({ rows, count }) => {
788 return {
789 data: rows,
790 total: count
791 }
792 })
793 }
794
795 static async searchAndPopulateAccountAndServer (value: string, start: number, count: number, sort: string, hideNSFW: boolean) {
796 const query: IFindOptions<VideoModel> = {
797 offset: start,
798 limit: count,
799 order: getSort(sort),
800 where: {
801 [Sequelize.Op.or]: [
802 {
803 name: {
804 [ Sequelize.Op.iLike ]: '%' + value + '%'
805 }
806 },
807 {
808 preferredUsernameChannel: Sequelize.where(Sequelize.col('VideoChannel->Actor.preferredUsername'), {
809 [ Sequelize.Op.iLike ]: '%' + value + '%'
810 })
811 },
812 {
813 preferredUsernameAccount: Sequelize.where(Sequelize.col('VideoChannel->Account->Actor.preferredUsername'), {
814 [ Sequelize.Op.iLike ]: '%' + value + '%'
815 })
816 },
817 {
818 host: Sequelize.where(Sequelize.col('VideoChannel->Account->Actor->Server.host'), {
819 [ Sequelize.Op.iLike ]: '%' + value + '%'
820 })
821 }
822 ]
823 }
824 }
825
826 const serverActor = await getServerActor()
827 const scopes = {
828 method: [
829 ScopeNames.AVAILABLE_FOR_LIST, {
830 actorId: serverActor.id,
831 hideNSFW
832 }
833 ]
834 }
835
836 return VideoModel.scope(scopes)
837 .findAndCountAll(query)
838 .then(({ rows, count }) => {
839 return {
840 data: rows,
841 total: count
842 }
843 })
844 }
845
846 static load (id: number) {
847 return VideoModel.findById(id)
848 }
849
850 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
851 const query: IFindOptions<VideoModel> = {
852 where: {
853 url
854 }
855 }
856
857 if (t !== undefined) query.transaction = t
858
859 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
860 }
861
862 static loadByUUIDOrURLAndPopulateAccount (uuid: string, url: string, t?: Sequelize.Transaction) {
863 const query: IFindOptions<VideoModel> = {
864 where: {
865 [Sequelize.Op.or]: [
866 { uuid },
867 { url }
868 ]
869 }
870 }
871
872 if (t !== undefined) query.transaction = t
873
874 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
875 }
876
877 static loadAndPopulateAccountAndServerAndTags (id: number) {
878 const options = {
879 order: [ [ 'Tags', 'name', 'ASC' ] ]
880 }
881
882 return VideoModel
883 .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_SCHEDULED_UPDATE ])
884 .findById(id, options)
885 }
886
887 static loadByUUID (uuid: string) {
888 const options = {
889 where: {
890 uuid
891 }
892 }
893
894 return VideoModel
895 .scope([ ScopeNames.WITH_FILES ])
896 .findOne(options)
897 }
898
899 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string, t?: Sequelize.Transaction) {
900 const options = {
901 order: [ [ 'Tags', 'name', 'ASC' ] ],
902 where: {
903 uuid
904 },
905 transaction: t
906 }
907
908 return VideoModel
909 .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_SCHEDULED_UPDATE ])
910 .findOne(options)
911 }
912
913 static async getStats () {
914 const totalLocalVideos = await VideoModel.count({
915 where: {
916 remote: false
917 }
918 })
919 const totalVideos = await VideoModel.count()
920
921 let totalLocalVideoViews = await VideoModel.sum('views', {
922 where: {
923 remote: false
924 }
925 })
926 // Sequelize could return null...
927 if (!totalLocalVideoViews) totalLocalVideoViews = 0
928
929 return {
930 totalLocalVideos,
931 totalLocalVideoViews,
932 totalVideos
933 }
934 }
935
936 private static buildActorWhereWithFilter (filter?: VideoFilter) {
937 if (filter && filter === 'local') {
938 return {
939 serverId: null
940 }
941 }
942
943 return {}
944 }
945
946 private static getCategoryLabel (id: number) {
947 return VIDEO_CATEGORIES[id] || 'Misc'
948 }
949
950 private static getLicenceLabel (id: number) {
951 return VIDEO_LICENCES[id] || 'Unknown'
952 }
953
954 private static getLanguageLabel (id: string) {
955 return VIDEO_LANGUAGES[id] || 'Unknown'
956 }
957
958 private static getPrivacyLabel (id: number) {
959 return VIDEO_PRIVACIES[id] || 'Unknown'
960 }
961
962 private static getStateLabel (id: number) {
963 return VIDEO_STATES[id] || 'Unknown'
964 }
965
966 getOriginalFile () {
967 if (Array.isArray(this.VideoFiles) === false) return undefined
968
969 // The original file is the file that have the higher resolution
970 return maxBy(this.VideoFiles, file => file.resolution)
971 }
972
973 getVideoFilename (videoFile: VideoFileModel) {
974 return this.uuid + '-' + videoFile.resolution + videoFile.extname
975 }
976
977 getThumbnailName () {
978 // We always have a copy of the thumbnail
979 const extension = '.jpg'
980 return this.uuid + extension
981 }
982
983 getPreviewName () {
984 const extension = '.jpg'
985 return this.uuid + extension
986 }
987
988 getTorrentFileName (videoFile: VideoFileModel) {
989 const extension = '.torrent'
990 return this.uuid + '-' + videoFile.resolution + extension
991 }
992
993 isOwned () {
994 return this.remote === false
995 }
996
997 createPreview (videoFile: VideoFileModel) {
998 return generateImageFromVideoFile(
999 this.getVideoFilePath(videoFile),
1000 CONFIG.STORAGE.PREVIEWS_DIR,
1001 this.getPreviewName(),
1002 PREVIEWS_SIZE
1003 )
1004 }
1005
1006 createThumbnail (videoFile: VideoFileModel) {
1007 return generateImageFromVideoFile(
1008 this.getVideoFilePath(videoFile),
1009 CONFIG.STORAGE.THUMBNAILS_DIR,
1010 this.getThumbnailName(),
1011 THUMBNAILS_SIZE
1012 )
1013 }
1014
1015 getTorrentFilePath (videoFile: VideoFileModel) {
1016 return join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1017 }
1018
1019 getVideoFilePath (videoFile: VideoFileModel) {
1020 return join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1021 }
1022
1023 async createTorrentAndSetInfoHash (videoFile: VideoFileModel) {
1024 const options = {
1025 // Keep the extname, it's used by the client to stream the file inside a web browser
1026 name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`,
1027 createdBy: 'PeerTube',
1028 announceList: [
1029 [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
1030 [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
1031 ],
1032 urlList: [
1033 CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1034 ]
1035 }
1036
1037 const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
1038
1039 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1040 logger.info('Creating torrent %s.', filePath)
1041
1042 await writeFilePromise(filePath, torrent)
1043
1044 const parsedTorrent = parseTorrent(torrent)
1045 videoFile.infoHash = parsedTorrent.infoHash
1046 }
1047
1048 getEmbedStaticPath () {
1049 return '/videos/embed/' + this.uuid
1050 }
1051
1052 getThumbnailStaticPath () {
1053 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
1054 }
1055
1056 getPreviewStaticPath () {
1057 return join(STATIC_PATHS.PREVIEWS, this.getPreviewName())
1058 }
1059
1060 toFormattedJSON (options?: {
1061 additionalAttributes: {
1062 state?: boolean,
1063 waitTranscoding?: boolean,
1064 scheduledUpdate?: boolean
1065 }
1066 }): Video {
1067 const formattedAccount = this.VideoChannel.Account.toFormattedJSON()
1068 const formattedVideoChannel = this.VideoChannel.toFormattedJSON()
1069
1070 const videoObject: Video = {
1071 id: this.id,
1072 uuid: this.uuid,
1073 name: this.name,
1074 category: {
1075 id: this.category,
1076 label: VideoModel.getCategoryLabel(this.category)
1077 },
1078 licence: {
1079 id: this.licence,
1080 label: VideoModel.getLicenceLabel(this.licence)
1081 },
1082 language: {
1083 id: this.language,
1084 label: VideoModel.getLanguageLabel(this.language)
1085 },
1086 privacy: {
1087 id: this.privacy,
1088 label: VideoModel.getPrivacyLabel(this.privacy)
1089 },
1090 nsfw: this.nsfw,
1091 description: this.getTruncatedDescription(),
1092 isLocal: this.isOwned(),
1093 duration: this.duration,
1094 views: this.views,
1095 likes: this.likes,
1096 dislikes: this.dislikes,
1097 thumbnailPath: this.getThumbnailStaticPath(),
1098 previewPath: this.getPreviewStaticPath(),
1099 embedPath: this.getEmbedStaticPath(),
1100 createdAt: this.createdAt,
1101 updatedAt: this.updatedAt,
1102 publishedAt: this.publishedAt,
1103 account: {
1104 id: formattedAccount.id,
1105 uuid: formattedAccount.uuid,
1106 name: formattedAccount.name,
1107 displayName: formattedAccount.displayName,
1108 url: formattedAccount.url,
1109 host: formattedAccount.host,
1110 avatar: formattedAccount.avatar
1111 },
1112 channel: {
1113 id: formattedVideoChannel.id,
1114 uuid: formattedVideoChannel.uuid,
1115 name: formattedVideoChannel.name,
1116 displayName: formattedVideoChannel.displayName,
1117 url: formattedVideoChannel.url,
1118 host: formattedVideoChannel.host,
1119 avatar: formattedVideoChannel.avatar
1120 }
1121 }
1122
1123 if (options) {
1124 if (options.additionalAttributes.state === true) {
1125 videoObject.state = {
1126 id: this.state,
1127 label: VideoModel.getStateLabel(this.state)
1128 }
1129 }
1130
1131 if (options.additionalAttributes.waitTranscoding === true) {
1132 videoObject.waitTranscoding = this.waitTranscoding
1133 }
1134
1135 if (options.additionalAttributes.scheduledUpdate === true && this.ScheduleVideoUpdate) {
1136 videoObject.scheduledUpdate = {
1137 updateAt: this.ScheduleVideoUpdate.updateAt,
1138 privacy: this.ScheduleVideoUpdate.privacy || undefined
1139 }
1140 }
1141 }
1142
1143 return videoObject
1144 }
1145
1146 toFormattedDetailsJSON (): VideoDetails {
1147 const formattedJson = this.toFormattedJSON({
1148 additionalAttributes: {
1149 scheduledUpdate: true
1150 }
1151 })
1152
1153 const detailsJson = {
1154 support: this.support,
1155 descriptionPath: this.getDescriptionPath(),
1156 channel: this.VideoChannel.toFormattedJSON(),
1157 account: this.VideoChannel.Account.toFormattedJSON(),
1158 tags: map(this.Tags, 'name'),
1159 commentsEnabled: this.commentsEnabled,
1160 waitTranscoding: this.waitTranscoding,
1161 state: {
1162 id: this.state,
1163 label: VideoModel.getStateLabel(this.state)
1164 },
1165 files: []
1166 }
1167
1168 // Format and sort video files
1169 detailsJson.files = this.getFormattedVideoFilesJSON()
1170
1171 return Object.assign(formattedJson, detailsJson)
1172 }
1173
1174 getFormattedVideoFilesJSON (): VideoFile[] {
1175 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1176
1177 return this.VideoFiles
1178 .map(videoFile => {
1179 let resolutionLabel = videoFile.resolution + 'p'
1180
1181 return {
1182 resolution: {
1183 id: videoFile.resolution,
1184 label: resolutionLabel
1185 },
1186 magnetUri: this.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
1187 size: videoFile.size,
1188 fps: videoFile.fps,
1189 torrentUrl: this.getTorrentUrl(videoFile, baseUrlHttp),
1190 torrentDownloadUrl: this.getTorrentDownloadUrl(videoFile, baseUrlHttp),
1191 fileUrl: this.getVideoFileUrl(videoFile, baseUrlHttp),
1192 fileDownloadUrl: this.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
1193 } as VideoFile
1194 })
1195 .sort((a, b) => {
1196 if (a.resolution.id < b.resolution.id) return 1
1197 if (a.resolution.id === b.resolution.id) return 0
1198 return -1
1199 })
1200 }
1201
1202 toActivityPubObject (): VideoTorrentObject {
1203 const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
1204 if (!this.Tags) this.Tags = []
1205
1206 const tag = this.Tags.map(t => ({
1207 type: 'Hashtag' as 'Hashtag',
1208 name: t.name
1209 }))
1210
1211 let language
1212 if (this.language) {
1213 language = {
1214 identifier: this.language,
1215 name: VideoModel.getLanguageLabel(this.language)
1216 }
1217 }
1218
1219 let category
1220 if (this.category) {
1221 category = {
1222 identifier: this.category + '',
1223 name: VideoModel.getCategoryLabel(this.category)
1224 }
1225 }
1226
1227 let licence
1228 if (this.licence) {
1229 licence = {
1230 identifier: this.licence + '',
1231 name: VideoModel.getLicenceLabel(this.licence)
1232 }
1233 }
1234
1235 const url = []
1236 for (const file of this.VideoFiles) {
1237 url.push({
1238 type: 'Link',
1239 mimeType: VIDEO_EXT_MIMETYPE[file.extname],
1240 href: this.getVideoFileUrl(file, baseUrlHttp),
1241 width: file.resolution,
1242 size: file.size
1243 })
1244
1245 url.push({
1246 type: 'Link',
1247 mimeType: 'application/x-bittorrent',
1248 href: this.getTorrentUrl(file, baseUrlHttp),
1249 width: file.resolution
1250 })
1251
1252 url.push({
1253 type: 'Link',
1254 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
1255 href: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
1256 width: file.resolution
1257 })
1258 }
1259
1260 // Add video url too
1261 url.push({
1262 type: 'Link',
1263 mimeType: 'text/html',
1264 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + this.uuid
1265 })
1266
1267 const subtitleLanguage = []
1268 for (const caption of this.VideoCaptions) {
1269 subtitleLanguage.push({
1270 identifier: caption.language,
1271 name: VideoCaptionModel.getLanguageLabel(caption.language)
1272 })
1273 }
1274
1275 return {
1276 type: 'Video' as 'Video',
1277 id: this.url,
1278 name: this.name,
1279 duration: this.getActivityStreamDuration(),
1280 uuid: this.uuid,
1281 tag,
1282 category,
1283 licence,
1284 language,
1285 views: this.views,
1286 sensitive: this.nsfw,
1287 waitTranscoding: this.waitTranscoding,
1288 state: this.state,
1289 commentsEnabled: this.commentsEnabled,
1290 published: this.publishedAt.toISOString(),
1291 updated: this.updatedAt.toISOString(),
1292 mediaType: 'text/markdown',
1293 content: this.getTruncatedDescription(),
1294 support: this.support,
1295 subtitleLanguage,
1296 icon: {
1297 type: 'Image',
1298 url: this.getThumbnailUrl(baseUrlHttp),
1299 mediaType: 'image/jpeg',
1300 width: THUMBNAILS_SIZE.width,
1301 height: THUMBNAILS_SIZE.height
1302 },
1303 url,
1304 likes: getVideoLikesActivityPubUrl(this),
1305 dislikes: getVideoDislikesActivityPubUrl(this),
1306 shares: getVideoSharesActivityPubUrl(this),
1307 comments: getVideoCommentsActivityPubUrl(this),
1308 attributedTo: [
1309 {
1310 type: 'Person',
1311 id: this.VideoChannel.Account.Actor.url
1312 },
1313 {
1314 type: 'Group',
1315 id: this.VideoChannel.Actor.url
1316 }
1317 ]
1318 }
1319 }
1320
1321 getTruncatedDescription () {
1322 if (!this.description) return null
1323
1324 const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
1325 return peertubeTruncate(this.description, maxLength)
1326 }
1327
1328 async optimizeOriginalVideofile () {
1329 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1330 const newExtname = '.mp4'
1331 const inputVideoFile = this.getOriginalFile()
1332 const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
1333 const videoTranscodedPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
1334
1335 const transcodeOptions = {
1336 inputPath: videoInputPath,
1337 outputPath: videoTranscodedPath
1338 }
1339
1340 // Could be very long!
1341 await transcode(transcodeOptions)
1342
1343 try {
1344 await unlinkPromise(videoInputPath)
1345
1346 // Important to do this before getVideoFilename() to take in account the new file extension
1347 inputVideoFile.set('extname', newExtname)
1348
1349 const videoOutputPath = this.getVideoFilePath(inputVideoFile)
1350 await renamePromise(videoTranscodedPath, videoOutputPath)
1351 const stats = await statPromise(videoOutputPath)
1352 const fps = await getVideoFileFPS(videoOutputPath)
1353
1354 inputVideoFile.set('size', stats.size)
1355 inputVideoFile.set('fps', fps)
1356
1357 await this.createTorrentAndSetInfoHash(inputVideoFile)
1358 await inputVideoFile.save()
1359
1360 } catch (err) {
1361 // Auto destruction...
1362 this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
1363
1364 throw err
1365 }
1366 }
1367
1368 async transcodeOriginalVideofile (resolution: VideoResolution, isPortraitMode: boolean) {
1369 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
1370 const extname = '.mp4'
1371
1372 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
1373 const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
1374
1375 const newVideoFile = new VideoFileModel({
1376 resolution,
1377 extname,
1378 size: 0,
1379 videoId: this.id
1380 })
1381 const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
1382
1383 const transcodeOptions = {
1384 inputPath: videoInputPath,
1385 outputPath: videoOutputPath,
1386 resolution,
1387 isPortraitMode
1388 }
1389
1390 await transcode(transcodeOptions)
1391
1392 const stats = await statPromise(videoOutputPath)
1393 const fps = await getVideoFileFPS(videoOutputPath)
1394
1395 newVideoFile.set('size', stats.size)
1396 newVideoFile.set('fps', fps)
1397
1398 await this.createTorrentAndSetInfoHash(newVideoFile)
1399
1400 await newVideoFile.save()
1401
1402 this.VideoFiles.push(newVideoFile)
1403 }
1404
1405 async importVideoFile (inputFilePath: string) {
1406 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
1407 const { size } = await statPromise(inputFilePath)
1408 const fps = await getVideoFileFPS(inputFilePath)
1409
1410 let updatedVideoFile = new VideoFileModel({
1411 resolution: videoFileResolution,
1412 extname: extname(inputFilePath),
1413 size,
1414 fps,
1415 videoId: this.id
1416 })
1417
1418 const currentVideoFile = this.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
1419
1420 if (currentVideoFile) {
1421 // Remove old file and old torrent
1422 await this.removeFile(currentVideoFile)
1423 await this.removeTorrent(currentVideoFile)
1424 // Remove the old video file from the array
1425 this.VideoFiles = this.VideoFiles.filter(f => f !== currentVideoFile)
1426
1427 // Update the database
1428 currentVideoFile.set('extname', updatedVideoFile.extname)
1429 currentVideoFile.set('size', updatedVideoFile.size)
1430 currentVideoFile.set('fps', updatedVideoFile.fps)
1431
1432 updatedVideoFile = currentVideoFile
1433 }
1434
1435 const outputPath = this.getVideoFilePath(updatedVideoFile)
1436 await copyFilePromise(inputFilePath, outputPath)
1437
1438 await this.createTorrentAndSetInfoHash(updatedVideoFile)
1439
1440 await updatedVideoFile.save()
1441
1442 this.VideoFiles.push(updatedVideoFile)
1443 }
1444
1445 getOriginalFileResolution () {
1446 const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
1447
1448 return getVideoFileResolution(originalFilePath)
1449 }
1450
1451 getDescriptionPath () {
1452 return `/api/${API_VERSION}/videos/${this.uuid}/description`
1453 }
1454
1455 removeThumbnail () {
1456 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
1457 return unlinkPromise(thumbnailPath)
1458 }
1459
1460 removePreview () {
1461 // Same name than video thumbnail
1462 return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
1463 }
1464
1465 removeFile (videoFile: VideoFileModel) {
1466 const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
1467 return unlinkPromise(filePath)
1468 }
1469
1470 removeTorrent (videoFile: VideoFileModel) {
1471 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
1472 return unlinkPromise(torrentPath)
1473 }
1474
1475 getActivityStreamDuration () {
1476 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
1477 return 'PT' + this.duration + 'S'
1478 }
1479
1480 private getBaseUrls () {
1481 let baseUrlHttp
1482 let baseUrlWs
1483
1484 if (this.isOwned()) {
1485 baseUrlHttp = CONFIG.WEBSERVER.URL
1486 baseUrlWs = CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
1487 } else {
1488 baseUrlHttp = REMOTE_SCHEME.HTTP + '://' + this.VideoChannel.Account.Actor.Server.host
1489 baseUrlWs = REMOTE_SCHEME.WS + '://' + this.VideoChannel.Account.Actor.Server.host
1490 }
1491
1492 return { baseUrlHttp, baseUrlWs }
1493 }
1494
1495 private getThumbnailUrl (baseUrlHttp: string) {
1496 return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
1497 }
1498
1499 private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1500 return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1501 }
1502
1503 private getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1504 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
1505 }
1506
1507 private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1508 return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
1509 }
1510
1511 private getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
1512 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile)
1513 }
1514
1515 private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
1516 const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
1517 const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
1518 const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
1519
1520 const magnetHash = {
1521 xs,
1522 announce,
1523 urlList,
1524 infoHash: videoFile.infoHash,
1525 name: this.name
1526 }
1527
1528 return magnetUtil.encode(magnetHash)
1529 }
1530 }