]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-transcoding.ts
stricter youtubedl format selectors (#3516)
[github/Chocobozzz/PeerTube.git] / server / lib / video-transcoding.ts
1 import { copyFile, ensureDir, move, remove, stat } from 'fs-extra'
2 import { basename, extname as extnameUtil, join } from 'path'
3 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
4 import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/types/models'
5 import { VideoResolution } from '../../shared/models/videos'
6 import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
7 import { transcode, TranscodeOptions, TranscodeOptionsType } from '../helpers/ffmpeg-utils'
8 import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../helpers/ffprobe-utils'
9 import { logger } from '../helpers/logger'
10 import { CONFIG } from '../initializers/config'
11 import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
12 import { VideoFileModel } from '../models/video/video-file'
13 import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
14 import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls'
15 import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths'
16 import { availableEncoders } from './video-transcoding-profiles'
17
18 /**
19 *
20 * Functions that run transcoding functions, update the database, cleanup files, create torrent files...
21 * Mainly called by the job queue
22 *
23 */
24
25 // Optimize the original video file and replace it. The resolution is not changed.
26 async function optimizeOriginalVideofile (video: MVideoWithFile, inputVideoFileArg?: MVideoFile) {
27 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
28 const newExtname = '.mp4'
29
30 const inputVideoFile = inputVideoFileArg || video.getMaxQualityFile()
31 const videoInputPath = getVideoFilePath(video, inputVideoFile)
32 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
33
34 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
35 ? 'quick-transcode'
36 : 'video'
37
38 const transcodeOptions: TranscodeOptions = {
39 type: transcodeType,
40
41 inputPath: videoInputPath,
42 outputPath: videoTranscodedPath,
43
44 availableEncoders,
45 profile: 'default',
46
47 resolution: inputVideoFile.resolution
48 }
49
50 // Could be very long!
51 await transcode(transcodeOptions)
52
53 try {
54 await remove(videoInputPath)
55
56 // Important to do this before getVideoFilename() to take in account the new file extension
57 inputVideoFile.extname = newExtname
58
59 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
60
61 await onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
62
63 return transcodeType
64 } catch (err) {
65 // Auto destruction...
66 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
67
68 throw err
69 }
70 }
71
72 // Transcode the original video file to a lower resolution.
73 async function transcodeNewResolution (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean) {
74 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
75 const extname = '.mp4'
76
77 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
78 const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile())
79
80 const newVideoFile = new VideoFileModel({
81 resolution,
82 extname,
83 size: 0,
84 videoId: video.id
85 })
86 const videoOutputPath = getVideoFilePath(video, newVideoFile)
87 const videoTranscodedPath = join(transcodeDirectory, getVideoFilename(video, newVideoFile))
88
89 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
90 ? {
91 type: 'only-audio' as 'only-audio',
92
93 inputPath: videoInputPath,
94 outputPath: videoTranscodedPath,
95
96 availableEncoders,
97 profile: 'default',
98
99 resolution
100 }
101 : {
102 type: 'video' as 'video',
103 inputPath: videoInputPath,
104 outputPath: videoTranscodedPath,
105
106 availableEncoders,
107 profile: 'default',
108
109 resolution,
110 isPortraitMode: isPortrait
111 }
112
113 await transcode(transcodeOptions)
114
115 return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
116 }
117
118 // Merge an image with an audio file to create a video
119 async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution) {
120 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
121 const newExtname = '.mp4'
122
123 const inputVideoFile = video.getMinQualityFile()
124
125 const audioInputPath = getVideoFilePath(video, inputVideoFile)
126 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
127
128 // If the user updates the video preview during transcoding
129 const previewPath = video.getPreview().getPath()
130 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
131 await copyFile(previewPath, tmpPreviewPath)
132
133 const transcodeOptions = {
134 type: 'merge-audio' as 'merge-audio',
135
136 inputPath: tmpPreviewPath,
137 outputPath: videoTranscodedPath,
138
139 availableEncoders,
140 profile: 'default',
141
142 audioPath: audioInputPath,
143 resolution
144 }
145
146 try {
147 await transcode(transcodeOptions)
148
149 await remove(audioInputPath)
150 await remove(tmpPreviewPath)
151 } catch (err) {
152 await remove(tmpPreviewPath)
153 throw err
154 }
155
156 // Important to do this before getVideoFilename() to take in account the new file extension
157 inputVideoFile.extname = newExtname
158
159 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
160 // ffmpeg generated a new video file, so update the video duration
161 // See https://trac.ffmpeg.org/ticket/5456
162 video.duration = await getDurationFromVideoFile(videoTranscodedPath)
163 await video.save()
164
165 return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
166 }
167
168 // Concat TS segments from a live video to a fragmented mp4 HLS playlist
169 async function generateHlsPlaylistFromTS (options: {
170 video: MVideoWithFile
171 concatenatedTsFilePath: string
172 resolution: VideoResolution
173 isPortraitMode: boolean
174 isAAC: boolean
175 }) {
176 return generateHlsPlaylistCommon({
177 video: options.video,
178 resolution: options.resolution,
179 isPortraitMode: options.isPortraitMode,
180 inputPath: options.concatenatedTsFilePath,
181 type: 'hls-from-ts' as 'hls-from-ts',
182 isAAC: options.isAAC
183 })
184 }
185
186 // Generate an HLS playlist from an input file, and update the master playlist
187 function generateHlsPlaylist (options: {
188 video: MVideoWithFile
189 videoInputPath: string
190 resolution: VideoResolution
191 copyCodecs: boolean
192 isPortraitMode: boolean
193 }) {
194 return generateHlsPlaylistCommon({
195 video: options.video,
196 resolution: options.resolution,
197 copyCodecs: options.copyCodecs,
198 isPortraitMode: options.isPortraitMode,
199 inputPath: options.videoInputPath,
200 type: 'hls' as 'hls'
201 })
202 }
203
204 function getEnabledResolutions (type: 'vod' | 'live') {
205 const transcoding = type === 'vod'
206 ? CONFIG.TRANSCODING
207 : CONFIG.LIVE.TRANSCODING
208
209 return Object.keys(transcoding.RESOLUTIONS)
210 .filter(key => transcoding.ENABLED && transcoding.RESOLUTIONS[key] === true)
211 .map(r => parseInt(r, 10))
212 }
213
214 // ---------------------------------------------------------------------------
215
216 export {
217 generateHlsPlaylist,
218 generateHlsPlaylistFromTS,
219 optimizeOriginalVideofile,
220 transcodeNewResolution,
221 mergeAudioVideofile,
222 getEnabledResolutions
223 }
224
225 // ---------------------------------------------------------------------------
226
227 async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) {
228 const stats = await stat(transcodingPath)
229 const fps = await getVideoFileFPS(transcodingPath)
230 const metadata = await getMetadataFromFile(transcodingPath)
231
232 await move(transcodingPath, outputPath, { overwrite: true })
233
234 videoFile.size = stats.size
235 videoFile.fps = fps
236 videoFile.metadata = metadata
237
238 await createTorrentAndSetInfoHash(video, videoFile)
239
240 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
241 video.VideoFiles = await video.$get('VideoFiles')
242
243 return video
244 }
245
246 async function generateHlsPlaylistCommon (options: {
247 type: 'hls' | 'hls-from-ts'
248 video: MVideoWithFile
249 inputPath: string
250 resolution: VideoResolution
251 copyCodecs?: boolean
252 isAAC?: boolean
253 isPortraitMode: boolean
254 }) {
255 const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC } = options
256
257 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
258 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
259
260 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
261 const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
262
263 const transcodeOptions = {
264 type,
265
266 inputPath,
267 outputPath,
268
269 availableEncoders,
270 profile: 'default',
271
272 resolution,
273 copyCodecs,
274 isPortraitMode,
275
276 isAAC,
277
278 hlsPlaylist: {
279 videoFilename
280 }
281 }
282
283 await transcode(transcodeOptions)
284
285 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
286
287 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
288 videoId: video.id,
289 playlistUrl,
290 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
291 p2pMediaLoaderInfohashes: [],
292 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
293
294 type: VideoStreamingPlaylistType.HLS
295 }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
296 videoStreamingPlaylist.Video = video
297
298 const newVideoFile = new VideoFileModel({
299 resolution,
300 extname: extnameUtil(videoFilename),
301 size: 0,
302 fps: -1,
303 videoStreamingPlaylistId: videoStreamingPlaylist.id
304 })
305
306 const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
307 const stats = await stat(videoFilePath)
308
309 newVideoFile.size = stats.size
310 newVideoFile.fps = await getVideoFileFPS(videoFilePath)
311 newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
312
313 await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
314
315 await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
316 videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
317
318 videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
319 playlistUrl, videoStreamingPlaylist.VideoFiles
320 )
321 await videoStreamingPlaylist.save()
322
323 video.setHLSPlaylist(videoStreamingPlaylist)
324
325 await updateMasterHLSPlaylist(video)
326 await updateSha256VODSegments(video)
327
328 return outputPath
329 }