Transcode HLS playlists in a tmp directory

This commit is contained in:
Chocobozzz 2021-02-02 11:50:29 +01:00
parent d8ba492119
commit aaedadd538
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 22 additions and 5 deletions

View File

@ -270,18 +270,20 @@ async function generateHlsPlaylistCommon (options: {
job?: Job job?: Job
}) { }) {
const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options 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) const videoTranscodedBasePath = join(transcodeDirectory, type, video.uuid)
await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)) await ensureDir(videoTranscodedBasePath)
const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution) const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)
const playlistFileTranscodePath = join(videoTranscodedBasePath, playlistFilename)
const transcodeOptions = { const transcodeOptions = {
type, type,
inputPath, inputPath,
outputPath, outputPath: playlistFileTranscodePath,
availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
profile: CONFIG.TRANSCODING.PROFILE, profile: CONFIG.TRANSCODING.PROFILE,
@ -303,6 +305,7 @@ async function generateHlsPlaylistCommon (options: {
const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
// Create or update the playlist
const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
videoId: video.id, videoId: video.id,
playlistUrl, playlistUrl,
@ -314,6 +317,7 @@ async function generateHlsPlaylistCommon (options: {
}, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ] }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
videoStreamingPlaylist.Video = video videoStreamingPlaylist.Video = video
// Build the new playlist file
const newVideoFile = new VideoFileModel({ const newVideoFile = new VideoFileModel({
resolution, resolution,
extname: extnameUtil(videoFilename), extname: extnameUtil(videoFilename),
@ -323,6 +327,19 @@ async function generateHlsPlaylistCommon (options: {
}) })
const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile) 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) const stats = await stat(videoFilePath)
newVideoFile.size = stats.size newVideoFile.size = stats.size
@ -344,5 +361,5 @@ async function generateHlsPlaylistCommon (options: {
await updateMasterHLSPlaylist(video) await updateMasterHLSPlaylist(video)
await updateSha256VODSegments(video) await updateSha256VODSegments(video)
return outputPath return playlistPath
} }