2023-07-31 07:34:36 -05:00
|
|
|
import { ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS, isAudioFile } from '@peertube/peertube-ffmpeg'
|
2024-03-15 09:47:18 -05:00
|
|
|
import { FileStorage, VideoFileMetadata, VideoResolution } from '@peertube/peertube-models'
|
|
|
|
import { getFileSize, getLowercaseExtension } from '@peertube/peertube-node-utils'
|
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { MIMETYPES } from '@server/initializers/constants.js'
|
|
|
|
import { VideoFileModel } from '@server/models/video/video-file.js'
|
|
|
|
import { VideoSourceModel } from '@server/models/video/video-source.js'
|
|
|
|
import { MVideo, MVideoFile, MVideoId, MVideoWithAllFiles } from '@server/types/models/index.js'
|
|
|
|
import { FfprobeData } from 'fluent-ffmpeg'
|
|
|
|
import { move, remove } from 'fs-extra'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { lTags } from './object-storage/shared/index.js'
|
2024-03-15 09:47:18 -05:00
|
|
|
import { storeOriginalVideoFile } from './object-storage/videos.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { generateHLSVideoFilename, generateWebVideoFilename } from './paths.js'
|
|
|
|
import { VideoPathManager } from './video-path-manager.js'
|
2023-04-21 07:55:10 -05:00
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
export async function buildNewFile (options: {
|
2023-04-21 07:55:10 -05:00
|
|
|
path: string
|
|
|
|
mode: 'web-video' | 'hls'
|
2023-10-19 07:18:22 -05:00
|
|
|
ffprobe?: FfprobeData
|
2023-04-21 07:55:10 -05:00
|
|
|
}) {
|
2023-10-19 07:18:22 -05:00
|
|
|
const { path, mode, ffprobe: probeArg } = options
|
2023-04-21 07:55:10 -05:00
|
|
|
|
2023-10-19 07:18:22 -05:00
|
|
|
const probe = probeArg ?? await ffprobePromise(path)
|
2023-04-21 07:55:10 -05:00
|
|
|
const size = await getFileSize(path)
|
|
|
|
|
|
|
|
const videoFile = new VideoFileModel({
|
|
|
|
extname: getLowercaseExtension(path),
|
|
|
|
size,
|
|
|
|
metadata: await buildFileMetadata(path, probe)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (await isAudioFile(path, probe)) {
|
2024-02-28 03:00:01 -06:00
|
|
|
videoFile.fps = 0
|
2023-04-21 07:55:10 -05:00
|
|
|
videoFile.resolution = VideoResolution.H_NOVIDEO
|
2024-02-28 03:00:01 -06:00
|
|
|
videoFile.width = 0
|
|
|
|
videoFile.height = 0
|
2023-04-21 07:55:10 -05:00
|
|
|
} else {
|
2024-02-27 04:18:56 -06:00
|
|
|
const dimensions = await getVideoStreamDimensionsInfo(path, probe)
|
2023-04-21 07:55:10 -05:00
|
|
|
videoFile.fps = await getVideoStreamFPS(path, probe)
|
2024-02-27 04:18:56 -06:00
|
|
|
videoFile.resolution = dimensions.resolution
|
|
|
|
videoFile.width = dimensions.width
|
|
|
|
videoFile.height = dimensions.height
|
2023-04-21 07:55:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
videoFile.filename = mode === 'web-video'
|
2023-07-11 02:21:13 -05:00
|
|
|
? generateWebVideoFilename(videoFile.resolution, videoFile.extname)
|
2023-04-21 07:55:10 -05:00
|
|
|
: generateHLSVideoFilename(videoFile.resolution)
|
|
|
|
|
|
|
|
return videoFile
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2022-07-29 07:50:41 -05:00
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
export async function removeHLSPlaylist (video: MVideoWithAllFiles) {
|
2022-07-29 07:50:41 -05:00
|
|
|
const hls = video.getHLSPlaylist()
|
|
|
|
if (!hls) return
|
|
|
|
|
2023-05-22 03:16:45 -05:00
|
|
|
const videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
|
2022-07-29 07:50:41 -05:00
|
|
|
|
2023-05-22 03:16:45 -05:00
|
|
|
try {
|
|
|
|
await video.removeStreamingPlaylistFiles(hls)
|
|
|
|
await hls.destroy()
|
|
|
|
|
|
|
|
video.VideoStreamingPlaylists = video.VideoStreamingPlaylists.filter(p => p.id !== hls.id)
|
|
|
|
} finally {
|
|
|
|
videoFileMutexReleaser()
|
|
|
|
}
|
2022-07-29 07:50:41 -05:00
|
|
|
}
|
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
export async function removeHLSFile (video: MVideoWithAllFiles, fileToDeleteId: number) {
|
2022-07-29 07:50:41 -05:00
|
|
|
logger.info('Deleting HLS file %d of %s.', fileToDeleteId, video.url, lTags(video.uuid))
|
|
|
|
|
|
|
|
const hls = video.getHLSPlaylist()
|
|
|
|
const files = hls.VideoFiles
|
|
|
|
|
|
|
|
if (files.length === 1) {
|
|
|
|
await removeHLSPlaylist(video)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2023-05-22 03:16:45 -05:00
|
|
|
const videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
|
2022-07-29 07:50:41 -05:00
|
|
|
|
2023-05-22 03:16:45 -05:00
|
|
|
try {
|
|
|
|
const toDelete = files.find(f => f.id === fileToDeleteId)
|
|
|
|
await video.removeStreamingPlaylistVideoFile(video.getHLSPlaylist(), toDelete)
|
|
|
|
await toDelete.destroy()
|
|
|
|
|
|
|
|
hls.VideoFiles = hls.VideoFiles.filter(f => f.id !== toDelete.id)
|
|
|
|
} finally {
|
|
|
|
videoFileMutexReleaser()
|
|
|
|
}
|
2022-07-29 07:50:41 -05:00
|
|
|
|
|
|
|
return hls
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
export async function removeAllWebVideoFiles (video: MVideoWithAllFiles) {
|
2023-05-22 03:16:45 -05:00
|
|
|
const videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (const file of video.VideoFiles) {
|
2023-07-11 02:21:13 -05:00
|
|
|
await video.removeWebVideoFile(file)
|
2023-05-22 03:16:45 -05:00
|
|
|
await file.destroy()
|
|
|
|
}
|
2022-07-29 07:50:41 -05:00
|
|
|
|
2023-05-22 03:16:45 -05:00
|
|
|
video.VideoFiles = []
|
|
|
|
} finally {
|
|
|
|
videoFileMutexReleaser()
|
|
|
|
}
|
2022-07-29 07:50:41 -05:00
|
|
|
|
|
|
|
return video
|
|
|
|
}
|
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
export async function removeWebVideoFile (video: MVideoWithAllFiles, fileToDeleteId: number) {
|
2022-07-29 07:50:41 -05:00
|
|
|
const files = video.VideoFiles
|
|
|
|
|
|
|
|
if (files.length === 1) {
|
2023-07-11 02:21:13 -05:00
|
|
|
return removeAllWebVideoFiles(video)
|
2022-07-29 07:50:41 -05:00
|
|
|
}
|
|
|
|
|
2023-05-22 03:16:45 -05:00
|
|
|
const videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
|
|
|
|
try {
|
|
|
|
const toDelete = files.find(f => f.id === fileToDeleteId)
|
2023-07-11 02:21:13 -05:00
|
|
|
await video.removeWebVideoFile(toDelete)
|
2023-05-22 03:16:45 -05:00
|
|
|
await toDelete.destroy()
|
2022-07-29 07:50:41 -05:00
|
|
|
|
2023-05-22 03:16:45 -05:00
|
|
|
video.VideoFiles = files.filter(f => f.id !== toDelete.id)
|
|
|
|
} finally {
|
|
|
|
videoFileMutexReleaser()
|
|
|
|
}
|
2022-07-29 07:50:41 -05:00
|
|
|
|
|
|
|
return video
|
|
|
|
}
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
export async function buildFileMetadata (path: string, existingProbe?: FfprobeData) {
|
2023-04-21 07:55:10 -05:00
|
|
|
const metadata = existingProbe || await ffprobePromise(path)
|
|
|
|
|
|
|
|
return new VideoFileMetadata(metadata)
|
|
|
|
}
|
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
export function getVideoFileMimeType (extname: string, isAudio: boolean) {
|
2023-10-03 05:20:11 -05:00
|
|
|
return isAudio && extname === '.mp4' // We use .mp4 even for audio file only
|
|
|
|
? MIMETYPES.AUDIO.EXT_MIMETYPE['.m4a']
|
|
|
|
: MIMETYPES.VIDEO.EXT_MIMETYPE[extname]
|
|
|
|
}
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
export async function createVideoSource (options: {
|
|
|
|
inputFilename: string
|
|
|
|
inputProbe: FfprobeData
|
|
|
|
inputPath: string
|
|
|
|
video: MVideoId
|
|
|
|
createdAt?: Date
|
|
|
|
}) {
|
|
|
|
const { inputFilename, inputPath, inputProbe, video, createdAt } = options
|
|
|
|
|
|
|
|
const videoSource = new VideoSourceModel({
|
|
|
|
inputFilename,
|
|
|
|
videoId: video.id,
|
|
|
|
createdAt
|
|
|
|
})
|
|
|
|
|
|
|
|
if (inputPath) {
|
|
|
|
const probe = inputProbe ?? await ffprobePromise(inputPath)
|
|
|
|
|
|
|
|
if (await isAudioFile(inputPath, probe)) {
|
|
|
|
videoSource.fps = 0
|
|
|
|
videoSource.resolution = VideoResolution.H_NOVIDEO
|
|
|
|
videoSource.width = 0
|
|
|
|
videoSource.height = 0
|
|
|
|
} else {
|
|
|
|
const dimensions = await getVideoStreamDimensionsInfo(inputPath, probe)
|
|
|
|
videoSource.fps = await getVideoStreamFPS(inputPath, probe)
|
|
|
|
videoSource.resolution = dimensions.resolution
|
|
|
|
videoSource.width = dimensions.width
|
|
|
|
videoSource.height = dimensions.height
|
|
|
|
}
|
|
|
|
|
|
|
|
videoSource.metadata = await buildFileMetadata(inputPath, probe)
|
|
|
|
videoSource.size = await getFileSize(inputPath)
|
|
|
|
}
|
2023-04-21 07:55:10 -05:00
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
return videoSource.save()
|
|
|
|
}
|
2023-04-21 07:55:10 -05:00
|
|
|
|
2024-03-15 09:47:18 -05:00
|
|
|
export async function saveNewOriginalFileIfNeeded (video: MVideo, videoFile: MVideoFile) {
|
|
|
|
if (!CONFIG.TRANSCODING.ORIGINAL_FILE.KEEP) return
|
|
|
|
|
|
|
|
const videoSource = await VideoSourceModel.loadLatest(video.id)
|
|
|
|
|
|
|
|
// Already have saved an original file
|
|
|
|
if (!videoSource || videoSource.keptOriginalFilename) return
|
|
|
|
videoSource.keptOriginalFilename = videoFile.filename
|
|
|
|
|
|
|
|
const lTags = loggerTagsFactory(video.uuid)
|
|
|
|
|
|
|
|
logger.info(`Storing original video file ${videoSource.keptOriginalFilename} of video ${video.name}`, lTags())
|
|
|
|
|
|
|
|
const sourcePath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, videoFile)
|
|
|
|
|
|
|
|
if (CONFIG.OBJECT_STORAGE.ENABLED) {
|
|
|
|
const fileUrl = await storeOriginalVideoFile(sourcePath, videoSource.keptOriginalFilename)
|
|
|
|
await remove(sourcePath)
|
|
|
|
|
|
|
|
videoSource.storage = FileStorage.OBJECT_STORAGE
|
|
|
|
videoSource.fileUrl = fileUrl
|
|
|
|
} else {
|
|
|
|
const destinationPath = VideoPathManager.Instance.getFSOriginalVideoFilePath(videoSource.keptOriginalFilename)
|
|
|
|
await move(sourcePath, destinationPath)
|
|
|
|
|
|
|
|
videoSource.storage = FileStorage.FILE_SYSTEM
|
|
|
|
}
|
|
|
|
|
|
|
|
await videoSource.save()
|
|
|
|
|
|
|
|
// Delete previously kept video files
|
|
|
|
const allSources = await VideoSourceModel.listAll(video.id)
|
|
|
|
for (const oldSource of allSources) {
|
|
|
|
if (!oldSource.keptOriginalFilename) continue
|
|
|
|
if (oldSource.id === videoSource.id) continue
|
|
|
|
|
|
|
|
try {
|
|
|
|
await video.removeOriginalFile(oldSource)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot delete old original file ' + oldSource.keptOriginalFilename, { err, ...lTags() })
|
|
|
|
}
|
|
|
|
}
|
2022-07-29 07:50:41 -05:00
|
|
|
}
|