]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/hls.ts
Generate random uuid for video files
[github/Chocobozzz/PeerTube.git] / server / lib / hls.ts
index e38a8788c654a308cdc7c112b67968af7d70bde8..212bd095be9cb01fdd7b3fbb3c194c8fefc52cb3 100644 (file)
@@ -1,18 +1,18 @@
-import { basename, dirname, join } from 'path'
-import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION } from '../initializers/constants'
 import { close, ensureDir, move, open, outputJSON, pathExists, read, readFile, remove, writeFile } from 'fs-extra'
-import { getVideoStreamSize, getAudioStreamCodec, getVideoStreamCodec } from '../helpers/ffmpeg-utils'
+import { flatten, uniq } from 'lodash'
+import { basename, dirname, join } from 'path'
+import { MVideoWithFile } from '@server/types/models'
 import { sha256 } from '../helpers/core-utils'
-import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
+import { getAudioStreamCodec, getVideoStreamCodec, getVideoStreamSize } from '../helpers/ffprobe-utils'
 import { logger } from '../helpers/logger'
 import { doRequest, doRequestAndSaveToFile } from '../helpers/requests'
 import { generateRandomString } from '../helpers/utils'
-import { flatten, uniq } from 'lodash'
-import { VideoFileModel } from '../models/video/video-file'
 import { CONFIG } from '../initializers/config'
+import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION } from '../initializers/constants'
 import { sequelizeTypescript } from '../initializers/database'
-import { MVideoWithFile } from '@server/types/models'
-import { getVideoFilename, getVideoFilePath } from './video-paths'
+import { VideoFileModel } from '../models/video/video-file'
+import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
+import { getVideoFilePath } from './video-paths'
 
 async function updateStreamingPlaylistsInfohashesIfNeeded () {
   const playlistsToUpdate = await VideoStreamingPlaylistModel.listByIncorrectPeerVersion()
@@ -36,8 +36,10 @@ async function updateMasterHLSPlaylist (video: MVideoWithFile) {
   const streamingPlaylist = video.getHLSPlaylist()
 
   for (const file of streamingPlaylist.VideoFiles) {
+    const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(file.resolution)
+
     // If we did not generated a playlist for this resolution, skip
-    const filePlaylistPath = join(directory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(file.resolution))
+    const filePlaylistPath = join(directory, playlistFilename)
     if (await pathExists(filePlaylistPath) === false) continue
 
     const videoFilePath = getVideoFilePath(streamingPlaylist, file)
@@ -50,16 +52,15 @@ async function updateMasterHLSPlaylist (video: MVideoWithFile) {
     let line = `#EXT-X-STREAM-INF:${bandwidth},${resolution}`
     if (file.fps) line += ',FRAME-RATE=' + file.fps
 
-    const videoCodec = await getVideoStreamCodec(videoFilePath)
-    line += `,CODECS="${videoCodec}`
-
-    const audioCodec = await getAudioStreamCodec(videoFilePath)
-    if (audioCodec) line += `,${audioCodec}`
+    const codecs = await Promise.all([
+      getVideoStreamCodec(videoFilePath),
+      getAudioStreamCodec(videoFilePath)
+    ])
 
-    line += '"'
+    line += `,CODECS="${codecs.filter(c => !!c).join(',')}"`
 
     masterPlaylists.push(line)
-    masterPlaylists.push(VideoStreamingPlaylistModel.getHlsPlaylistFilename(file.resolution))
+    masterPlaylists.push(playlistFilename)
   }
 
   await writeFile(masterPlaylistPath, masterPlaylists.join('\n') + '\n')
@@ -93,7 +94,7 @@ async function updateSha256VODSegments (video: MVideoWithFile) {
     }
     await close(fd)
 
-    const videoFilename = getVideoFilename(hlsPlaylist, file)
+    const videoFilename = file.filename
     json[videoFilename] = rangeHashes
   }
 
@@ -106,28 +107,12 @@ async function buildSha256Segment (segmentPath: string) {
   return sha256(buf)
 }
 
-function getRangesFromPlaylist (playlistContent: string) {
-  const ranges: { offset: number, length: number }[] = []
-  const lines = playlistContent.split('\n')
-  const regex = /^#EXT-X-BYTERANGE:(\d+)@(\d+)$/
-
-  for (const line of lines) {
-    const captured = regex.exec(line)
-
-    if (captured) {
-      ranges.push({ length: parseInt(captured[1], 10), offset: parseInt(captured[2], 10) })
-    }
-  }
-
-  return ranges
-}
-
 function downloadPlaylistSegments (playlistUrl: string, destinationDir: string, timeout: number) {
   let timer
 
   logger.info('Importing HLS playlist %s', playlistUrl)
 
-  return new Promise<string>(async (res, rej) => {
+  return new Promise<void>(async (res, rej) => {
     const tmpDirectory = join(CONFIG.STORAGE.TMP_DIR, await generateRandomString(10))
 
     await ensureDir(tmpDirectory)
@@ -151,7 +136,7 @@ function downloadPlaylistSegments (playlistUrl: string, destinationDir: string,
         const destPath = join(tmpDirectory, basename(fileUrl))
 
         const bodyKBLimit = 10 * 1000 * 1000 // 10GB
-        await doRequestAndSaveToFile({ uri: fileUrl }, destPath, bodyKBLimit)
+        await doRequestAndSaveToFile(fileUrl, destPath, { bodyKBLimit })
       }
 
       clearTimeout(timer)
@@ -172,7 +157,7 @@ function downloadPlaylistSegments (playlistUrl: string, destinationDir: string,
   }
 
   async function fetchUniqUrls (playlistUrl: string) {
-    const { body } = await doRequest<string>({ uri: playlistUrl })
+    const { body } = await doRequest(playlistUrl)
 
     if (!body) return []
 
@@ -199,3 +184,19 @@ export {
 }
 
 // ---------------------------------------------------------------------------
+
+function getRangesFromPlaylist (playlistContent: string) {
+  const ranges: { offset: number, length: number }[] = []
+  const lines = playlistContent.split('\n')
+  const regex = /^#EXT-X-BYTERANGE:(\d+)@(\d+)$/
+
+  for (const line of lines) {
+    const captured = regex.exec(line)
+
+    if (captured) {
+      ranges.push({ length: parseInt(captured[1], 10), offset: parseInt(captured[2], 10) })
+    }
+  }
+
+  return ranges
+}