]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/migrations/peertube-4.0.ts
Stop using tsconfig register
[github/Chocobozzz/PeerTube.git] / scripts / migrations / peertube-4.0.ts
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'
11
12 run()
13 .then(() => process.exit(0))
14 .catch(err => {
15 console.error(err)
16 process.exit(-1)
17 })
18
19 async function run () {
20 console.log('Migrate old HLS paths to new format.')
21
22 await initDatabaseModels(true)
23
24 JobQueue.Instance.init(true)
25
26 const ids = await VideoModel.listLocalIds()
27
28 await Bluebird.map(ids, async id => {
29 try {
30 await processVideo(id)
31 } catch (err) {
32 console.error('Cannot process video %s.', { err })
33 }
34 }, { concurrency: 5 })
35
36 console.log('Migration finished!')
37 }
38
39 async function processVideo (videoId: number) {
40 const video = await VideoModel.loadWithFiles(videoId)
41
42 const hls = video.getHLSPlaylist()
43 if (video.isLive || !hls || hls.playlistFilename !== 'master.m3u8' || hls.VideoFiles.length === 0) {
44 return
45 }
46
47 console.log(`Renaming HLS playlist files of video ${video.name}.`)
48
49 const playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
50 const hlsDirPath = VideoPathManager.Instance.getFSHLSOutputPath(video)
51
52 const masterPlaylistPath = join(hlsDirPath, playlist.playlistFilename)
53 let masterPlaylistContent = await readFile(masterPlaylistPath, 'utf8')
54
55 for (const videoFile of hls.VideoFiles) {
56 const srcName = `${videoFile.resolution}.m3u8`
57 const dstName = getHlsResolutionPlaylistFilename(videoFile.filename)
58
59 const src = join(hlsDirPath, srcName)
60 const dst = join(hlsDirPath, dstName)
61
62 try {
63 await move(src, dst)
64
65 masterPlaylistContent = masterPlaylistContent.replace(new RegExp('^' + srcName + '$', 'm'), dstName)
66 } catch (err) {
67 console.error('Cannot move video file %s to %s.', src, dst, err)
68 }
69 }
70
71 await writeFile(masterPlaylistPath, masterPlaylistContent)
72
73 if (playlist.segmentsSha256Filename === 'segments-sha256.json') {
74 try {
75 const newName = generateHlsSha256SegmentsFilename(video.isLive)
76
77 const dst = join(hlsDirPath, newName)
78 await move(join(hlsDirPath, playlist.segmentsSha256Filename), dst)
79 playlist.segmentsSha256Filename = newName
80 } catch (err) {
81 console.error(`Cannot rename ${video.name} segments-sha256.json file to a new name`, err)
82 }
83 }
84
85 if (playlist.playlistFilename === 'master.m3u8') {
86 try {
87 const newName = generateHLSMasterPlaylistFilename(video.isLive)
88
89 const dst = join(hlsDirPath, newName)
90 await move(join(hlsDirPath, playlist.playlistFilename), dst)
91 playlist.playlistFilename = newName
92 } catch (err) {
93 console.error(`Cannot rename ${video.name} master.m3u8 file to a new name`, err)
94 }
95 }
96
97 // Everything worked, we can save the playlist now
98 await playlist.save()
99
100 const allVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
101 await federateVideoIfNeeded(allVideo, false)
102
103 console.log(`Successfully moved HLS files of ${video.name}.`)
104 }