peertube-4.0.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import Bluebird from 'bluebird'
  2. import { move } from 'fs-extra/esm'
  3. import { readFile, writeFile } from 'fs/promises'
  4. import { join } from 'path'
  5. import { initDatabaseModels } from '@server/initializers/database.js'
  6. import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js'
  7. import { JobQueue } from '@server/lib/job-queue/index.js'
  8. import {
  9. generateHLSMasterPlaylistFilename,
  10. generateHlsSha256SegmentsFilename,
  11. getHlsResolutionPlaylistFilename
  12. } from '@server/lib/paths.js'
  13. import { VideoPathManager } from '@server/lib/video-path-manager.js'
  14. import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist.js'
  15. import { VideoModel } from '@server/models/video/video.js'
  16. run()
  17. .then(() => process.exit(0))
  18. .catch(err => {
  19. console.error(err)
  20. process.exit(-1)
  21. })
  22. async function run () {
  23. console.log('Migrate old HLS paths to new format.')
  24. await initDatabaseModels(true)
  25. JobQueue.Instance.init()
  26. const ids = await VideoModel.listLocalIds()
  27. await Bluebird.map(ids, async id => {
  28. try {
  29. await processVideo(id)
  30. } catch (err) {
  31. console.error('Cannot process video %s.', { err })
  32. }
  33. }, { concurrency: 5 })
  34. console.log('Migration finished!')
  35. }
  36. async function processVideo (videoId: number) {
  37. const video = await VideoModel.loadWithFiles(videoId)
  38. const hls = video.getHLSPlaylist()
  39. if (video.isLive || !hls || hls.playlistFilename !== 'master.m3u8' || hls.VideoFiles.length === 0) {
  40. return
  41. }
  42. console.log(`Renaming HLS playlist files of video ${video.name}.`)
  43. const playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
  44. const hlsDirPath = VideoPathManager.Instance.getFSHLSOutputPath(video)
  45. const masterPlaylistPath = join(hlsDirPath, playlist.playlistFilename)
  46. let masterPlaylistContent = await readFile(masterPlaylistPath, 'utf8')
  47. for (const videoFile of hls.VideoFiles) {
  48. const srcName = `${videoFile.resolution}.m3u8`
  49. const dstName = getHlsResolutionPlaylistFilename(videoFile.filename)
  50. const src = join(hlsDirPath, srcName)
  51. const dst = join(hlsDirPath, dstName)
  52. try {
  53. await move(src, dst)
  54. masterPlaylistContent = masterPlaylistContent.replace(new RegExp('^' + srcName + '$', 'm'), dstName)
  55. } catch (err) {
  56. console.error('Cannot move video file %s to %s.', src, dst, err)
  57. }
  58. }
  59. await writeFile(masterPlaylistPath, masterPlaylistContent)
  60. if (playlist.segmentsSha256Filename === 'segments-sha256.json') {
  61. try {
  62. const newName = generateHlsSha256SegmentsFilename(video.isLive)
  63. const dst = join(hlsDirPath, newName)
  64. await move(join(hlsDirPath, playlist.segmentsSha256Filename), dst)
  65. playlist.segmentsSha256Filename = newName
  66. } catch (err) {
  67. console.error(`Cannot rename ${video.name} segments-sha256.json file to a new name`, err)
  68. }
  69. }
  70. if (playlist.playlistFilename === 'master.m3u8') {
  71. try {
  72. const newName = generateHLSMasterPlaylistFilename(video.isLive)
  73. const dst = join(hlsDirPath, newName)
  74. await move(join(hlsDirPath, playlist.playlistFilename), dst)
  75. playlist.playlistFilename = newName
  76. } catch (err) {
  77. console.error(`Cannot rename ${video.name} master.m3u8 file to a new name`, err)
  78. }
  79. }
  80. // Everything worked, we can save the playlist now
  81. await playlist.save()
  82. const allVideo = await VideoModel.loadFull(video.id)
  83. await federateVideoIfNeeded(allVideo, false)
  84. console.log(`Successfully moved HLS files of ${video.name}.`)
  85. }