]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/video-transcoding.ts
Added comments for transcode functions
[github/Chocobozzz/PeerTube.git] / server / lib / video-transcoding.ts
index a78de61e5673052955b8da1d7c48f36441a8b4f8..8e906a1eba60df6f755f047bd165f51c6de8c010 100644 (file)
@@ -1,12 +1,19 @@
-import { CONFIG } from '../initializers'
-import { extname, join } from 'path'
-import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils'
-import { copy, remove, rename, stat } from 'fs-extra'
+import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
+import { join } from 'path'
+import { getVideoFileFPS, transcode, canDoQuickTranscode } from '../helpers/ffmpeg-utils'
+import { ensureDir, move, remove, stat } from 'fs-extra'
 import { logger } from '../helpers/logger'
 import { VideoResolution } from '../../shared/models/videos'
 import { VideoFileModel } from '../models/video/video-file'
 import { VideoModel } from '../models/video/video'
-
+import { updateMasterHLSPlaylist, updateSha256Segments } from './hls'
+import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
+import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
+import { CONFIG } from '../initializers/config'
+
+/**
+ * Optimize the original video file and replace it. The resolution is not changed.
+ */
 async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
   const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
   const newExtname = '.mp4'
@@ -15,9 +22,13 @@ async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFi
   const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
   const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
 
+  const doQuickTranscode = await(canDoQuickTranscode(videoInputPath))
+
   const transcodeOptions = {
     inputPath: videoInputPath,
-    outputPath: videoTranscodedPath
+    outputPath: videoTranscodedPath,
+    resolution: inputVideoFile.resolution,
+    doQuickTranscode
   }
 
   // Could be very long!
@@ -30,7 +41,7 @@ async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFi
     inputVideoFile.set('extname', newExtname)
 
     const videoOutputPath = video.getVideoFilePath(inputVideoFile)
-    await rename(videoTranscodedPath, videoOutputPath)
+    await move(videoTranscodedPath, videoOutputPath)
     const stats = await stat(videoOutputPath)
     const fps = await getVideoFileFPS(videoOutputPath)
 
@@ -47,7 +58,10 @@ async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFi
   }
 }
 
-async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
+/**
+ * Transcode the original video file to a lower resolution.
+ */
+async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
   const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
   const extname = '.mp4'
 
@@ -60,13 +74,13 @@ async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoR
     size: 0,
     videoId: video.id
   })
-  const videoOutputPath = join(videosDirectory, video.getVideoFilename(newVideoFile))
+  const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
 
   const transcodeOptions = {
     inputPath: videoInputPath,
     outputPath: videoOutputPath,
     resolution,
-    isPortraitMode
+    isPortraitMode: isPortrait
   }
 
   await transcode(transcodeOptions)
@@ -84,48 +98,44 @@ async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoR
   video.VideoFiles.push(newVideoFile)
 }
 
-async function importVideoFile (video: VideoModel, inputFilePath: string) {
-  const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
-  const { size } = await stat(inputFilePath)
-  const fps = await getVideoFileFPS(inputFilePath)
-
-  let updatedVideoFile = new VideoFileModel({
-    resolution: videoFileResolution,
-    extname: extname(inputFilePath),
-    size,
-    fps,
-    videoId: video.id
-  })
-
-  const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
+async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
+  const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
+  await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
 
-  if (currentVideoFile) {
-    // Remove old file and old torrent
-    await video.removeFile(currentVideoFile)
-    await video.removeTorrent(currentVideoFile)
-    // Remove the old video file from the array
-    video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
+  const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile()))
+  const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
 
-    // Update the database
-    currentVideoFile.set('extname', updatedVideoFile.extname)
-    currentVideoFile.set('size', updatedVideoFile.size)
-    currentVideoFile.set('fps', updatedVideoFile.fps)
+  const transcodeOptions = {
+    inputPath: videoInputPath,
+    outputPath,
+    resolution,
+    isPortraitMode,
 
-    updatedVideoFile = currentVideoFile
+    hlsPlaylist: {
+      videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
+    }
   }
 
-  const outputPath = video.getVideoFilePath(updatedVideoFile)
-  await copy(inputFilePath, outputPath)
+  await transcode(transcodeOptions)
+
+  await updateMasterHLSPlaylist(video)
+  await updateSha256Segments(video)
 
-  await video.createTorrentAndSetInfoHash(updatedVideoFile)
+  const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
 
-  await updatedVideoFile.save()
+  await VideoStreamingPlaylistModel.upsert({
+    videoId: video.id,
+    playlistUrl,
+    segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
+    p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
+    p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
 
-  video.VideoFiles.push(updatedVideoFile)
+    type: VideoStreamingPlaylistType.HLS
+  })
 }
 
 export {
+  generateHlsPlaylist,
   optimizeVideofile,
-  transcodeOriginalVideofile,
-  importVideoFile
+  transcodeOriginalVideofile
 }