video-paths.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile } from '@server/typings/models'
  2. import { extractVideo } from './videos'
  3. import { join } from 'path'
  4. import { CONFIG } from '@server/initializers/config'
  5. import { HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants'
  6. // ################## Video file name ##################
  7. function getVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
  8. const video = extractVideo(videoOrPlaylist)
  9. if (isStreamingPlaylist(videoOrPlaylist)) {
  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 (isStreamingPlaylist(videoOrPlaylist)) {
  22. const video = extractVideo(videoOrPlaylist)
  23. return join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid, 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. // ################## Torrents ##################
  29. function getTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
  30. const video = extractVideo(videoOrPlaylist)
  31. const extension = '.torrent'
  32. if (isStreamingPlaylist(videoOrPlaylist)) {
  33. return `${video.uuid}-${videoFile.resolution}-${videoOrPlaylist.getStringType()}${extension}`
  34. }
  35. return video.uuid + '-' + videoFile.resolution + extension
  36. }
  37. function getTorrentFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
  38. return join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile))
  39. }
  40. // ---------------------------------------------------------------------------
  41. export {
  42. generateVideoStreamingPlaylistName,
  43. generateWebTorrentVideoName,
  44. getVideoFilename,
  45. getVideoFilePath,
  46. getTorrentFileName,
  47. getTorrentFilePath
  48. }