video-paths.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoUUID } from '@server/types/models'
  2. import { join } from 'path'
  3. import { CONFIG } from '@server/initializers/config'
  4. import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants'
  5. import { extractVideo } from '@server/helpers/video'
  6. // ################## Video file name ##################
  7. function getVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
  8. const video = extractVideo(videoOrPlaylist)
  9. if (videoFile.isHLS()) {
  10. return generateVideoStreamingPlaylistName(video.uuid, videoFile.resolution)
  11. }
  12. return generateWebTorrentVideoName(video.uuid, videoFile.resolution, videoFile.extname)
  13. }
  14. function generateVideoStreamingPlaylistName (uuid: string, resolution: number) {
  15. return `${uuid}-${resolution}-fragmented.mp4`
  16. }
  17. function generateWebTorrentVideoName (uuid: string, resolution: number, extname: string) {
  18. return uuid + '-' + resolution + extname
  19. }
  20. function getVideoFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile, isRedundancy = false) {
  21. if (videoFile.isHLS()) {
  22. const video = extractVideo(videoOrPlaylist)
  23. return join(getHLSDirectory(video), getVideoFilename(videoOrPlaylist, videoFile))
  24. }
  25. const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR
  26. return join(baseDir, getVideoFilename(videoOrPlaylist, videoFile))
  27. }
  28. // ################## Streaming playlist ##################
  29. function getHLSDirectory (video: MVideoUUID, isRedundancy = false) {
  30. const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_STREAMING_PLAYLIST_DIRECTORY
  31. return join(baseDir, video.uuid)
  32. }
  33. // ################## Torrents ##################
  34. function getTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
  35. const video = extractVideo(videoOrPlaylist)
  36. const extension = '.torrent'
  37. if (isStreamingPlaylist(videoOrPlaylist)) {
  38. return `${video.uuid}-${videoFile.resolution}-${videoOrPlaylist.getStringType()}${extension}`
  39. }
  40. return video.uuid + '-' + videoFile.resolution + extension
  41. }
  42. function getTorrentFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
  43. return join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile))
  44. }
  45. // ---------------------------------------------------------------------------
  46. export {
  47. generateVideoStreamingPlaylistName,
  48. generateWebTorrentVideoName,
  49. getVideoFilename,
  50. getVideoFilePath,
  51. getTorrentFileName,
  52. getTorrentFilePath,
  53. getHLSDirectory
  54. }