From aaedadd5386b580e9ebac540201399c25c7f0b0f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 2 Feb 2021 11:50:29 +0100 Subject: Transcode HLS playlists in a tmp directory --- server/lib/video-transcoding.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'server') diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.ts index 37a4f3019..d32372fe5 100644 --- a/server/lib/video-transcoding.ts +++ b/server/lib/video-transcoding.ts @@ -270,18 +270,20 @@ async function generateHlsPlaylistCommon (options: { job?: Job }) { const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options + const transcodeDirectory = CONFIG.STORAGE.TMP_DIR - const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) - await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)) + const videoTranscodedBasePath = join(transcodeDirectory, type, video.uuid) + await ensureDir(videoTranscodedBasePath) - const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)) const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution) + const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution) + const playlistFileTranscodePath = join(videoTranscodedBasePath, playlistFilename) const transcodeOptions = { type, inputPath, - outputPath, + outputPath: playlistFileTranscodePath, availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), profile: CONFIG.TRANSCODING.PROFILE, @@ -303,6 +305,7 @@ async function generateHlsPlaylistCommon (options: { const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) + // Create or update the playlist const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ videoId: video.id, playlistUrl, @@ -314,6 +317,7 @@ async function generateHlsPlaylistCommon (options: { }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ] videoStreamingPlaylist.Video = video + // Build the new playlist file const newVideoFile = new VideoFileModel({ resolution, extname: extnameUtil(videoFilename), @@ -323,6 +327,19 @@ async function generateHlsPlaylistCommon (options: { }) const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile) + + // Move files from tmp transcoded directory to the appropriate place + const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) + await ensureDir(baseHlsDirectory) + + // Move playlist file + const playlistPath = join(baseHlsDirectory, playlistFilename) + await move(playlistFileTranscodePath, playlistPath) + // Move video file + await move(join(videoTranscodedBasePath, videoFilename), videoFilePath) + // Cleanup directory + await remove(videoTranscodedBasePath) + const stats = await stat(videoFilePath) newVideoFile.size = stats.size @@ -344,5 +361,5 @@ async function generateHlsPlaylistCommon (options: { await updateMasterHLSPlaylist(video) await updateSha256VODSegments(video) - return outputPath + return playlistPath } -- cgit v1.2.3