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 { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
5 import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
6 import { VideoResolution } from '../../../shared/models/videos'
7 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
8 import { transcode, TranscodeOptions, TranscodeOptionsType } from '../../helpers/ffmpeg-utils'
9 import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../../helpers/ffprobe-utils'
10 import { logger } from '../../helpers/logger'
11 import { CONFIG } from '../../initializers/config'
12 import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../../initializers/constants'
13 import { VideoFileModel } from '../../models/video/video-file'
14 import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist'
15 import { updateMasterHLSPlaylist, updateSha256VODSegments } from '../hls'
16 import { generateVideoFilename, generateVideoStreamingPlaylistName, getVideoFilePath } from '../video-paths'
17 import { VideoTranscodingProfilesManager } from './video-transcoding-profiles'
21 * Functions that run transcoding functions, update the database, cleanup files, create torrent files...
22 * Mainly called by the job queue
26 // Optimize the original video file and replace it. The resolution is not changed.
27 async function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVideoFile, job?: Job) {
28 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
29 const newExtname = '.mp4'
31 const videoInputPath = getVideoFilePath(video, inputVideoFile)
32 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
34 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
38 const transcodeOptions: TranscodeOptions = {
41 inputPath: videoInputPath,
42 outputPath: videoTranscodedPath,
44 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
45 profile: CONFIG.TRANSCODING.PROFILE,
47 resolution: inputVideoFile.resolution,
52 // Could be very long!
53 await transcode(transcodeOptions)
56 await remove(videoInputPath)
58 // Important to do this before getVideoFilename() to take in account the new filename
59 inputVideoFile.extname = newExtname
60 inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname)
62 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
64 await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
68 // Auto destruction...
69 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
75 // Transcode the original video file to a lower resolution.
76 async function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: VideoResolution, isPortrait: boolean, job: Job) {
77 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
78 const extname = '.mp4'
80 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
81 const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile())
83 const newVideoFile = new VideoFileModel({
86 filename: generateVideoFilename(video, false, resolution, extname),
91 const videoOutputPath = getVideoFilePath(video, newVideoFile)
92 const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename)
94 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
96 type: 'only-audio' as 'only-audio',
98 inputPath: videoInputPath,
99 outputPath: videoTranscodedPath,
101 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
102 profile: CONFIG.TRANSCODING.PROFILE,
109 type: 'video' as 'video',
110 inputPath: videoInputPath,
111 outputPath: videoTranscodedPath,
113 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
114 profile: CONFIG.TRANSCODING.PROFILE,
117 isPortraitMode: isPortrait,
122 await transcode(transcodeOptions)
124 return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
127 // Merge an image with an audio file to create a video
128 async function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolution, job: Job) {
129 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
130 const newExtname = '.mp4'
132 const inputVideoFile = video.getMinQualityFile()
134 const audioInputPath = getVideoFilePath(video, inputVideoFile)
135 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
137 // If the user updates the video preview during transcoding
138 const previewPath = video.getPreview().getPath()
139 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
140 await copyFile(previewPath, tmpPreviewPath)
142 const transcodeOptions = {
143 type: 'merge-audio' as 'merge-audio',
145 inputPath: tmpPreviewPath,
146 outputPath: videoTranscodedPath,
148 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
149 profile: CONFIG.TRANSCODING.PROFILE,
151 audioPath: audioInputPath,
158 await transcode(transcodeOptions)
160 await remove(audioInputPath)
161 await remove(tmpPreviewPath)
163 await remove(tmpPreviewPath)
167 // Important to do this before getVideoFilename() to take in account the new file extension
168 inputVideoFile.extname = newExtname
169 inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname)
171 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
172 // ffmpeg generated a new video file, so update the video duration
173 // See https://trac.ffmpeg.org/ticket/5456
174 video.duration = await getDurationFromVideoFile(videoTranscodedPath)
177 return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
180 // Concat TS segments from a live video to a fragmented mp4 HLS playlist
181 async function generateHlsPlaylistResolutionFromTS (options: {
182 video: MVideoFullLight
183 concatenatedTsFilePath: string
184 resolution: VideoResolution
185 isPortraitMode: boolean
188 return generateHlsPlaylistCommon({
189 video: options.video,
190 resolution: options.resolution,
191 isPortraitMode: options.isPortraitMode,
192 inputPath: options.concatenatedTsFilePath,
193 type: 'hls-from-ts' as 'hls-from-ts',
198 // Generate an HLS playlist from an input file, and update the master playlist
199 function generateHlsPlaylistResolution (options: {
200 video: MVideoFullLight
201 videoInputPath: string
202 resolution: VideoResolution
204 isPortraitMode: boolean
207 return generateHlsPlaylistCommon({
208 video: options.video,
209 resolution: options.resolution,
210 copyCodecs: options.copyCodecs,
211 isPortraitMode: options.isPortraitMode,
212 inputPath: options.videoInputPath,
213 type: 'hls' as 'hls',
218 // ---------------------------------------------------------------------------
221 generateHlsPlaylistResolution,
222 generateHlsPlaylistResolutionFromTS,
223 optimizeOriginalVideofile,
224 transcodeNewWebTorrentResolution,
228 // ---------------------------------------------------------------------------
230 async function onWebTorrentVideoFileTranscoding (
231 video: MVideoFullLight,
232 videoFile: MVideoFile,
233 transcodingPath: string,
236 const stats = await stat(transcodingPath)
237 const fps = await getVideoFileFPS(transcodingPath)
238 const metadata = await getMetadataFromFile(transcodingPath)
240 await move(transcodingPath, outputPath, { overwrite: true })
242 videoFile.size = stats.size
244 videoFile.metadata = metadata
246 await createTorrentAndSetInfoHash(video, videoFile)
248 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
249 video.VideoFiles = await video.$get('VideoFiles')
254 async function generateHlsPlaylistCommon (options: {
255 type: 'hls' | 'hls-from-ts'
256 video: MVideoFullLight
258 resolution: VideoResolution
261 isPortraitMode: boolean
265 const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
266 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
268 const videoTranscodedBasePath = join(transcodeDirectory, type)
269 await ensureDir(videoTranscodedBasePath)
271 const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
272 const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)
273 const playlistFileTranscodePath = join(videoTranscodedBasePath, playlistFilename)
275 const transcodeOptions = {
279 outputPath: playlistFileTranscodePath,
281 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
282 profile: CONFIG.TRANSCODING.PROFILE,
297 await transcode(transcodeOptions)
299 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
301 // Create or update the playlist
302 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
305 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
306 p2pMediaLoaderInfohashes: [],
307 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
309 type: VideoStreamingPlaylistType.HLS
310 }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
311 videoStreamingPlaylist.Video = video
313 // Build the new playlist file
314 const extname = extnameUtil(videoFilename)
315 const newVideoFile = new VideoFileModel({
319 filename: generateVideoFilename(video, true, resolution, extname),
321 videoStreamingPlaylistId: videoStreamingPlaylist.id
324 const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
326 // Move files from tmp transcoded directory to the appropriate place
327 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
328 await ensureDir(baseHlsDirectory)
330 // Move playlist file
331 const playlistPath = join(baseHlsDirectory, playlistFilename)
332 await move(playlistFileTranscodePath, playlistPath, { overwrite: true })
334 await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true })
336 const stats = await stat(videoFilePath)
338 newVideoFile.size = stats.size
339 newVideoFile.fps = await getVideoFileFPS(videoFilePath)
340 newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
342 await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
344 await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
345 videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
347 videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
348 playlistUrl, videoStreamingPlaylist.VideoFiles
350 await videoStreamingPlaylist.save()
352 video.setHLSPlaylist(videoStreamingPlaylist)
354 await updateMasterHLSPlaylist(video)
355 await updateSha256VODSegments(video)