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, MVideoFile, MVideoFullLight } from '@server/types/models'
7 import { VideoResolution } from '../../../shared/models/videos'
8 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
9 import { transcode, TranscodeOptions, TranscodeOptionsType } from '../../helpers/ffmpeg-utils'
10 import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../../helpers/ffprobe-utils'
11 import { logger } from '../../helpers/logger'
12 import { CONFIG } from '../../initializers/config'
13 import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../../initializers/constants'
14 import { VideoFileModel } from '../../models/video/video-file'
15 import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist'
16 import { updateMasterHLSPlaylist, updateSha256VODSegments } from '../hls'
17 import { generateVideoFilename, generateVideoStreamingPlaylistName, getVideoFilePath } from '../video-paths'
18 import { VideoTranscodingProfilesManager } from './video-transcoding-profiles'
22 * Functions that run transcoding functions, update the database, cleanup files, create torrent files...
23 * Mainly called by the job queue
27 // Optimize the original video file and replace it. The resolution is not changed.
28 async function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVideoFile, job?: Job) {
29 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
30 const newExtname = '.mp4'
32 const videoInputPath = getVideoFilePath(video, inputVideoFile)
33 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
35 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
39 const resolution = toEven(inputVideoFile.resolution)
41 const transcodeOptions: TranscodeOptions = {
44 inputPath: videoInputPath,
45 outputPath: videoTranscodedPath,
47 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
48 profile: CONFIG.TRANSCODING.PROFILE,
55 // Could be very long!
56 await transcode(transcodeOptions)
59 await remove(videoInputPath)
61 // Important to do this before getVideoFilename() to take in account the new filename
62 inputVideoFile.extname = newExtname
63 inputVideoFile.filename = generateVideoFilename(video, false, resolution, newExtname)
65 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
67 await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
71 // Auto destruction...
72 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
78 // Transcode the original video file to a lower resolution.
79 async function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: VideoResolution, isPortrait: boolean, job: Job) {
80 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
81 const extname = '.mp4'
83 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
84 const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile())
86 const newVideoFile = new VideoFileModel({
89 filename: generateVideoFilename(video, false, resolution, extname),
94 const videoOutputPath = getVideoFilePath(video, newVideoFile)
95 const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename)
97 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
99 type: 'only-audio' as 'only-audio',
101 inputPath: videoInputPath,
102 outputPath: videoTranscodedPath,
104 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
105 profile: CONFIG.TRANSCODING.PROFILE,
112 type: 'video' as 'video',
113 inputPath: videoInputPath,
114 outputPath: videoTranscodedPath,
116 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
117 profile: CONFIG.TRANSCODING.PROFILE,
120 isPortraitMode: isPortrait,
125 await transcode(transcodeOptions)
127 return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
130 // Merge an image with an audio file to create a video
131 async function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolution, job: Job) {
132 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
133 const newExtname = '.mp4'
135 const inputVideoFile = video.getMinQualityFile()
137 const audioInputPath = getVideoFilePath(video, inputVideoFile)
138 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
140 // If the user updates the video preview during transcoding
141 const previewPath = video.getPreview().getPath()
142 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
143 await copyFile(previewPath, tmpPreviewPath)
145 const transcodeOptions = {
146 type: 'merge-audio' as 'merge-audio',
148 inputPath: tmpPreviewPath,
149 outputPath: videoTranscodedPath,
151 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
152 profile: CONFIG.TRANSCODING.PROFILE,
154 audioPath: audioInputPath,
161 await transcode(transcodeOptions)
163 await remove(audioInputPath)
164 await remove(tmpPreviewPath)
166 await remove(tmpPreviewPath)
170 // Important to do this before getVideoFilename() to take in account the new file extension
171 inputVideoFile.extname = newExtname
172 inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname)
174 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
175 // ffmpeg generated a new video file, so update the video duration
176 // See https://trac.ffmpeg.org/ticket/5456
177 video.duration = await getDurationFromVideoFile(videoTranscodedPath)
180 return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
183 // Concat TS segments from a live video to a fragmented mp4 HLS playlist
184 async function generateHlsPlaylistResolutionFromTS (options: {
185 video: MVideoFullLight
186 concatenatedTsFilePath: string
187 resolution: VideoResolution
188 isPortraitMode: boolean
191 return generateHlsPlaylistCommon({
192 video: options.video,
193 resolution: options.resolution,
194 isPortraitMode: options.isPortraitMode,
195 inputPath: options.concatenatedTsFilePath,
196 type: 'hls-from-ts' as 'hls-from-ts',
201 // Generate an HLS playlist from an input file, and update the master playlist
202 function generateHlsPlaylistResolution (options: {
203 video: MVideoFullLight
204 videoInputPath: string
205 resolution: VideoResolution
207 isPortraitMode: boolean
210 return generateHlsPlaylistCommon({
211 video: options.video,
212 resolution: options.resolution,
213 copyCodecs: options.copyCodecs,
214 isPortraitMode: options.isPortraitMode,
215 inputPath: options.videoInputPath,
216 type: 'hls' as 'hls',
221 // ---------------------------------------------------------------------------
224 generateHlsPlaylistResolution,
225 generateHlsPlaylistResolutionFromTS,
226 optimizeOriginalVideofile,
227 transcodeNewWebTorrentResolution,
231 // ---------------------------------------------------------------------------
233 async function onWebTorrentVideoFileTranscoding (
234 video: MVideoFullLight,
235 videoFile: MVideoFile,
236 transcodingPath: string,
239 const stats = await stat(transcodingPath)
240 const fps = await getVideoFileFPS(transcodingPath)
241 const metadata = await getMetadataFromFile(transcodingPath)
243 await move(transcodingPath, outputPath, { overwrite: true })
245 videoFile.size = stats.size
247 videoFile.metadata = metadata
249 await createTorrentAndSetInfoHash(video, videoFile)
251 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
252 video.VideoFiles = await video.$get('VideoFiles')
257 async function generateHlsPlaylistCommon (options: {
258 type: 'hls' | 'hls-from-ts'
259 video: MVideoFullLight
261 resolution: VideoResolution
264 isPortraitMode: boolean
268 const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
269 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
271 const videoTranscodedBasePath = join(transcodeDirectory, type)
272 await ensureDir(videoTranscodedBasePath)
274 const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
275 const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)
276 const playlistFileTranscodePath = join(videoTranscodedBasePath, playlistFilename)
278 const transcodeOptions = {
282 outputPath: playlistFileTranscodePath,
284 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
285 profile: CONFIG.TRANSCODING.PROFILE,
300 await transcode(transcodeOptions)
302 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
304 // Create or update the playlist
305 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
308 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
309 p2pMediaLoaderInfohashes: [],
310 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
312 type: VideoStreamingPlaylistType.HLS
313 }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
314 videoStreamingPlaylist.Video = video
316 // Build the new playlist file
317 const extname = extnameUtil(videoFilename)
318 const newVideoFile = new VideoFileModel({
322 filename: generateVideoFilename(video, true, resolution, extname),
324 videoStreamingPlaylistId: videoStreamingPlaylist.id
327 const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
329 // Move files from tmp transcoded directory to the appropriate place
330 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
331 await ensureDir(baseHlsDirectory)
333 // Move playlist file
334 const playlistPath = join(baseHlsDirectory, playlistFilename)
335 await move(playlistFileTranscodePath, playlistPath, { overwrite: true })
337 await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true })
339 const stats = await stat(videoFilePath)
341 newVideoFile.size = stats.size
342 newVideoFile.fps = await getVideoFileFPS(videoFilePath)
343 newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
345 await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
347 await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
348 videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
350 videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
351 playlistUrl, videoStreamingPlaylist.VideoFiles
353 await videoStreamingPlaylist.save()
355 video.setHLSPlaylist(videoStreamingPlaylist)
357 await updateMasterHLSPlaylist(video)
358 await updateSha256VODSegments(video)