1 import { Job } from 'bull'
2 import { copyFile, ensureDir, move, remove, stat } from 'fs-extra'
3 import { basename, extname as extnameUtil, join } from 'path'
4 import { toEven } from '@server/helpers/core-utils'
5 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
6 import { MStreamingPlaylistFilesVideo, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
7 import { VideoResolution, VideoStorage } from '../../../shared/models/videos'
8 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
12 getVideoStreamDuration,
16 TranscodeVODOptionsType
17 } from '../../helpers/ffmpeg'
18 import { CONFIG } from '../../initializers/config'
19 import { P2P_MEDIA_LOADER_PEER_VERSION } from '../../initializers/constants'
20 import { VideoFileModel } from '../../models/video/video-file'
21 import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist'
22 import { updateMasterHLSPlaylist, updateSha256VODSegments } from '../hls'
24 generateHLSMasterPlaylistFilename,
25 generateHlsSha256SegmentsFilename,
26 generateHLSVideoFilename,
27 generateWebTorrentVideoFilename,
28 getHlsResolutionPlaylistFilename
30 import { VideoPathManager } from '../video-path-manager'
31 import { VideoTranscodingProfilesManager } from './default-transcoding-profiles'
35 * Functions that run transcoding functions, update the database, cleanup files, create torrent files...
36 * Mainly called by the job queue
40 // Optimize the original video file and replace it. The resolution is not changed.
41 function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVideoFile, job?: Job) {
42 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
43 const newExtname = '.mp4'
45 return VideoPathManager.Instance.makeAvailableVideoFile(inputVideoFile.withVideoOrPlaylist(video), async videoInputPath => {
46 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
48 const transcodeType: TranscodeVODOptionsType = await canDoQuickTranscode(videoInputPath)
52 const resolution = toEven(inputVideoFile.resolution)
54 const transcodeOptions: TranscodeVODOptions = {
57 inputPath: videoInputPath,
58 outputPath: videoTranscodedPath,
60 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
61 profile: CONFIG.TRANSCODING.PROFILE,
68 // Could be very long!
69 await transcodeVOD(transcodeOptions)
71 // Important to do this before getVideoFilename() to take in account the new filename
72 inputVideoFile.extname = newExtname
73 inputVideoFile.filename = generateWebTorrentVideoFilename(resolution, newExtname)
74 inputVideoFile.storage = VideoStorage.FILE_SYSTEM
76 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, inputVideoFile)
78 const { videoFile } = await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
79 await remove(videoInputPath)
81 return { transcodeType, videoFile }
85 // Transcode the original video file to a lower resolution
86 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
87 function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: VideoResolution, isPortrait: boolean, job: Job) {
88 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
89 const extname = '.mp4'
91 return VideoPathManager.Instance.makeAvailableVideoFile(video.getMaxQualityFile().withVideoOrPlaylist(video), async videoInputPath => {
92 const newVideoFile = new VideoFileModel({
95 filename: generateWebTorrentVideoFilename(resolution, extname),
100 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
101 const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename)
103 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
105 type: 'only-audio' as 'only-audio',
107 inputPath: videoInputPath,
108 outputPath: videoTranscodedPath,
110 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
111 profile: CONFIG.TRANSCODING.PROFILE,
118 type: 'video' as 'video',
119 inputPath: videoInputPath,
120 outputPath: videoTranscodedPath,
122 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
123 profile: CONFIG.TRANSCODING.PROFILE,
126 isPortraitMode: isPortrait,
131 await transcodeVOD(transcodeOptions)
133 return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
137 // Merge an image with an audio file to create a video
138 function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolution, job: Job) {
139 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
140 const newExtname = '.mp4'
142 const inputVideoFile = video.getMinQualityFile()
144 return VideoPathManager.Instance.makeAvailableVideoFile(inputVideoFile.withVideoOrPlaylist(video), async audioInputPath => {
145 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
147 // If the user updates the video preview during transcoding
148 const previewPath = video.getPreview().getPath()
149 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
150 await copyFile(previewPath, tmpPreviewPath)
152 const transcodeOptions = {
153 type: 'merge-audio' as 'merge-audio',
155 inputPath: tmpPreviewPath,
156 outputPath: videoTranscodedPath,
158 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
159 profile: CONFIG.TRANSCODING.PROFILE,
161 audioPath: audioInputPath,
168 await transcodeVOD(transcodeOptions)
170 await remove(audioInputPath)
171 await remove(tmpPreviewPath)
173 await remove(tmpPreviewPath)
177 // Important to do this before getVideoFilename() to take in account the new file extension
178 inputVideoFile.extname = newExtname
179 inputVideoFile.resolution = resolution
180 inputVideoFile.filename = generateWebTorrentVideoFilename(inputVideoFile.resolution, newExtname)
182 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, inputVideoFile)
183 // ffmpeg generated a new video file, so update the video duration
184 // See https://trac.ffmpeg.org/ticket/5456
185 video.duration = await getVideoStreamDuration(videoTranscodedPath)
188 return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
192 // Concat TS segments from a live video to a fragmented mp4 HLS playlist
193 async function generateHlsPlaylistResolutionFromTS (options: {
195 concatenatedTsFilePath: string
196 resolution: VideoResolution
197 isPortraitMode: boolean
200 return generateHlsPlaylistCommon({
201 video: options.video,
202 resolution: options.resolution,
203 isPortraitMode: options.isPortraitMode,
204 inputPath: options.concatenatedTsFilePath,
205 type: 'hls-from-ts' as 'hls-from-ts',
210 // Generate an HLS playlist from an input file, and update the master playlist
211 function generateHlsPlaylistResolution (options: {
213 videoInputPath: string
214 resolution: VideoResolution
216 isPortraitMode: boolean
219 return generateHlsPlaylistCommon({
220 video: options.video,
221 resolution: options.resolution,
222 copyCodecs: options.copyCodecs,
223 isPortraitMode: options.isPortraitMode,
224 inputPath: options.videoInputPath,
225 type: 'hls' as 'hls',
230 // ---------------------------------------------------------------------------
233 generateHlsPlaylistResolution,
234 generateHlsPlaylistResolutionFromTS,
235 optimizeOriginalVideofile,
236 transcodeNewWebTorrentResolution,
240 // ---------------------------------------------------------------------------
242 async function onWebTorrentVideoFileTranscoding (
243 video: MVideoFullLight,
244 videoFile: MVideoFile,
245 transcodingPath: string,
248 const stats = await stat(transcodingPath)
249 const fps = await getVideoStreamFPS(transcodingPath)
250 const metadata = await buildFileMetadata(transcodingPath)
252 await move(transcodingPath, outputPath, { overwrite: true })
254 videoFile.size = stats.size
256 videoFile.metadata = metadata
258 await createTorrentAndSetInfoHash(video, videoFile)
260 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
261 video.VideoFiles = await video.$get('VideoFiles')
263 return { video, videoFile }
266 async function generateHlsPlaylistCommon (options: {
267 type: 'hls' | 'hls-from-ts'
270 resolution: VideoResolution
273 isPortraitMode: boolean
277 const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
278 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
280 const videoTranscodedBasePath = join(transcodeDirectory, type)
281 await ensureDir(videoTranscodedBasePath)
283 const videoFilename = generateHLSVideoFilename(resolution)
284 const resolutionPlaylistFilename = getHlsResolutionPlaylistFilename(videoFilename)
285 const resolutionPlaylistFileTranscodePath = join(videoTranscodedBasePath, resolutionPlaylistFilename)
287 const transcodeOptions = {
291 outputPath: resolutionPlaylistFileTranscodePath,
293 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
294 profile: CONFIG.TRANSCODING.PROFILE,
309 await transcodeVOD(transcodeOptions)
311 // Create or update the playlist
312 const playlist = await VideoStreamingPlaylistModel.loadOrGenerate(video)
314 if (!playlist.playlistFilename) {
315 playlist.playlistFilename = generateHLSMasterPlaylistFilename(video.isLive)
318 if (!playlist.segmentsSha256Filename) {
319 playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(video.isLive)
322 playlist.p2pMediaLoaderInfohashes = []
323 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
325 playlist.type = VideoStreamingPlaylistType.HLS
327 await playlist.save()
329 // Build the new playlist file
330 const extname = extnameUtil(videoFilename)
331 const newVideoFile = new VideoFileModel({
335 filename: videoFilename,
337 videoStreamingPlaylistId: playlist.id
340 const videoFilePath = VideoPathManager.Instance.getFSVideoFileOutputPath(playlist, newVideoFile)
342 // Move files from tmp transcoded directory to the appropriate place
343 await ensureDir(VideoPathManager.Instance.getFSHLSOutputPath(video))
345 // Move playlist file
346 const resolutionPlaylistPath = VideoPathManager.Instance.getFSHLSOutputPath(video, resolutionPlaylistFilename)
347 await move(resolutionPlaylistFileTranscodePath, resolutionPlaylistPath, { overwrite: true })
349 await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true })
351 const stats = await stat(videoFilePath)
353 newVideoFile.size = stats.size
354 newVideoFile.fps = await getVideoStreamFPS(videoFilePath)
355 newVideoFile.metadata = await buildFileMetadata(videoFilePath)
357 await createTorrentAndSetInfoHash(playlist, newVideoFile)
359 const savedVideoFile = await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
361 const playlistWithFiles = playlist as MStreamingPlaylistFilesVideo
362 playlistWithFiles.VideoFiles = await playlist.$get('VideoFiles')
363 playlist.assignP2PMediaLoaderInfoHashes(video, playlistWithFiles.VideoFiles)
365 await playlist.save()
367 video.setHLSPlaylist(playlist)
369 await updateMasterHLSPlaylist(video, playlistWithFiles)
370 await updateSha256VODSegments(video, playlistWithFiles)
372 return { resolutionPlaylistPath, videoFile: savedVideoFile }