Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aaedadd

Browse files
committed
Transcode HLS playlists in a tmp directory
1 parent d8ba492 commit aaedadd

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

server/lib/video-transcoding.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,20 @@ async function generateHlsPlaylistCommon (options: {
270270
job?: Job
271271
}) {
272272
const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
273+
const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
273274

274-
const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
275-
await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
275+
const videoTranscodedBasePath = join(transcodeDirectory, type, video.uuid)
276+
await ensureDir(videoTranscodedBasePath)
276277

277-
const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
278278
const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
279+
const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)
280+
const playlistFileTranscodePath = join(videoTranscodedBasePath, playlistFilename)
279281

280282
const transcodeOptions = {
281283
type,
282284

283285
inputPath,
284-
outputPath,
286+
outputPath: playlistFileTranscodePath,
285287

286288
availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
287289
profile: CONFIG.TRANSCODING.PROFILE,
@@ -303,6 +305,7 @@ async function generateHlsPlaylistCommon (options: {
303305

304306
const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
305307

308+
// Create or update the playlist
306309
const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
307310
videoId: video.id,
308311
playlistUrl,
@@ -314,6 +317,7 @@ async function generateHlsPlaylistCommon (options: {
314317
}, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
315318
videoStreamingPlaylist.Video = video
316319

320+
// Build the new playlist file
317321
const newVideoFile = new VideoFileModel({
318322
resolution,
319323
extname: extnameUtil(videoFilename),
@@ -323,6 +327,19 @@ async function generateHlsPlaylistCommon (options: {
323327
})
324328

325329
const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
330+
331+
// Move files from tmp transcoded directory to the appropriate place
332+
const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
333+
await ensureDir(baseHlsDirectory)
334+
335+
// Move playlist file
336+
const playlistPath = join(baseHlsDirectory, playlistFilename)
337+
await move(playlistFileTranscodePath, playlistPath)
338+
// Move video file
339+
await move(join(videoTranscodedBasePath, videoFilename), videoFilePath)
340+
// Cleanup directory
341+
await remove(videoTranscodedBasePath)
342+
326343
const stats = await stat(videoFilePath)
327344

328345
newVideoFile.size = stats.size
@@ -344,5 +361,5 @@ async function generateHlsPlaylistCommon (options: {
344361
await updateMasterHLSPlaylist(video)
345362
await updateSha256VODSegments(video)
346363

347-
return outputPath
364+
return playlistPath
348365
}

0 commit comments

Comments
 (0)