]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/transcoding/video-transcoding.ts
Introduce contact form command
[github/Chocobozzz/PeerTube.git] / server / lib / transcoding / video-transcoding.ts
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'
19
20 /**
21 *
22 * Functions that run transcoding functions, update the database, cleanup files, create torrent files...
23 * Mainly called by the job queue
24 *
25 */
26
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'
31
32 const videoInputPath = getVideoFilePath(video, inputVideoFile)
33 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
34
35 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
36 ? 'quick-transcode'
37 : 'video'
38
39 const resolution = toEven(inputVideoFile.resolution)
40
41 const transcodeOptions: TranscodeOptions = {
42 type: transcodeType,
43
44 inputPath: videoInputPath,
45 outputPath: videoTranscodedPath,
46
47 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
48 profile: CONFIG.TRANSCODING.PROFILE,
49
50 resolution,
51
52 job
53 }
54
55 // Could be very long!
56 await transcode(transcodeOptions)
57
58 try {
59 await remove(videoInputPath)
60
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)
64
65 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
66
67 await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
68
69 return transcodeType
70 } catch (err) {
71 // Auto destruction...
72 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
73
74 throw err
75 }
76 }
77
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'
82
83 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
84 const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile())
85
86 const newVideoFile = new VideoFileModel({
87 resolution,
88 extname,
89 filename: generateVideoFilename(video, false, resolution, extname),
90 size: 0,
91 videoId: video.id
92 })
93
94 const videoOutputPath = getVideoFilePath(video, newVideoFile)
95 const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename)
96
97 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
98 ? {
99 type: 'only-audio' as 'only-audio',
100
101 inputPath: videoInputPath,
102 outputPath: videoTranscodedPath,
103
104 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
105 profile: CONFIG.TRANSCODING.PROFILE,
106
107 resolution,
108
109 job
110 }
111 : {
112 type: 'video' as 'video',
113 inputPath: videoInputPath,
114 outputPath: videoTranscodedPath,
115
116 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
117 profile: CONFIG.TRANSCODING.PROFILE,
118
119 resolution,
120 isPortraitMode: isPortrait,
121
122 job
123 }
124
125 await transcode(transcodeOptions)
126
127 return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
128 }
129
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'
134
135 const inputVideoFile = video.getMinQualityFile()
136
137 const audioInputPath = getVideoFilePath(video, inputVideoFile)
138 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
139
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)
144
145 const transcodeOptions = {
146 type: 'merge-audio' as 'merge-audio',
147
148 inputPath: tmpPreviewPath,
149 outputPath: videoTranscodedPath,
150
151 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
152 profile: CONFIG.TRANSCODING.PROFILE,
153
154 audioPath: audioInputPath,
155 resolution,
156
157 job
158 }
159
160 try {
161 await transcode(transcodeOptions)
162
163 await remove(audioInputPath)
164 await remove(tmpPreviewPath)
165 } catch (err) {
166 await remove(tmpPreviewPath)
167 throw err
168 }
169
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)
173
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)
178 await video.save()
179
180 return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
181 }
182
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
189 isAAC: boolean
190 }) {
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',
197 isAAC: options.isAAC
198 })
199 }
200
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
206 copyCodecs: boolean
207 isPortraitMode: boolean
208 job?: Job
209 }) {
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',
217 job: options.job
218 })
219 }
220
221 // ---------------------------------------------------------------------------
222
223 export {
224 generateHlsPlaylistResolution,
225 generateHlsPlaylistResolutionFromTS,
226 optimizeOriginalVideofile,
227 transcodeNewWebTorrentResolution,
228 mergeAudioVideofile
229 }
230
231 // ---------------------------------------------------------------------------
232
233 async function onWebTorrentVideoFileTranscoding (
234 video: MVideoFullLight,
235 videoFile: MVideoFile,
236 transcodingPath: string,
237 outputPath: string
238 ) {
239 const stats = await stat(transcodingPath)
240 const fps = await getVideoFileFPS(transcodingPath)
241 const metadata = await getMetadataFromFile(transcodingPath)
242
243 await move(transcodingPath, outputPath, { overwrite: true })
244
245 videoFile.size = stats.size
246 videoFile.fps = fps
247 videoFile.metadata = metadata
248
249 await createTorrentAndSetInfoHash(video, videoFile)
250
251 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
252 video.VideoFiles = await video.$get('VideoFiles')
253
254 return video
255 }
256
257 async function generateHlsPlaylistCommon (options: {
258 type: 'hls' | 'hls-from-ts'
259 video: MVideoFullLight
260 inputPath: string
261 resolution: VideoResolution
262 copyCodecs?: boolean
263 isAAC?: boolean
264 isPortraitMode: boolean
265
266 job?: Job
267 }) {
268 const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
269 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
270
271 const videoTranscodedBasePath = join(transcodeDirectory, type)
272 await ensureDir(videoTranscodedBasePath)
273
274 const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
275 const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)
276 const playlistFileTranscodePath = join(videoTranscodedBasePath, playlistFilename)
277
278 const transcodeOptions = {
279 type,
280
281 inputPath,
282 outputPath: playlistFileTranscodePath,
283
284 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
285 profile: CONFIG.TRANSCODING.PROFILE,
286
287 resolution,
288 copyCodecs,
289 isPortraitMode,
290
291 isAAC,
292
293 hlsPlaylist: {
294 videoFilename
295 },
296
297 job
298 }
299
300 await transcode(transcodeOptions)
301
302 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
303
304 // Create or update the playlist
305 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
306 videoId: video.id,
307 playlistUrl,
308 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
309 p2pMediaLoaderInfohashes: [],
310 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
311
312 type: VideoStreamingPlaylistType.HLS
313 }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
314 videoStreamingPlaylist.Video = video
315
316 // Build the new playlist file
317 const extname = extnameUtil(videoFilename)
318 const newVideoFile = new VideoFileModel({
319 resolution,
320 extname,
321 size: 0,
322 filename: generateVideoFilename(video, true, resolution, extname),
323 fps: -1,
324 videoStreamingPlaylistId: videoStreamingPlaylist.id
325 })
326
327 const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
328
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)
332
333 // Move playlist file
334 const playlistPath = join(baseHlsDirectory, playlistFilename)
335 await move(playlistFileTranscodePath, playlistPath, { overwrite: true })
336 // Move video file
337 await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true })
338
339 const stats = await stat(videoFilePath)
340
341 newVideoFile.size = stats.size
342 newVideoFile.fps = await getVideoFileFPS(videoFilePath)
343 newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
344
345 await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
346
347 await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
348 videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
349
350 videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
351 playlistUrl, videoStreamingPlaylist.VideoFiles
352 )
353 await videoStreamingPlaylist.save()
354
355 video.setHLSPlaylist(videoStreamingPlaylist)
356
357 await updateMasterHLSPlaylist(video)
358 await updateSha256VODSegments(video)
359
360 return playlistPath
361 }