1 import Bluebird from 'bluebird'
2 import { move, readFile, writeFile } from 'fs-extra'
3 import { join } from 'path'
4 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
5 import { JobQueue } from '@server/lib/job-queue'
6 import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
7 import { VideoPathManager } from '@server/lib/video-path-manager'
8 import { VideoModel } from '@server/models/video/video'
9 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
10 import { initDatabaseModels } from '../../server/initializers/database'
13 .then(() => process.exit(0))
19 async function run () {
20 console.log('Migrate old HLS paths to new format.')
22 await initDatabaseModels(true)
24 JobQueue.Instance.init(true)
26 const ids = await VideoModel.listLocalIds()
28 await Bluebird.map(ids, async id => {
30 await processVideo(id)
32 console.error('Cannot process video %s.', { err })
34 }, { concurrency: 5 })
36 console.log('Migration finished!')
39 async function processVideo (videoId: number) {
40 const video = await VideoModel.loadWithFiles(videoId)
42 const hls = video.getHLSPlaylist()
43 if (video.isLive || !hls || hls.playlistFilename !== 'master.m3u8' || hls.VideoFiles.length === 0) {
47 console.log(`Renaming HLS playlist files of video ${video.name}.`)
49 const playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
50 const hlsDirPath = VideoPathManager.Instance.getFSHLSOutputPath(video)
52 const masterPlaylistPath = join(hlsDirPath, playlist.playlistFilename)
53 let masterPlaylistContent = await readFile(masterPlaylistPath, 'utf8')
55 for (const videoFile of hls.VideoFiles) {
56 const srcName = `${videoFile.resolution}.m3u8`
57 const dstName = getHlsResolutionPlaylistFilename(videoFile.filename)
59 const src = join(hlsDirPath, srcName)
60 const dst = join(hlsDirPath, dstName)
65 masterPlaylistContent = masterPlaylistContent.replace(new RegExp('^' + srcName + '$', 'm'), dstName)
67 console.error('Cannot move video file %s to %s.', src, dst, err)
71 await writeFile(masterPlaylistPath, masterPlaylistContent)
73 if (playlist.segmentsSha256Filename === 'segments-sha256.json') {
75 const newName = generateHlsSha256SegmentsFilename(video.isLive)
77 const dst = join(hlsDirPath, newName)
78 await move(join(hlsDirPath, playlist.segmentsSha256Filename), dst)
79 playlist.segmentsSha256Filename = newName
81 console.error(`Cannot rename ${video.name} segments-sha256.json file to a new name`, err)
85 if (playlist.playlistFilename === 'master.m3u8') {
87 const newName = generateHLSMasterPlaylistFilename(video.isLive)
89 const dst = join(hlsDirPath, newName)
90 await move(join(hlsDirPath, playlist.playlistFilename), dst)
91 playlist.playlistFilename = newName
93 console.error(`Cannot rename ${video.name} master.m3u8 file to a new name`, err)
97 // Everything worked, we can save the playlist now
100 const allVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
101 await federateVideoIfNeeded(allVideo, false)
103 console.log(`Successfully moved HLS files of ${video.name}.`)