+ const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY)
+
+ const rootFiles = await readdir(hlsDirectory)
+
+ const playlistFiles = rootFiles.filter(file => {
+ return file.endsWith('.m3u8') && file !== streamingPlaylist.playlistFilename
+ })
+
+ await cleanupLiveFiles(hlsDirectory)
+
+ await live.destroy()
+
+ video.isLive = false
+ // Reinit views
+ video.views = 0
+ video.state = VideoState.TO_TRANSCODE
+
+ await video.save()
+
+ // Remove old HLS playlist video files
+ const videoWithFiles = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
+
+ const hlsPlaylist = videoWithFiles.getHLSPlaylist()
+ await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
+
+ // Reset playlist
+ hlsPlaylist.VideoFiles = []
+ hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename()
+ hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename()
+ await hlsPlaylist.save()
+
+ let durationDone = false
+
+ for (const playlistFile of playlistFiles) {
+ const concatenatedTsFile = buildConcatenatedName(playlistFile)
+ const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
+
+ const probe = await ffprobePromise(concatenatedTsFilePath)
+ const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
+
+ const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath, probe)
+
+ const outputPath = await generateHlsPlaylistResolutionFromTS({
+ video: videoWithFiles,
+ concatenatedTsFilePath,
+ resolution: videoFileResolution,
+ isPortraitMode,
+ isAAC: audioStream?.codec_name === 'aac'
+ })
+
+ if (!durationDone) {
+ videoWithFiles.duration = await getDurationFromVideoFile(outputPath)
+ await videoWithFiles.save()
+
+ durationDone = true
+ }
+ }
+
+ await remove(replayDirectory)
+
+ // Regenerate the thumbnail & preview?
+ if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
+ await generateVideoMiniature({
+ video: videoWithFiles,
+ videoFile: videoWithFiles.getMaxQualityFile(),
+ type: ThumbnailType.MINIATURE
+ })
+ }
+
+ if (videoWithFiles.getPreview().automaticallyGenerated === true) {
+ await generateVideoMiniature({
+ video: videoWithFiles,
+ videoFile: videoWithFiles.getMaxQualityFile(),
+ type: ThumbnailType.PREVIEW
+ })
+ }
+
+ await publishAndFederateIfNeeded(videoWithFiles, true)
+}
+
+async function cleanupLiveFiles (hlsDirectory: string) {
+ if (!await pathExists(hlsDirectory)) return