]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/video-transcoding.ts
Save
[github/Chocobozzz/PeerTube.git] / server / lib / video-transcoding.ts
index 444b0d954be1dc674d3d198798b98a09ec735249..a7b73a30db2a4be67525bc33f2af52868afc4ae0 100644 (file)
@@ -2,8 +2,8 @@ import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSER
 import { basename, extname as extnameUtil, join } from 'path'
 import {
   canDoQuickTranscode,
-  getMetadataFromFile,
   getDurationFromVideoFile,
+  getMetadataFromFile,
   getVideoFileFPS,
   transcode,
   TranscodeOptions,
@@ -13,14 +13,14 @@ import { copyFile, 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 { updateMasterHLSPlaylist, updateSha256Segments } from './hls'
+import { updateMasterHLSPlaylist, updateSha256VODSegments } 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'
-import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/typings/models'
+import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/types/models'
 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
 import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths'
-import { extractVideo } from './videos'
+import { spawn } from 'child_process'
 
 /**
  * Optimize the original video file and replace it. The resolution is not changed.
@@ -183,7 +183,7 @@ async function generateHlsPlaylist (video: MVideoWithFile, resolution: VideoReso
   const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
     videoId: video.id,
     playlistUrl,
-    segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
+    segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
     p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
     p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
 
@@ -208,13 +208,13 @@ async function generateHlsPlaylist (video: MVideoWithFile, resolution: VideoReso
 
   await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
 
-  await newVideoFile.save()
+  await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
   videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
 
   video.setHLSPlaylist(videoStreamingPlaylist)
 
   await updateMasterHLSPlaylist(video)
-  await updateSha256Segments(video)
+  await updateSha256VODSegments(video)
 
   return video
 }
@@ -235,23 +235,16 @@ async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoF
   const fps = await getVideoFileFPS(transcodingPath)
   const metadata = await getMetadataFromFile(transcodingPath)
 
-  await move(transcodingPath, outputPath)
-
-  const extractedVideo = extractVideo(video)
+  await move(transcodingPath, outputPath, { overwrite: true })
 
   videoFile.size = stats.size
   videoFile.fps = fps
   videoFile.metadata = metadata
-  videoFile.metadataUrl = extractedVideo.getVideoFileMetadataUrl(videoFile, extractedVideo.getBaseUrls().baseUrlHttp)
 
   await createTorrentAndSetInfoHash(video, videoFile)
 
-  const updatedVideoFile = await videoFile.save()
-
-  // Add it if this is a new created file
-  if (video.VideoFiles.some(f => f.id === videoFile.id) === false) {
-    video.VideoFiles.push(updatedVideoFile)
-  }
+  await VideoFileModel.customUpsert(videoFile, 'video', undefined)
+  video.VideoFiles = await video.$get('VideoFiles')
 
   return video
 }