2023-04-21 14:55:10 +02:00
|
|
|
import { MutexInterface } from 'async-mutex'
|
|
|
|
import { Job } from 'bullmq'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { ensureDir, move } from 'fs-extra/esm'
|
2024-02-27 11:18:56 +01:00
|
|
|
import { join } from 'path'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { pick } from '@peertube/peertube-core-utils'
|
|
|
|
import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
|
|
|
|
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent.js'
|
|
|
|
import { sequelizeTypescript } from '@server/initializers/database.js'
|
2024-02-27 11:18:56 +01:00
|
|
|
import { MVideo } from '@server/types/models/index.js'
|
|
|
|
import { getVideoStreamDuration } from '@peertube/peertube-ffmpeg'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { CONFIG } from '../../initializers/config.js'
|
|
|
|
import { VideoFileModel } from '../../models/video/video-file.js'
|
|
|
|
import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist.js'
|
2024-02-27 11:18:56 +01:00
|
|
|
import { renameVideoFileInPlaylist, updatePlaylistAfterFileChange } from '../hls.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { generateHLSVideoFilename, getHlsResolutionPlaylistFilename } from '../paths.js'
|
2024-02-27 11:18:56 +01:00
|
|
|
import { buildNewFile } from '../video-file.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { VideoPathManager } from '../video-path-manager.js'
|
|
|
|
import { buildFFmpegVOD } from './shared/index.js'
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
// Concat TS segments from a live video to a fragmented mp4 HLS playlist
|
|
|
|
export async function generateHlsPlaylistResolutionFromTS (options: {
|
|
|
|
video: MVideo
|
|
|
|
concatenatedTsFilePath: string
|
2023-07-31 14:34:36 +02:00
|
|
|
resolution: number
|
2023-04-21 14:55:10 +02:00
|
|
|
fps: number
|
|
|
|
isAAC: boolean
|
|
|
|
inputFileMutexReleaser: MutexInterface.Releaser
|
|
|
|
}) {
|
|
|
|
return generateHlsPlaylistCommon({
|
|
|
|
type: 'hls-from-ts' as 'hls-from-ts',
|
|
|
|
inputPath: options.concatenatedTsFilePath,
|
|
|
|
|
|
|
|
...pick(options, [ 'video', 'resolution', 'fps', 'inputFileMutexReleaser', 'isAAC' ])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate an HLS playlist from an input file, and update the master playlist
|
|
|
|
export function generateHlsPlaylistResolution (options: {
|
|
|
|
video: MVideo
|
|
|
|
videoInputPath: string
|
2023-07-31 14:34:36 +02:00
|
|
|
resolution: number
|
2023-04-21 14:55:10 +02:00
|
|
|
fps: number
|
|
|
|
copyCodecs: boolean
|
|
|
|
inputFileMutexReleaser: MutexInterface.Releaser
|
|
|
|
job?: Job
|
|
|
|
}) {
|
|
|
|
return generateHlsPlaylistCommon({
|
|
|
|
type: 'hls' as 'hls',
|
|
|
|
inputPath: options.videoInputPath,
|
|
|
|
|
|
|
|
...pick(options, [ 'video', 'resolution', 'fps', 'copyCodecs', 'inputFileMutexReleaser', 'job' ])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function onHLSVideoFileTranscoding (options: {
|
|
|
|
video: MVideo
|
|
|
|
videoOutputPath: string
|
|
|
|
m3u8OutputPath: string
|
2023-09-01 16:47:25 +02:00
|
|
|
filesLockedInParent?: boolean // default false
|
2023-04-21 14:55:10 +02:00
|
|
|
}) {
|
2024-02-27 11:18:56 +01:00
|
|
|
const { video, videoOutputPath, m3u8OutputPath, filesLockedInParent = false } = options
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
// Create or update the playlist
|
|
|
|
const playlist = await retryTransactionWrapper(() => {
|
|
|
|
return sequelizeTypescript.transaction(async transaction => {
|
|
|
|
return VideoStreamingPlaylistModel.loadOrGenerate(video, transaction)
|
|
|
|
})
|
|
|
|
})
|
2024-02-27 11:18:56 +01:00
|
|
|
|
|
|
|
const newVideoFile = await buildNewFile({ mode: 'hls', path: videoOutputPath })
|
|
|
|
newVideoFile.videoStreamingPlaylistId = playlist.id
|
2023-04-21 14:55:10 +02:00
|
|
|
|
2023-09-01 16:47:25 +02:00
|
|
|
const mutexReleaser = !filesLockedInParent
|
|
|
|
? await VideoPathManager.Instance.lockFiles(video.uuid)
|
|
|
|
: null
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await video.reload()
|
|
|
|
|
2024-02-27 11:18:56 +01:00
|
|
|
const videoFilePath = VideoPathManager.Instance.getFSVideoFileOutputPath(playlist, newVideoFile)
|
2023-04-21 14:55:10 +02:00
|
|
|
await ensureDir(VideoPathManager.Instance.getFSHLSOutputPath(video))
|
|
|
|
|
|
|
|
// Move playlist file
|
2024-02-27 11:18:56 +01:00
|
|
|
const resolutionPlaylistPath = VideoPathManager.Instance.getFSHLSOutputPath(
|
|
|
|
video,
|
|
|
|
getHlsResolutionPlaylistFilename(newVideoFile.filename)
|
|
|
|
)
|
2023-04-21 14:55:10 +02:00
|
|
|
await move(m3u8OutputPath, resolutionPlaylistPath, { overwrite: true })
|
2024-02-27 11:18:56 +01:00
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
// Move video file
|
|
|
|
await move(videoOutputPath, videoFilePath, { overwrite: true })
|
|
|
|
|
2024-02-27 11:18:56 +01:00
|
|
|
await renameVideoFileInPlaylist(resolutionPlaylistPath, newVideoFile.filename)
|
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
// Update video duration if it was not set (in case of a live for example)
|
|
|
|
if (!video.duration) {
|
|
|
|
video.duration = await getVideoStreamDuration(videoFilePath)
|
|
|
|
await video.save()
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:18:56 +01:00
|
|
|
await createTorrentAndSetInfoHash(playlist, newVideoFile)
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
const oldFile = await VideoFileModel.loadHLSFile({
|
|
|
|
playlistId: playlist.id,
|
2024-02-27 11:18:56 +01:00
|
|
|
fps: newVideoFile.fps,
|
|
|
|
resolution: newVideoFile.resolution
|
2023-04-21 14:55:10 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if (oldFile) {
|
|
|
|
await video.removeStreamingPlaylistVideoFile(playlist, oldFile)
|
|
|
|
await oldFile.destroy()
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:18:56 +01:00
|
|
|
const savedVideoFile = await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
await updatePlaylistAfterFileChange(video, playlist)
|
|
|
|
|
|
|
|
return { resolutionPlaylistPath, videoFile: savedVideoFile }
|
|
|
|
} finally {
|
2023-09-01 16:47:25 +02:00
|
|
|
if (mutexReleaser) mutexReleaser()
|
2023-04-21 14:55:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function generateHlsPlaylistCommon (options: {
|
|
|
|
type: 'hls' | 'hls-from-ts'
|
|
|
|
video: MVideo
|
|
|
|
inputPath: string
|
|
|
|
|
2023-07-31 14:34:36 +02:00
|
|
|
resolution: number
|
2023-04-21 14:55:10 +02:00
|
|
|
fps: number
|
|
|
|
|
|
|
|
inputFileMutexReleaser: MutexInterface.Releaser
|
|
|
|
|
|
|
|
copyCodecs?: boolean
|
|
|
|
isAAC?: boolean
|
|
|
|
|
|
|
|
job?: Job
|
|
|
|
}) {
|
|
|
|
const { type, video, inputPath, resolution, fps, copyCodecs, isAAC, job, inputFileMutexReleaser } = options
|
|
|
|
const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
|
|
|
|
|
|
|
|
const videoTranscodedBasePath = join(transcodeDirectory, type)
|
|
|
|
await ensureDir(videoTranscodedBasePath)
|
|
|
|
|
|
|
|
const videoFilename = generateHLSVideoFilename(resolution)
|
|
|
|
const videoOutputPath = join(videoTranscodedBasePath, videoFilename)
|
|
|
|
|
|
|
|
const resolutionPlaylistFilename = getHlsResolutionPlaylistFilename(videoFilename)
|
|
|
|
const m3u8OutputPath = join(videoTranscodedBasePath, resolutionPlaylistFilename)
|
|
|
|
|
|
|
|
const transcodeOptions = {
|
|
|
|
type,
|
|
|
|
|
|
|
|
inputPath,
|
|
|
|
outputPath: m3u8OutputPath,
|
|
|
|
|
|
|
|
resolution,
|
|
|
|
fps,
|
|
|
|
copyCodecs,
|
|
|
|
|
|
|
|
isAAC,
|
|
|
|
|
|
|
|
inputFileMutexReleaser,
|
|
|
|
|
|
|
|
hlsPlaylist: {
|
|
|
|
videoFilename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await buildFFmpegVOD(job).transcode(transcodeOptions)
|
|
|
|
|
2023-09-01 16:47:25 +02:00
|
|
|
await onHLSVideoFileTranscoding({
|
|
|
|
video,
|
|
|
|
videoOutputPath,
|
|
|
|
m3u8OutputPath,
|
|
|
|
filesLockedInParent: !inputFileMutexReleaser
|
|
|
|
})
|
2023-04-21 14:55:10 +02:00
|
|
|
}
|