PeerTube/server/core/models/video/video-caption.ts

254 lines
6.1 KiB
TypeScript
Raw Normal View History

import { remove } from 'fs-extra/esm'
2020-12-08 14:30:29 +01:00
import { join } from 'path'
Add Podcast RSS feeds (#5487) * Initial test implementation of Podcast RSS This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option. I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort. * Update to pfeed-podcast 1.2.2 * Initial test implementation of Podcast RSS This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option. I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort. * Update to pfeed-podcast 1.2.2 * Initial test implementation of Podcast RSS This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option. I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort. * Update to pfeed-podcast 1.2.2 * Add correct feed image to RSS channel * Prefer HLS videos for podcast RSS Remove video/stream titles, add optional height attribute to podcast RSS * Prefix podcast RSS images with root server URL * Add optional video query support to include captions * Add transcripts & person images to podcast RSS feed * Prefer webseed/webtorrent files over HLS fragmented mp4s * Experimentally adding podcast fields to basic config page * Add validation for new basic config fields * Don't include "content" in podcast feed, use full description for "description" * Initial test implementation of Podcast RSS This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option. I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort. * Update to pfeed-podcast 1.2.2 * Add correct feed image to RSS channel * Prefer HLS videos for podcast RSS Remove video/stream titles, add optional height attribute to podcast RSS * Prefix podcast RSS images with root server URL * Add optional video query support to include captions * Add transcripts & person images to podcast RSS feed * Prefer webseed/webtorrent files over HLS fragmented mp4s * Experimentally adding podcast fields to basic config page * Add validation for new basic config fields * Don't include "content" in podcast feed, use full description for "description" * Add medium/socialInteract to podcast RSS feeds. Use HTML for description * Change base production image to bullseye, install prosody in image * Add liveItem and trackers to Podcast RSS feeds Remove height from alternateEnclosure, replaced with title. * Clear Podcast RSS feed cache when live streams start/end * Upgrade to Node 16 * Refactor clearCacheRoute to use ApiCache * Remove unnecessary type hint * Update dockerfile to node 16, install python-is-python2 * Use new file paths for captions/playlists * Fix legacy videos in RSS after migration to object storage * Improve method of identifying non-fragmented mp4s in podcast RSS feeds * Don't include fragmented MP4s in podcast RSS feeds * Add experimental support for podcast:categories on the podcast RSS item * Fix undefined category when no videos exist Allows for empty feeds to exist (important for feeds that might only go live) * Add support for podcast:locked -- user has to opt in to show their email * Use comma for podcast:categories delimiter * Make cache clearing async * Fix merge, temporarily test with pfeed-podcast * Syntax changes * Add EXT_MIMETYPE constants for captions * Update & fix tests, fix enclosure mimetypes, remove admin email * Add test for podacst:socialInteract * Add filters hooks for podcast customTags * Remove showdown, updated to pfeed-podcast 6.1.2 * Add 'action:api.live-video.state.updated' hook * Avoid assigning undefined category to podcast feeds * Remove nvmrc * Remove comment * Remove unused podcast config * Remove more unused podcast config * Fix MChannelAccountDefault type hint missed in merge * Remove extra line * Re-add newline in config * Fix lint errors for isEmailPublic * Fix thumbnails in podcast feeds * Requested changes based on review * Provide podcast rss 2.0 only on video channels * Misc cleanup for a less messy PR * Lint fixes * Remove pfeed-podcast * Add peertube version to new hooks * Don't use query include, remove TODO * Remove film medium hack * Clear podcast rss cache before video/channel update hooks * Clear podcast rss cache before video uploaded/deleted hooks * Refactor podcast feed cache clearing * Set correct person name from video channel * Styling * Fix tests --------- Co-authored-by: Chocobozzz <me@florianbigard.com>
2023-05-22 16:00:05 +02:00
import { Op, OrderItem, Transaction } from 'sequelize'
2018-07-12 19:02:00 +02:00
import {
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
2020-01-31 16:56:52 +01:00
CreatedAt,
DataType,
2018-07-12 19:02:00 +02:00
ForeignKey,
Is,
Model,
Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { VideoCaption } from '@peertube/peertube-models'
import {
MVideo,
MVideoCaption,
MVideoCaptionFormattable,
MVideoCaptionLanguageUrl,
MVideoCaptionVideo
} from '@server/types/models/index.js'
import { buildUUID } from '@peertube/peertube-node-utils'
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions.js'
import { logger } from '../../helpers/logger.js'
import { CONFIG } from '../../initializers/config.js'
import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants.js'
import { buildWhereIdOrUUID, throwIfNotValid } from '../shared/index.js'
import { VideoModel } from './video.js'
2018-07-12 19:02:00 +02:00
export enum ScopeNames {
WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
}
2019-04-23 09:50:57 +02:00
@Scopes(() => ({
2018-07-12 19:02:00 +02:00
[ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: {
include: [
{
2019-08-15 11:53:26 +02:00
attributes: [ 'id', 'uuid', 'remote' ],
2019-04-23 09:50:57 +02:00
model: VideoModel.unscoped(),
2018-07-12 19:02:00 +02:00
required: true
}
]
}
2019-04-23 09:50:57 +02:00
}))
2018-07-12 19:02:00 +02:00
@Table({
tableName: 'videoCaption',
indexes: [
2021-02-15 14:08:16 +01:00
{
fields: [ 'filename' ],
unique: true
},
2018-07-12 19:02:00 +02:00
{
fields: [ 'videoId' ]
},
{
fields: [ 'videoId', 'language' ],
unique: true
}
]
})
2021-05-12 14:09:04 +02:00
export class VideoCaptionModel extends Model<Partial<AttributesOnly<VideoCaptionModel>>> {
2018-07-12 19:02:00 +02:00
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
@Column
language: string
2021-02-15 14:08:16 +01:00
@AllowNull(false)
@Column
filename: string
@AllowNull(true)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
fileUrl: string
2018-07-12 19:02:00 +02:00
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
2018-07-12 19:02:00 +02:00
@BeforeDestroy
2021-06-15 09:17:19 +02:00
static async removeFiles (instance: VideoCaptionModel, options) {
2018-07-16 14:22:16 +02:00
if (!instance.Video) {
2021-06-15 09:17:19 +02:00
instance.Video = await instance.$get('Video', { transaction: options.transaction })
2018-07-16 14:22:16 +02:00
}
2018-07-12 19:02:00 +02:00
if (instance.isOwned()) {
2021-02-15 14:08:16 +01:00
logger.info('Removing caption %s.', instance.filename)
2018-07-16 14:22:16 +02:00
try {
await instance.removeCaptionFile()
} catch (err) {
2021-02-15 14:08:16 +01:00
logger.error('Cannot remove caption file %s.', instance.filename)
2018-07-16 14:22:16 +02:00
}
2018-07-12 19:02:00 +02:00
}
return undefined
}
2021-06-09 16:22:01 +02:00
static loadByVideoIdAndLanguage (videoId: string | number, language: string, transaction?: Transaction): Promise<MVideoCaptionVideo> {
2018-07-12 19:02:00 +02:00
const videoInclude = {
model: VideoModel.unscoped(),
attributes: [ 'id', 'remote', 'uuid' ],
2021-06-15 09:17:19 +02:00
where: buildWhereIdOrUUID(videoId)
2018-07-12 19:02:00 +02:00
}
const query = {
where: {
language
},
include: [
videoInclude
2021-06-15 09:17:19 +02:00
],
transaction
2018-07-12 19:02:00 +02:00
}
return VideoCaptionModel.findOne(query)
}
2021-02-15 14:08:16 +01:00
static loadWithVideoByFilename (filename: string): Promise<MVideoCaptionVideo> {
const query = {
where: {
filename
},
include: [
{
model: VideoModel.unscoped(),
attributes: [ 'id', 'remote', 'uuid' ]
}
]
2018-07-12 19:02:00 +02:00
}
2021-02-15 14:08:16 +01:00
return VideoCaptionModel.findOne(query)
}
static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) {
2021-06-09 16:22:01 +02:00
const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language, transaction)
2021-02-15 14:08:16 +01:00
// Delete existing file
if (existing) await existing.destroy({ transaction })
return caption.save({ transaction })
2018-07-12 19:02:00 +02:00
}
2021-06-09 17:06:56 +02:00
static listVideoCaptions (videoId: number, transaction?: Transaction): Promise<MVideoCaptionVideo[]> {
2018-07-12 19:02:00 +02:00
const query = {
2019-04-18 11:28:17 +02:00
order: [ [ 'language', 'ASC' ] ] as OrderItem[],
2018-07-12 19:02:00 +02:00
where: {
videoId
2021-06-09 16:22:01 +02:00
},
transaction
2018-07-12 19:02:00 +02:00
}
return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
}
Add Podcast RSS feeds (#5487) * Initial test implementation of Podcast RSS This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option. I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort. * Update to pfeed-podcast 1.2.2 * Initial test implementation of Podcast RSS This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option. I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort. * Update to pfeed-podcast 1.2.2 * Initial test implementation of Podcast RSS This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option. I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort. * Update to pfeed-podcast 1.2.2 * Add correct feed image to RSS channel * Prefer HLS videos for podcast RSS Remove video/stream titles, add optional height attribute to podcast RSS * Prefix podcast RSS images with root server URL * Add optional video query support to include captions * Add transcripts & person images to podcast RSS feed * Prefer webseed/webtorrent files over HLS fragmented mp4s * Experimentally adding podcast fields to basic config page * Add validation for new basic config fields * Don't include "content" in podcast feed, use full description for "description" * Initial test implementation of Podcast RSS This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option. I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort. * Update to pfeed-podcast 1.2.2 * Add correct feed image to RSS channel * Prefer HLS videos for podcast RSS Remove video/stream titles, add optional height attribute to podcast RSS * Prefix podcast RSS images with root server URL * Add optional video query support to include captions * Add transcripts & person images to podcast RSS feed * Prefer webseed/webtorrent files over HLS fragmented mp4s * Experimentally adding podcast fields to basic config page * Add validation for new basic config fields * Don't include "content" in podcast feed, use full description for "description" * Add medium/socialInteract to podcast RSS feeds. Use HTML for description * Change base production image to bullseye, install prosody in image * Add liveItem and trackers to Podcast RSS feeds Remove height from alternateEnclosure, replaced with title. * Clear Podcast RSS feed cache when live streams start/end * Upgrade to Node 16 * Refactor clearCacheRoute to use ApiCache * Remove unnecessary type hint * Update dockerfile to node 16, install python-is-python2 * Use new file paths for captions/playlists * Fix legacy videos in RSS after migration to object storage * Improve method of identifying non-fragmented mp4s in podcast RSS feeds * Don't include fragmented MP4s in podcast RSS feeds * Add experimental support for podcast:categories on the podcast RSS item * Fix undefined category when no videos exist Allows for empty feeds to exist (important for feeds that might only go live) * Add support for podcast:locked -- user has to opt in to show their email * Use comma for podcast:categories delimiter * Make cache clearing async * Fix merge, temporarily test with pfeed-podcast * Syntax changes * Add EXT_MIMETYPE constants for captions * Update & fix tests, fix enclosure mimetypes, remove admin email * Add test for podacst:socialInteract * Add filters hooks for podcast customTags * Remove showdown, updated to pfeed-podcast 6.1.2 * Add 'action:api.live-video.state.updated' hook * Avoid assigning undefined category to podcast feeds * Remove nvmrc * Remove comment * Remove unused podcast config * Remove more unused podcast config * Fix MChannelAccountDefault type hint missed in merge * Remove extra line * Re-add newline in config * Fix lint errors for isEmailPublic * Fix thumbnails in podcast feeds * Requested changes based on review * Provide podcast rss 2.0 only on video channels * Misc cleanup for a less messy PR * Lint fixes * Remove pfeed-podcast * Add peertube version to new hooks * Don't use query include, remove TODO * Remove film medium hack * Clear podcast rss cache before video/channel update hooks * Clear podcast rss cache before video uploaded/deleted hooks * Refactor podcast feed cache clearing * Set correct person name from video channel * Styling * Fix tests --------- Co-authored-by: Chocobozzz <me@florianbigard.com>
2023-05-22 16:00:05 +02:00
static async listCaptionsOfMultipleVideos (videoIds: number[], transaction?: Transaction) {
const query = {
order: [ [ 'language', 'ASC' ] ] as OrderItem[],
where: {
videoId: {
[Op.in]: videoIds
}
},
transaction
}
const captions = await VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll<MVideoCaptionVideo>(query)
const result: { [ id: number ]: MVideoCaptionVideo[] } = {}
for (const id of videoIds) {
result[id] = []
}
for (const caption of captions) {
result[caption.videoId].push(caption)
}
return result
}
2018-07-12 19:02:00 +02:00
static getLanguageLabel (language: string) {
return VIDEO_LANGUAGES[language] || 'Unknown'
}
2019-04-18 11:28:17 +02:00
static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Transaction) {
2018-07-12 19:02:00 +02:00
const query = {
where: {
videoId
},
transaction
}
return VideoCaptionModel.destroy(query)
}
2021-02-15 14:08:16 +01:00
static generateCaptionName (language: string) {
return `${buildUUID()}-${language}.vtt`
2021-02-15 14:08:16 +01:00
}
2018-07-12 19:02:00 +02:00
isOwned () {
return this.Video.remote === false
}
2019-08-20 19:05:31 +02:00
toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
2018-07-12 19:02:00 +02:00
return {
language: {
id: this.language,
label: VideoCaptionModel.getLanguageLabel(this.language)
},
captionPath: this.getCaptionStaticPath(),
updatedAt: this.updatedAt.toISOString()
2018-07-12 19:02:00 +02:00
}
}
2023-06-01 14:51:16 +02:00
getCaptionStaticPath (this: MVideoCaptionLanguageUrl) {
2021-02-15 14:08:16 +01:00
return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename)
2018-07-12 19:02:00 +02:00
}
2021-02-15 14:08:16 +01:00
removeCaptionFile (this: MVideoCaption) {
return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.filename)
2018-07-12 19:02:00 +02:00
}
2023-06-01 14:51:16 +02:00
getFileUrl (this: MVideoCaptionLanguageUrl, video: MVideo) {
if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath()
2021-02-18 10:15:11 +01:00
return this.fileUrl
}
2021-06-09 16:22:01 +02:00
isEqual (this: MVideoCaption, other: MVideoCaption) {
if (this.fileUrl) return this.fileUrl === other.fileUrl
return this.filename === other.filename
}
2018-07-12 19:02:00 +02:00
}