Federated video streaming platform using ActivityPub and P2P in the web browser with Angular. https://joinpeertube.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

105 lines
3.4 KiB

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