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 { retryTransactionWrapper } from '@server/helpers/database-utils'
6 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
7 import { sequelizeTypescript } from '@server/initializers/database'
8 import { MStreamingPlaylistFilesVideo, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
9 import { VideoResolution, VideoStorage } from '../../../shared/models/videos'
10 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
14 getVideoStreamDuration,
18 TranscodeVODOptionsType
19 } from '../../helpers/ffmpeg'
20 import { CONFIG } from '../../initializers/config'
21 import { P2P_MEDIA_LOADER_PEER_VERSION } from '../../initializers/constants'
22 import { VideoFileModel } from '../../models/video/video-file'
23 import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist'
24 import { updateMasterHLSPlaylist, updateSha256VODSegments } from '../hls'
26 generateHLSMasterPlaylistFilename,
27 generateHlsSha256SegmentsFilename,
28 generateHLSVideoFilename,
29 generateWebTorrentVideoFilename,
30 getHlsResolutionPlaylistFilename
32 import { VideoPathManager } from '../video-path-manager'
33 import { VideoTranscodingProfilesManager } from './default-transcoding-profiles'
37 * Functions that run transcoding functions, update the database, cleanup files, create torrent files...
38 * Mainly called by the job queue
42 // Optimize the original video file and replace it. The resolution is not changed.
43 function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVideoFile, job?: Job) {
44 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
45 const newExtname = '.mp4'
47 return VideoPathManager.Instance.makeAvailableVideoFile(inputVideoFile.withVideoOrPlaylist(video), async videoInputPath => {
48 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
50 const transcodeType: TranscodeVODOptionsType = await canDoQuickTranscode(videoInputPath)
54 const resolution = toEven(inputVideoFile.resolution)
56 const transcodeOptions: TranscodeVODOptions = {
59 inputPath: videoInputPath,
60 outputPath: videoTranscodedPath,
62 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
63 profile: CONFIG.TRANSCODING.PROFILE,
70 // Could be very long!
71 await transcodeVOD(transcodeOptions)
73 // Important to do this before getVideoFilename() to take in account the new filename
74 inputVideoFile.extname = newExtname
75 inputVideoFile.filename = generateWebTorrentVideoFilename(resolution, newExtname)
76 inputVideoFile.storage = VideoStorage.FILE_SYSTEM
78 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, inputVideoFile)
80 const { videoFile } = await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
81 await remove(videoInputPath)
83 return { transcodeType, videoFile }
87 // Transcode the original video file to a lower resolution
88 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
89 function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: VideoResolution, isPortrait: boolean, job: Job) {
90 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
91 const extname = '.mp4'
93 return VideoPathManager.Instance.makeAvailableVideoFile(video.getMaxQualityFile().withVideoOrPlaylist(video), async videoInputPath => {
94 const newVideoFile = new VideoFileModel({
97 filename: generateWebTorrentVideoFilename(resolution, extname),
102 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
103 const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename)
105 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
107 type: 'only-audio' as 'only-audio',
109 inputPath: videoInputPath,
110 outputPath: videoTranscodedPath,
112 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
113 profile: CONFIG.TRANSCODING.PROFILE,
120 type: 'video' as 'video',
121 inputPath: videoInputPath,
122 outputPath: videoTranscodedPath,
124 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
125 profile: CONFIG.TRANSCODING.PROFILE,
128 isPortraitMode: isPortrait,
133 await transcodeVOD(transcodeOptions)
135 return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
139 // Merge an image with an audio file to create a video
140 function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolution, job: Job) {
141 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
142 const newExtname = '.mp4'
144 const inputVideoFile = video.getMinQualityFile()
146 return VideoPathManager.Instance.makeAvailableVideoFile(inputVideoFile.withVideoOrPlaylist(video), async audioInputPath => {
147 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
149 // If the user updates the video preview during transcoding
150 const previewPath = video.getPreview().getPath()
151 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
152 await copyFile(previewPath, tmpPreviewPath)
154 const transcodeOptions = {
155 type: 'merge-audio' as 'merge-audio',
157 inputPath: tmpPreviewPath,
158 outputPath: videoTranscodedPath,
160 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
161 profile: CONFIG.TRANSCODING.PROFILE,
163 audioPath: audioInputPath,
170 await transcodeVOD(transcodeOptions)
172 await remove(audioInputPath)
173 await remove(tmpPreviewPath)
175 await remove(tmpPreviewPath)
179 // Important to do this before getVideoFilename() to take in account the new file extension
180 inputVideoFile.extname = newExtname
181 inputVideoFile.resolution = resolution
182 inputVideoFile.filename = generateWebTorrentVideoFilename(inputVideoFile.resolution, newExtname)
184 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, inputVideoFile)
185 // ffmpeg generated a new video file, so update the video duration
186 // See https://trac.ffmpeg.org/ticket/5456
187 video.duration = await getVideoStreamDuration(videoTranscodedPath)
190 return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
194 // Concat TS segments from a live video to a fragmented mp4 HLS playlist
195 async function generateHlsPlaylistResolutionFromTS (options: {
197 concatenatedTsFilePath: string
198 resolution: VideoResolution
199 isPortraitMode: boolean
202 return generateHlsPlaylistCommon({
203 video: options.video,
204 resolution: options.resolution,
205 isPortraitMode: options.isPortraitMode,
206 inputPath: options.concatenatedTsFilePath,
207 type: 'hls-from-ts' as 'hls-from-ts',
212 // Generate an HLS playlist from an input file, and update the master playlist
213 function generateHlsPlaylistResolution (options: {
215 videoInputPath: string
216 resolution: VideoResolution
218 isPortraitMode: boolean
221 return generateHlsPlaylistCommon({
222 video: options.video,
223 resolution: options.resolution,
224 copyCodecs: options.copyCodecs,
225 isPortraitMode: options.isPortraitMode,
226 inputPath: options.videoInputPath,
227 type: 'hls' as 'hls',
232 // ---------------------------------------------------------------------------
235 generateHlsPlaylistResolution,
236 generateHlsPlaylistResolutionFromTS,
237 optimizeOriginalVideofile,
238 transcodeNewWebTorrentResolution,
242 // ---------------------------------------------------------------------------
244 async function onWebTorrentVideoFileTranscoding (
245 video: MVideoFullLight,
246 videoFile: MVideoFile,
247 transcodingPath: string,
250 const stats = await stat(transcodingPath)
251 const fps = await getVideoStreamFPS(transcodingPath)
252 const metadata = await buildFileMetadata(transcodingPath)
254 await move(transcodingPath, outputPath, { overwrite: true })
256 videoFile.size = stats.size
258 videoFile.metadata = metadata
260 await createTorrentAndSetInfoHash(video, videoFile)
262 const oldFile = await VideoFileModel.loadWebTorrentFile({ videoId: video.id, fps: videoFile.fps, resolution: videoFile.resolution })
263 if (oldFile) await video.removeWebTorrentFileAndTorrent(oldFile)
265 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
266 video.VideoFiles = await video.$get('VideoFiles')
268 return { video, videoFile }
271 async function generateHlsPlaylistCommon (options: {
272 type: 'hls' | 'hls-from-ts'
275 resolution: VideoResolution
278 isPortraitMode: boolean
282 const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
283 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
285 const videoTranscodedBasePath = join(transcodeDirectory, type)
286 await ensureDir(videoTranscodedBasePath)
288 const videoFilename = generateHLSVideoFilename(resolution)
289 const resolutionPlaylistFilename = getHlsResolutionPlaylistFilename(videoFilename)
290 const resolutionPlaylistFileTranscodePath = join(videoTranscodedBasePath, resolutionPlaylistFilename)
292 const transcodeOptions = {
296 outputPath: resolutionPlaylistFileTranscodePath,
298 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
299 profile: CONFIG.TRANSCODING.PROFILE,
314 await transcodeVOD(transcodeOptions)
316 // Create or update the playlist
317 const { playlist, oldPlaylistFilename, oldSegmentsSha256Filename } = await retryTransactionWrapper(() => {
318 return sequelizeTypescript.transaction(async transaction => {
319 const playlist = await VideoStreamingPlaylistModel.loadOrGenerate(video, transaction)
321 const oldPlaylistFilename = playlist.playlistFilename
322 const oldSegmentsSha256Filename = playlist.segmentsSha256Filename
324 playlist.playlistFilename = generateHLSMasterPlaylistFilename(video.isLive)
325 playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(video.isLive)
327 playlist.p2pMediaLoaderInfohashes = []
328 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
330 playlist.type = VideoStreamingPlaylistType.HLS
332 await playlist.save({ transaction })
334 return { playlist, oldPlaylistFilename, oldSegmentsSha256Filename }
338 if (oldPlaylistFilename) await video.removeStreamingPlaylistFile(playlist, oldPlaylistFilename)
339 if (oldSegmentsSha256Filename) await video.removeStreamingPlaylistFile(playlist, oldSegmentsSha256Filename)
341 // Build the new playlist file
342 const extname = extnameUtil(videoFilename)
343 const newVideoFile = new VideoFileModel({
347 filename: videoFilename,
349 videoStreamingPlaylistId: playlist.id
352 const videoFilePath = VideoPathManager.Instance.getFSVideoFileOutputPath(playlist, newVideoFile)
354 // Move files from tmp transcoded directory to the appropriate place
355 await ensureDir(VideoPathManager.Instance.getFSHLSOutputPath(video))
357 // Move playlist file
358 const resolutionPlaylistPath = VideoPathManager.Instance.getFSHLSOutputPath(video, resolutionPlaylistFilename)
359 await move(resolutionPlaylistFileTranscodePath, resolutionPlaylistPath, { overwrite: true })
361 await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true })
363 const stats = await stat(videoFilePath)
365 newVideoFile.size = stats.size
366 newVideoFile.fps = await getVideoStreamFPS(videoFilePath)
367 newVideoFile.metadata = await buildFileMetadata(videoFilePath)
369 await createTorrentAndSetInfoHash(playlist, newVideoFile)
371 const oldFile = await VideoFileModel.loadHLSFile({ playlistId: playlist.id, fps: newVideoFile.fps, resolution: newVideoFile.resolution })
372 if (oldFile) await video.removeStreamingPlaylistVideoFile(playlist, oldFile)
374 const savedVideoFile = await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
376 const playlistWithFiles = playlist as MStreamingPlaylistFilesVideo
377 playlistWithFiles.VideoFiles = await playlist.$get('VideoFiles')
378 playlist.assignP2PMediaLoaderInfoHashes(video, playlistWithFiles.VideoFiles)
379 playlist.storage = VideoStorage.FILE_SYSTEM
381 await playlist.save()
383 video.setHLSPlaylist(playlist)
385 await updateMasterHLSPlaylist(video, playlistWithFiles)
386 await updateSha256VODSegments(video, playlistWithFiles)
388 return { resolutionPlaylistPath, videoFile: savedVideoFile }