Optimize video thumbnail generation
Process images in worker threads Reduce ffmpeg calls
|
@ -1,3 +1,4 @@
|
||||||
|
import { FfprobeData } from 'fluent-ffmpeg'
|
||||||
import { FFmpegCommandWrapper, FFmpegCommandWrapperOptions } from './ffmpeg-command-wrapper.js'
|
import { FFmpegCommandWrapper, FFmpegCommandWrapperOptions } from './ffmpeg-command-wrapper.js'
|
||||||
import { getVideoStreamDuration } from './ffprobe.js'
|
import { getVideoStreamDuration } from './ffprobe.js'
|
||||||
|
|
||||||
|
@ -38,10 +39,11 @@ export class FFmpegImage {
|
||||||
async generateThumbnailFromVideo (options: {
|
async generateThumbnailFromVideo (options: {
|
||||||
fromPath: string
|
fromPath: string
|
||||||
output: string
|
output: string
|
||||||
|
ffprobe?: FfprobeData
|
||||||
}) {
|
}) {
|
||||||
const { fromPath, output } = options
|
const { fromPath, output, ffprobe } = options
|
||||||
|
|
||||||
let duration = await getVideoStreamDuration(fromPath)
|
let duration = await getVideoStreamDuration(fromPath, ffprobe)
|
||||||
if (isNaN(duration)) duration = 0
|
if (isNaN(duration)) duration = 0
|
||||||
|
|
||||||
this.commandWrapper.buildCommand(fromPath)
|
this.commandWrapper.buildCommand(fromPath)
|
||||||
|
|
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 4.9 KiB |
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 4.9 KiB |
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 4.9 KiB |
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 4.9 KiB |
Before Width: | Height: | Size: 6.1 KiB After Width: | Height: | Size: 6.2 KiB |
Before Width: | Height: | Size: 6.5 KiB After Width: | Height: | Size: 6.5 KiB |
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.5 KiB |
|
@ -652,7 +652,7 @@ describe('Test video playlists', function () {
|
||||||
let video3: string
|
let video3: string
|
||||||
|
|
||||||
before(async function () {
|
before(async function () {
|
||||||
this.timeout(60000)
|
this.timeout(120000)
|
||||||
|
|
||||||
groupUser1 = [ Object.assign({}, servers[0], { accessToken: userTokenServer1 }) ]
|
groupUser1 = [ Object.assign({}, servers[0], { accessToken: userTokenServer1 }) ]
|
||||||
groupWithoutToken1 = [ Object.assign({}, servers[0], { accessToken: undefined }) ]
|
groupWithoutToken1 = [ Object.assign({}, servers[0], { accessToken: undefined }) ]
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import express from 'express'
|
import express, { UploadFiles } from 'express'
|
||||||
import { move } from 'fs-extra/esm'
|
import { move } from 'fs-extra/esm'
|
||||||
import { basename } from 'path'
|
import { basename } from 'path'
|
||||||
import { getResumableUploadPath } from '@server/helpers/upload.js'
|
import { getResumableUploadPath } from '@server/helpers/upload.js'
|
||||||
|
@ -13,9 +13,9 @@ import { buildNextVideoState } from '@server/lib/video-state.js'
|
||||||
import { openapiOperationDoc } from '@server/middlewares/doc.js'
|
import { openapiOperationDoc } from '@server/middlewares/doc.js'
|
||||||
import { VideoPasswordModel } from '@server/models/video/video-password.js'
|
import { VideoPasswordModel } from '@server/models/video/video-password.js'
|
||||||
import { VideoSourceModel } from '@server/models/video/video-source.js'
|
import { VideoSourceModel } from '@server/models/video/video-source.js'
|
||||||
import { MVideoFile, MVideoFullLight } from '@server/types/models/index.js'
|
import { MVideoFile, MVideoFullLight, MVideoThumbnail } from '@server/types/models/index.js'
|
||||||
import { uuidToShort } from '@peertube/peertube-node-utils'
|
import { uuidToShort } from '@peertube/peertube-node-utils'
|
||||||
import { HttpStatusCode, VideoCreate, VideoPrivacy, VideoState } from '@peertube/peertube-models'
|
import { HttpStatusCode, ThumbnailType, VideoCreate, VideoPrivacy, VideoState } from '@peertube/peertube-models'
|
||||||
import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger.js'
|
import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger.js'
|
||||||
import { createReqFiles } from '../../../helpers/express-utils.js'
|
import { createReqFiles } from '../../../helpers/express-utils.js'
|
||||||
import { logger, loggerTagsFactory } from '../../../helpers/logger.js'
|
import { logger, loggerTagsFactory } from '../../../helpers/logger.js'
|
||||||
|
@ -34,8 +34,9 @@ import {
|
||||||
} from '../../../middlewares/index.js'
|
} from '../../../middlewares/index.js'
|
||||||
import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update.js'
|
import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update.js'
|
||||||
import { VideoModel } from '../../../models/video/video.js'
|
import { VideoModel } from '../../../models/video/video.js'
|
||||||
import { getChaptersFromContainer } from '@peertube/peertube-ffmpeg'
|
import { ffprobePromise, getChaptersFromContainer } from '@peertube/peertube-ffmpeg'
|
||||||
import { replaceChapters, replaceChaptersFromDescriptionIfNeeded } from '@server/lib/video-chapters.js'
|
import { replaceChapters, replaceChaptersFromDescriptionIfNeeded } from '@server/lib/video-chapters.js'
|
||||||
|
import { FfprobeData } from 'fluent-ffmpeg'
|
||||||
|
|
||||||
const lTags = loggerTagsFactory('api', 'video')
|
const lTags = loggerTagsFactory('api', 'video')
|
||||||
const auditLogger = auditLoggerFactory('videos')
|
const auditLogger = auditLoggerFactory('videos')
|
||||||
|
@ -142,12 +143,15 @@ async function addVideo (options: {
|
||||||
video.VideoChannel = videoChannel
|
video.VideoChannel = videoChannel
|
||||||
video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
|
video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
|
||||||
|
|
||||||
const videoFile = await buildNewFile({ path: videoPhysicalFile.path, mode: 'web-video' })
|
const ffprobe = await ffprobePromise(videoPhysicalFile.path)
|
||||||
|
|
||||||
|
const videoFile = await buildNewFile({ path: videoPhysicalFile.path, mode: 'web-video', ffprobe })
|
||||||
const originalFilename = videoPhysicalFile.originalname
|
const originalFilename = videoPhysicalFile.originalname
|
||||||
|
|
||||||
const containerChapters = await getChaptersFromContainer({
|
const containerChapters = await getChaptersFromContainer({
|
||||||
path: videoPhysicalFile.path,
|
path: videoPhysicalFile.path,
|
||||||
maxTitleLength: CONSTRAINTS_FIELDS.VIDEO_CHAPTERS.TITLE.max
|
maxTitleLength: CONSTRAINTS_FIELDS.VIDEO_CHAPTERS.TITLE.max,
|
||||||
|
ffprobe
|
||||||
})
|
})
|
||||||
logger.debug(`Got ${containerChapters.length} chapters from video "${video.name}" container`, { containerChapters, ...lTags(video.uuid) })
|
logger.debug(`Got ${containerChapters.length} chapters from video "${video.name}" container`, { containerChapters, ...lTags(video.uuid) })
|
||||||
|
|
||||||
|
@ -158,19 +162,16 @@ async function addVideo (options: {
|
||||||
videoPhysicalFile.filename = basename(destination)
|
videoPhysicalFile.filename = basename(destination)
|
||||||
videoPhysicalFile.path = destination
|
videoPhysicalFile.path = destination
|
||||||
|
|
||||||
const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({
|
const thumbnails = await createThumbnailFiles({ video, files, videoFile, ffprobe })
|
||||||
video,
|
|
||||||
files,
|
|
||||||
fallback: type => generateLocalVideoMiniature({ video, videoFile, type })
|
|
||||||
})
|
|
||||||
|
|
||||||
const { videoCreated } = await sequelizeTypescript.transaction(async t => {
|
const { videoCreated } = await sequelizeTypescript.transaction(async t => {
|
||||||
const sequelizeOptions = { transaction: t }
|
const sequelizeOptions = { transaction: t }
|
||||||
|
|
||||||
const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
|
const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight
|
||||||
|
|
||||||
await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
|
for (const thumbnail of thumbnails) {
|
||||||
await videoCreated.addAndSaveThumbnail(previewModel, t)
|
await videoCreated.addAndSaveThumbnail(thumbnail, t)
|
||||||
|
}
|
||||||
|
|
||||||
// Do not forget to add video channel information to the created video
|
// Do not forget to add video channel information to the created video
|
||||||
videoCreated.VideoChannel = res.locals.videoChannel
|
videoCreated.VideoChannel = res.locals.videoChannel
|
||||||
|
@ -297,3 +298,27 @@ async function deleteUploadResumableCache (req: express.Request, res: express.Re
|
||||||
|
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function createThumbnailFiles (options: {
|
||||||
|
video: MVideoThumbnail
|
||||||
|
files: UploadFiles
|
||||||
|
videoFile: MVideoFile
|
||||||
|
ffprobe?: FfprobeData
|
||||||
|
}) {
|
||||||
|
const { video, videoFile, files, ffprobe } = options
|
||||||
|
|
||||||
|
const models = await buildVideoThumbnailsFromReq({
|
||||||
|
video,
|
||||||
|
files,
|
||||||
|
fallback: () => Promise.resolve(undefined)
|
||||||
|
})
|
||||||
|
|
||||||
|
const filteredModels = models.filter(m => !!m)
|
||||||
|
|
||||||
|
const thumbnailsToGenerate = [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ].filter(type => {
|
||||||
|
// Generate missing thumbnail types
|
||||||
|
return !filteredModels.some(m => m.type === type)
|
||||||
|
})
|
||||||
|
|
||||||
|
return [ ...filteredModels, ...await generateLocalVideoMiniature({ video, videoFile, types: thumbnailsToGenerate, ffprobe }) ]
|
||||||
|
}
|
||||||
|
|
|
@ -1,20 +1,17 @@
|
||||||
import { copy, remove } from 'fs-extra/esm'
|
import { copy, remove } from 'fs-extra/esm'
|
||||||
import { readFile, rename } from 'fs/promises'
|
import { readFile, rename } from 'fs/promises'
|
||||||
import { join } from 'path'
|
|
||||||
import { ColorActionName } from '@jimp/plugin-color'
|
import { ColorActionName } from '@jimp/plugin-color'
|
||||||
import { buildUUID, getLowercaseExtension } from '@peertube/peertube-node-utils'
|
import { buildUUID, getLowercaseExtension } from '@peertube/peertube-node-utils'
|
||||||
import { convertWebPToJPG, generateThumbnailFromVideo, processGIF } from './ffmpeg/index.js'
|
import { convertWebPToJPG, processGIF } from './ffmpeg/index.js'
|
||||||
import { logger, loggerTagsFactory } from './logger.js'
|
import { logger } from './logger.js'
|
||||||
|
|
||||||
import type Jimp from 'jimp'
|
import type Jimp from 'jimp'
|
||||||
|
|
||||||
const lTags = loggerTagsFactory('image-utils')
|
export function generateImageFilename (extension = '.jpg') {
|
||||||
|
|
||||||
function generateImageFilename (extension = '.jpg') {
|
|
||||||
return buildUUID() + extension
|
return buildUUID() + extension
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processImage (options: {
|
export async function processImage (options: {
|
||||||
path: string
|
path: string
|
||||||
destination: string
|
destination: string
|
||||||
newSize: { width: number, height: number }
|
newSize: { width: number, height: number }
|
||||||
|
@ -38,38 +35,11 @@ async function processImage (options: {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keepOriginal !== true) await remove(path)
|
if (keepOriginal !== true) await remove(path)
|
||||||
|
|
||||||
|
logger.debug('Finished processing image %s to %s.', path, destination)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generateImageFromVideoFile (options: {
|
export async function getImageSize (path: string) {
|
||||||
fromPath: string
|
|
||||||
folder: string
|
|
||||||
imageName: string
|
|
||||||
size: { width: number, height: number }
|
|
||||||
}) {
|
|
||||||
const { fromPath, folder, imageName, size } = options
|
|
||||||
|
|
||||||
const pendingImageName = 'pending-' + imageName
|
|
||||||
const pendingImagePath = join(folder, pendingImageName)
|
|
||||||
|
|
||||||
try {
|
|
||||||
await generateThumbnailFromVideo({ fromPath, output: pendingImagePath })
|
|
||||||
|
|
||||||
const destination = join(folder, imageName)
|
|
||||||
await processImage({ path: pendingImagePath, destination, newSize: size })
|
|
||||||
} catch (err) {
|
|
||||||
logger.error('Cannot generate image from video %s.', fromPath, { err, ...lTags() })
|
|
||||||
|
|
||||||
try {
|
|
||||||
await remove(pendingImagePath)
|
|
||||||
} catch (err) {
|
|
||||||
logger.debug('Cannot remove pending image path after generation error.', { err, ...lTags() })
|
|
||||||
}
|
|
||||||
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getImageSize (path: string) {
|
|
||||||
const inputBuffer = await readFile(path)
|
const inputBuffer = await readFile(path)
|
||||||
|
|
||||||
const Jimp = await import('jimp')
|
const Jimp = await import('jimp')
|
||||||
|
@ -83,16 +53,7 @@ async function getImageSize (path: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
// Private
|
||||||
export {
|
|
||||||
generateImageFilename,
|
|
||||||
generateImageFromVideoFile,
|
|
||||||
|
|
||||||
processImage,
|
|
||||||
|
|
||||||
getImageSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) {
|
async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) {
|
||||||
|
|
|
@ -971,6 +971,10 @@ const WORKER_THREADS = {
|
||||||
PROCESS_IMAGE: {
|
PROCESS_IMAGE: {
|
||||||
CONCURRENCY: 1,
|
CONCURRENCY: 1,
|
||||||
MAX_THREADS: 5
|
MAX_THREADS: 5
|
||||||
|
},
|
||||||
|
GET_IMAGE_SIZE: {
|
||||||
|
CONCURRENCY: 1,
|
||||||
|
MAX_THREADS: 5
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { Job } from 'bullmq'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
|
import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
|
||||||
import { getFFmpegCommandWrapperOptions } from '@server/helpers/ffmpeg/index.js'
|
import { getFFmpegCommandWrapperOptions } from '@server/helpers/ffmpeg/index.js'
|
||||||
import { generateImageFilename, getImageSize } from '@server/helpers/image-utils.js'
|
import { generateImageFilename } from '@server/helpers/image-utils.js'
|
||||||
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
||||||
import { deleteFileAndCatch } from '@server/helpers/utils.js'
|
import { deleteFileAndCatch } from '@server/helpers/utils.js'
|
||||||
import { CONFIG } from '@server/initializers/config.js'
|
import { CONFIG } from '@server/initializers/config.js'
|
||||||
|
@ -15,6 +15,7 @@ import { VideoModel } from '@server/models/video/video.js'
|
||||||
import { MVideo } from '@server/types/models/index.js'
|
import { MVideo } from '@server/types/models/index.js'
|
||||||
import { FFmpegImage, isAudioFile } from '@peertube/peertube-ffmpeg'
|
import { FFmpegImage, isAudioFile } from '@peertube/peertube-ffmpeg'
|
||||||
import { GenerateStoryboardPayload } from '@peertube/peertube-models'
|
import { GenerateStoryboardPayload } from '@peertube/peertube-models'
|
||||||
|
import { getImageSizeFromWorker } from '@server/lib/worker/parent-process.js'
|
||||||
|
|
||||||
const lTagsBase = loggerTagsFactory('storyboard')
|
const lTagsBase = loggerTagsFactory('storyboard')
|
||||||
|
|
||||||
|
@ -76,7 +77,7 @@ async function processGenerateStoryboard (job: Job): Promise<void> {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const imageSize = await getImageSize(destination)
|
const imageSize = await getImageSizeFromWorker(destination)
|
||||||
|
|
||||||
await retryTransactionWrapper(() => {
|
await retryTransactionWrapper(() => {
|
||||||
return sequelizeTypescript.transaction(async transaction => {
|
return sequelizeTypescript.transaction(async transaction => {
|
||||||
|
|
|
@ -26,7 +26,6 @@ import { isAbleToUploadVideo } from '@server/lib/user.js'
|
||||||
import { VideoPathManager } from '@server/lib/video-path-manager.js'
|
import { VideoPathManager } from '@server/lib/video-path-manager.js'
|
||||||
import { buildNextVideoState } from '@server/lib/video-state.js'
|
import { buildNextVideoState } from '@server/lib/video-state.js'
|
||||||
import { buildMoveToObjectStorageJob } from '@server/lib/video.js'
|
import { buildMoveToObjectStorageJob } from '@server/lib/video.js'
|
||||||
import { ThumbnailModel } from '@server/models/video/thumbnail.js'
|
|
||||||
import { MUserId, MVideoFile, MVideoFullLight } from '@server/types/models/index.js'
|
import { MUserId, MVideoFile, MVideoFullLight } from '@server/types/models/index.js'
|
||||||
import { MVideoImport, MVideoImportDefault, MVideoImportDefaultFiles, MVideoImportVideo } from '@server/types/models/video/video-import.js'
|
import { MVideoImport, MVideoImportDefault, MVideoImportDefaultFiles, MVideoImportVideo } from '@server/types/models/video/video-import.js'
|
||||||
import { getLowercaseExtension } from '@peertube/peertube-node-utils'
|
import { getLowercaseExtension } from '@peertube/peertube-node-utils'
|
||||||
|
@ -51,6 +50,7 @@ import { Notifier } from '../../notifier/index.js'
|
||||||
import { generateLocalVideoMiniature } from '../../thumbnail.js'
|
import { generateLocalVideoMiniature } from '../../thumbnail.js'
|
||||||
import { JobQueue } from '../job-queue.js'
|
import { JobQueue } from '../job-queue.js'
|
||||||
import { replaceChaptersIfNotExist } from '@server/lib/video-chapters.js'
|
import { replaceChaptersIfNotExist } from '@server/lib/video-chapters.js'
|
||||||
|
import { FfprobeData } from 'fluent-ffmpeg'
|
||||||
|
|
||||||
async function processVideoImport (job: Job): Promise<VideoImportPreventExceptionResult> {
|
async function processVideoImport (job: Job): Promise<VideoImportPreventExceptionResult> {
|
||||||
const payload = job.data as VideoImportPayload
|
const payload = job.data as VideoImportPayload
|
||||||
|
@ -205,21 +205,11 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
|
||||||
|
|
||||||
tempVideoPath = null // This path is not used anymore
|
tempVideoPath = null // This path is not used anymore
|
||||||
|
|
||||||
let {
|
const thumbnails = await generateMiniature({ videoImportWithFiles, videoFile, ffprobe })
|
||||||
miniatureModel: thumbnailModel,
|
|
||||||
miniatureJSONSave: thumbnailSave
|
|
||||||
} = await generateMiniature(videoImportWithFiles, videoFile, ThumbnailType.MINIATURE)
|
|
||||||
|
|
||||||
let {
|
|
||||||
miniatureModel: previewModel,
|
|
||||||
miniatureJSONSave: previewSave
|
|
||||||
} = await generateMiniature(videoImportWithFiles, videoFile, ThumbnailType.PREVIEW)
|
|
||||||
|
|
||||||
// Create torrent
|
// Create torrent
|
||||||
await createTorrentAndSetInfoHash(videoImportWithFiles.Video, videoFile)
|
await createTorrentAndSetInfoHash(videoImportWithFiles.Video, videoFile)
|
||||||
|
|
||||||
const videoFileSave = videoFile.toJSON()
|
|
||||||
|
|
||||||
const { videoImportUpdated, video } = await retryTransactionWrapper(() => {
|
const { videoImportUpdated, video } = await retryTransactionWrapper(() => {
|
||||||
return sequelizeTypescript.transaction(async t => {
|
return sequelizeTypescript.transaction(async t => {
|
||||||
// Refresh video
|
// Refresh video
|
||||||
|
@ -233,8 +223,9 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
|
||||||
video.state = buildNextVideoState(video.state)
|
video.state = buildNextVideoState(video.state)
|
||||||
await video.save({ transaction: t })
|
await video.save({ transaction: t })
|
||||||
|
|
||||||
if (thumbnailModel) await video.addAndSaveThumbnail(thumbnailModel, t)
|
for (const thumbnail of thumbnails) {
|
||||||
if (previewModel) await video.addAndSaveThumbnail(previewModel, t)
|
await video.addAndSaveThumbnail(thumbnail, t)
|
||||||
|
}
|
||||||
|
|
||||||
await replaceChaptersIfNotExist({ video, chapters: containerChapters, transaction: t })
|
await replaceChaptersIfNotExist({ video, chapters: containerChapters, transaction: t })
|
||||||
|
|
||||||
|
@ -249,14 +240,6 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
|
||||||
logger.info('Video %s imported.', video.uuid)
|
logger.info('Video %s imported.', video.uuid)
|
||||||
|
|
||||||
return { videoImportUpdated, video: videoForFederation }
|
return { videoImportUpdated, video: videoForFederation }
|
||||||
}).catch(err => {
|
|
||||||
// Reset fields
|
|
||||||
if (thumbnailModel) thumbnailModel = new ThumbnailModel(thumbnailSave)
|
|
||||||
if (previewModel) previewModel = new ThumbnailModel(previewSave)
|
|
||||||
|
|
||||||
videoFile = new VideoFileModel(videoFileSave)
|
|
||||||
|
|
||||||
throw err
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -279,34 +262,29 @@ async function refreshVideoImportFromDB (videoImport: MVideoImportDefault, video
|
||||||
return Object.assign(videoImport, { Video: videoWithFiles })
|
return Object.assign(videoImport, { Video: videoWithFiles })
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generateMiniature (
|
async function generateMiniature (options: {
|
||||||
videoImportWithFiles: MVideoImportDefaultFiles,
|
videoImportWithFiles: MVideoImportDefaultFiles
|
||||||
videoFile: MVideoFile,
|
videoFile: MVideoFile
|
||||||
thumbnailType: ThumbnailType_Type
|
ffprobe: FfprobeData
|
||||||
) {
|
}) {
|
||||||
// Generate miniature if the import did not created it
|
const { ffprobe, videoFile, videoImportWithFiles } = options
|
||||||
const needsMiniature = thumbnailType === ThumbnailType.MINIATURE
|
|
||||||
? !videoImportWithFiles.Video.getMiniature()
|
|
||||||
: !videoImportWithFiles.Video.getPreview()
|
|
||||||
|
|
||||||
if (!needsMiniature) {
|
const thumbnailsToGenerate: ThumbnailType_Type[] = []
|
||||||
return {
|
|
||||||
miniatureModel: null,
|
if (!videoImportWithFiles.Video.getMiniature()) {
|
||||||
miniatureJSONSave: null
|
thumbnailsToGenerate.push(ThumbnailType.MINIATURE)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const miniatureModel = await generateLocalVideoMiniature({
|
if (!videoImportWithFiles.Video.getPreview()) {
|
||||||
|
thumbnailsToGenerate.push(ThumbnailType.PREVIEW)
|
||||||
|
}
|
||||||
|
|
||||||
|
return generateLocalVideoMiniature({
|
||||||
video: videoImportWithFiles.Video,
|
video: videoImportWithFiles.Video,
|
||||||
videoFile,
|
videoFile,
|
||||||
type: thumbnailType
|
types: thumbnailsToGenerate,
|
||||||
|
ffprobe
|
||||||
})
|
})
|
||||||
const miniatureJSONSave = miniatureModel.toJSON()
|
|
||||||
|
|
||||||
return {
|
|
||||||
miniatureModel,
|
|
||||||
miniatureJSONSave
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function afterImportSuccess (options: {
|
async function afterImportSuccess (options: {
|
||||||
|
|
|
@ -155,9 +155,14 @@ async function saveReplayToExternalVideo (options: {
|
||||||
inputFileMutexReleaser()
|
inputFileMutexReleaser()
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) {
|
const thumbnails = await generateLocalVideoMiniature({
|
||||||
const image = await generateLocalVideoMiniature({ video: replayVideo, videoFile: replayVideo.getMaxQualityFile(), type })
|
video: replayVideo,
|
||||||
await replayVideo.addAndSaveThumbnail(image)
|
videoFile: replayVideo.getMaxQualityFile(),
|
||||||
|
types: [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const thumbnail of thumbnails) {
|
||||||
|
await replayVideo.addAndSaveThumbnail(thumbnail)
|
||||||
}
|
}
|
||||||
|
|
||||||
await moveToNextState({ video: replayVideo, isNewVideo: true })
|
await moveToNextState({ video: replayVideo, isNewVideo: true })
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import { ThumbnailType, ThumbnailType_Type } from '@peertube/peertube-models'
|
import { ThumbnailType, ThumbnailType_Type } from '@peertube/peertube-models'
|
||||||
import { generateImageFilename, generateImageFromVideoFile } from '../helpers/image-utils.js'
|
import { generateImageFilename } from '../helpers/image-utils.js'
|
||||||
import { CONFIG } from '../initializers/config.js'
|
import { CONFIG } from '../initializers/config.js'
|
||||||
import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants.js'
|
import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants.js'
|
||||||
import { ThumbnailModel } from '../models/video/thumbnail.js'
|
import { ThumbnailModel } from '../models/video/thumbnail.js'
|
||||||
|
@ -9,6 +9,13 @@ import { MThumbnail } from '../types/models/video/thumbnail.js'
|
||||||
import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist.js'
|
import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist.js'
|
||||||
import { VideoPathManager } from './video-path-manager.js'
|
import { VideoPathManager } from './video-path-manager.js'
|
||||||
import { downloadImageFromWorker, processImageFromWorker } from './worker/parent-process.js'
|
import { downloadImageFromWorker, processImageFromWorker } from './worker/parent-process.js'
|
||||||
|
import { generateThumbnailFromVideo } from '@server/helpers/ffmpeg/ffmpeg-image.js'
|
||||||
|
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
||||||
|
import { remove } from 'fs-extra'
|
||||||
|
import { FfprobeData } from 'fluent-ffmpeg'
|
||||||
|
import Bluebird from 'bluebird'
|
||||||
|
|
||||||
|
const lTags = loggerTagsFactory('thumbnail')
|
||||||
|
|
||||||
type ImageSize = { height?: number, width?: number }
|
type ImageSize = { height?: number, width?: number }
|
||||||
|
|
||||||
|
@ -88,29 +95,57 @@ function updateLocalVideoMiniatureFromExisting (options: {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns thumbnail models sorted by their size (height) in descendent order (biggest first)
|
||||||
function generateLocalVideoMiniature (options: {
|
function generateLocalVideoMiniature (options: {
|
||||||
video: MVideoThumbnail
|
video: MVideoThumbnail
|
||||||
videoFile: MVideoFile
|
videoFile: MVideoFile
|
||||||
type: ThumbnailType_Type
|
types: ThumbnailType_Type[]
|
||||||
}) {
|
ffprobe?: FfprobeData
|
||||||
const { video, videoFile, type } = options
|
}): Promise<MThumbnail[]> {
|
||||||
|
const { video, videoFile, types, ffprobe } = options
|
||||||
|
|
||||||
|
if (types.length === 0) return Promise.resolve([])
|
||||||
|
|
||||||
return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), input => {
|
return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), input => {
|
||||||
const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
|
// Get bigger images to generate first
|
||||||
|
const metadatas = types.map(type => buildMetadataFromVideo(video, type))
|
||||||
|
.sort((a, b) => {
|
||||||
|
if (a.height < b.height) return 1
|
||||||
|
if (a.height === b.height) return 0
|
||||||
|
return -1
|
||||||
|
})
|
||||||
|
|
||||||
const thumbnailCreator = videoFile.isAudio()
|
let biggestImagePath: string
|
||||||
? () => processImageFromWorker({
|
return Bluebird.mapSeries(metadatas, metadata => {
|
||||||
|
const { filename, basePath, height, width, existingThumbnail, outputPath, type } = metadata
|
||||||
|
|
||||||
|
let thumbnailCreator: () => Promise<any>
|
||||||
|
|
||||||
|
if (videoFile.isAudio()) {
|
||||||
|
thumbnailCreator = () => processImageFromWorker({
|
||||||
path: ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND,
|
path: ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND,
|
||||||
destination: outputPath,
|
destination: outputPath,
|
||||||
newSize: { width, height },
|
newSize: { width, height },
|
||||||
keepOriginal: true
|
keepOriginal: true
|
||||||
})
|
})
|
||||||
: () => generateImageFromVideoFile({
|
} else if (biggestImagePath) {
|
||||||
|
thumbnailCreator = () => processImageFromWorker({
|
||||||
|
path: biggestImagePath,
|
||||||
|
destination: outputPath,
|
||||||
|
newSize: { width, height },
|
||||||
|
keepOriginal: true
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
thumbnailCreator = () => generateImageFromVideoFile({
|
||||||
fromPath: input,
|
fromPath: input,
|
||||||
folder: basePath,
|
folder: basePath,
|
||||||
imageName: filename,
|
imageName: filename,
|
||||||
size: { height, width }
|
size: { height, width },
|
||||||
|
ffprobe
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!biggestImagePath) biggestImagePath = outputPath
|
||||||
|
|
||||||
return updateThumbnailFromFunction({
|
return updateThumbnailFromFunction({
|
||||||
thumbnailCreator,
|
thumbnailCreator,
|
||||||
|
@ -123,6 +158,7 @@ function generateLocalVideoMiniature (options: {
|
||||||
existingThumbnail
|
existingThumbnail
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
@ -188,22 +224,24 @@ function updateRemoteVideoThumbnail (options: {
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
async function regenerateMiniaturesIfNeeded (video: MVideoWithAllFiles) {
|
async function regenerateMiniaturesIfNeeded (video: MVideoWithAllFiles) {
|
||||||
|
const thumbnailsToGenerate: ThumbnailType_Type[] = []
|
||||||
|
|
||||||
if (video.getMiniature().automaticallyGenerated === true) {
|
if (video.getMiniature().automaticallyGenerated === true) {
|
||||||
const miniature = await generateLocalVideoMiniature({
|
thumbnailsToGenerate.push(ThumbnailType.MINIATURE)
|
||||||
video,
|
|
||||||
videoFile: video.getMaxQualityFile(),
|
|
||||||
type: ThumbnailType.MINIATURE
|
|
||||||
})
|
|
||||||
await video.addAndSaveThumbnail(miniature)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (video.getPreview().automaticallyGenerated === true) {
|
if (video.getPreview().automaticallyGenerated === true) {
|
||||||
const preview = await generateLocalVideoMiniature({
|
thumbnailsToGenerate.push(ThumbnailType.PREVIEW)
|
||||||
|
}
|
||||||
|
|
||||||
|
const models = await generateLocalVideoMiniature({
|
||||||
video,
|
video,
|
||||||
videoFile: video.getMaxQualityFile(),
|
videoFile: video.getMaxQualityFile(),
|
||||||
type: ThumbnailType.PREVIEW
|
types: thumbnailsToGenerate
|
||||||
})
|
})
|
||||||
await video.addAndSaveThumbnail(preview)
|
|
||||||
|
for (const model of models) {
|
||||||
|
await video.addAndSaveThumbnail(model)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,6 +294,7 @@ function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType_Typ
|
||||||
const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
|
const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
type,
|
||||||
filename,
|
filename,
|
||||||
basePath,
|
basePath,
|
||||||
existingThumbnail,
|
existingThumbnail,
|
||||||
|
@ -270,6 +309,7 @@ function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType_Typ
|
||||||
const basePath = CONFIG.STORAGE.PREVIEWS_DIR
|
const basePath = CONFIG.STORAGE.PREVIEWS_DIR
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
type,
|
||||||
filename,
|
filename,
|
||||||
basePath,
|
basePath,
|
||||||
existingThumbnail,
|
existingThumbnail,
|
||||||
|
@ -325,3 +365,35 @@ async function updateThumbnailFromFunction (parameters: {
|
||||||
|
|
||||||
return thumbnail
|
return thumbnail
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function generateImageFromVideoFile (options: {
|
||||||
|
fromPath: string
|
||||||
|
folder: string
|
||||||
|
imageName: string
|
||||||
|
size: { width: number, height: number }
|
||||||
|
ffprobe?: FfprobeData
|
||||||
|
}) {
|
||||||
|
const { fromPath, folder, imageName, size, ffprobe } = options
|
||||||
|
|
||||||
|
const pendingImageName = 'pending-' + imageName
|
||||||
|
const pendingImagePath = join(folder, pendingImageName)
|
||||||
|
|
||||||
|
try {
|
||||||
|
await generateThumbnailFromVideo({ fromPath, output: pendingImagePath, ffprobe })
|
||||||
|
|
||||||
|
const destination = join(folder, imageName)
|
||||||
|
await processImageFromWorker({ path: pendingImagePath, destination, newSize: size })
|
||||||
|
|
||||||
|
return destination
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Cannot generate image from video %s.', fromPath, { err, ...lTags() })
|
||||||
|
|
||||||
|
try {
|
||||||
|
await remove(pendingImagePath)
|
||||||
|
} catch (err) {
|
||||||
|
logger.debug('Cannot remove pending image path after generation error.', { err, ...lTags() })
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -13,10 +13,11 @@ import { MIMETYPES } from '@server/initializers/constants.js'
|
||||||
async function buildNewFile (options: {
|
async function buildNewFile (options: {
|
||||||
path: string
|
path: string
|
||||||
mode: 'web-video' | 'hls'
|
mode: 'web-video' | 'hls'
|
||||||
|
ffprobe?: FfprobeData
|
||||||
}) {
|
}) {
|
||||||
const { path, mode } = options
|
const { path, mode, ffprobe: probeArg } = options
|
||||||
|
|
||||||
const probe = await ffprobePromise(path)
|
const probe = probeArg ?? await ffprobePromise(path)
|
||||||
const size = await getFileSize(path)
|
const size = await getFileSize(path)
|
||||||
|
|
||||||
const videoFile = new VideoFileModel({
|
const videoFile = new VideoFileModel({
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import Piscina from 'piscina'
|
import Piscina from 'piscina'
|
||||||
import { JOB_CONCURRENCY, WORKER_THREADS } from '@server/initializers/constants.js'
|
import { JOB_CONCURRENCY, WORKER_THREADS } from '@server/initializers/constants.js'
|
||||||
import httpBroadcast from './workers/http-broadcast.js'
|
import type httpBroadcast from './workers/http-broadcast.js'
|
||||||
import downloadImage from './workers/image-downloader.js'
|
import type downloadImage from './workers/image-downloader.js'
|
||||||
import processImage from './workers/image-processor.js'
|
import type processImage from './workers/image-processor.js'
|
||||||
|
import type getImageSize from './workers/get-image-size.js'
|
||||||
|
|
||||||
let downloadImageWorker: Piscina
|
let downloadImageWorker: Piscina
|
||||||
|
|
||||||
|
@ -37,6 +38,22 @@ function processImageFromWorker (options: Parameters<typeof processImage>[0]): P
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
let getImageSizeWorker: Piscina
|
||||||
|
|
||||||
|
function getImageSizeFromWorker (options: Parameters<typeof getImageSize>[0]): Promise<ReturnType<typeof getImageSize>> {
|
||||||
|
if (!getImageSizeWorker) {
|
||||||
|
getImageSizeWorker = new Piscina({
|
||||||
|
filename: new URL(join('workers', 'get-image-size.js'), import.meta.url).href,
|
||||||
|
concurrentTasksPerWorker: WORKER_THREADS.GET_IMAGE_SIZE.CONCURRENCY,
|
||||||
|
maxThreads: WORKER_THREADS.GET_IMAGE_SIZE.MAX_THREADS
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return getImageSizeWorker.run(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
let parallelHTTPBroadcastWorker: Piscina
|
let parallelHTTPBroadcastWorker: Piscina
|
||||||
|
|
||||||
function parallelHTTPBroadcastFromWorker (options: Parameters<typeof httpBroadcast>[0]): Promise<ReturnType<typeof httpBroadcast>> {
|
function parallelHTTPBroadcastFromWorker (options: Parameters<typeof httpBroadcast>[0]): Promise<ReturnType<typeof httpBroadcast>> {
|
||||||
|
@ -73,5 +90,6 @@ export {
|
||||||
downloadImageFromWorker,
|
downloadImageFromWorker,
|
||||||
processImageFromWorker,
|
processImageFromWorker,
|
||||||
parallelHTTPBroadcastFromWorker,
|
parallelHTTPBroadcastFromWorker,
|
||||||
|
getImageSizeFromWorker,
|
||||||
sequentialHTTPBroadcastFromWorker
|
sequentialHTTPBroadcastFromWorker
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
import { getImageSize } from '@server/helpers/image-utils.js'
|
||||||
|
|
||||||
|
export default getImageSize
|