aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/transcoding/transcoding.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-02-11 10:51:33 +0100
committerChocobozzz <chocobozzz@cpy.re>2022-02-28 10:42:19 +0100
commitc729caf6cc34630877a0e5a1bda1719384cd0c8a (patch)
tree1d2e13722e518c73d2c9e6f0969615e29d51cf8c /server/lib/transcoding/transcoding.ts
parenta24bf4dc659cebb65d887862bf21d7a35e9ec791 (diff)
downloadPeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.gz
PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.zst
PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.zip
Add basic video editor support
Diffstat (limited to 'server/lib/transcoding/transcoding.ts')
-rw-r--r--server/lib/transcoding/transcoding.ts373
1 files changed, 373 insertions, 0 deletions
diff --git a/server/lib/transcoding/transcoding.ts b/server/lib/transcoding/transcoding.ts
new file mode 100644
index 000000000..d55364e25
--- /dev/null
+++ b/server/lib/transcoding/transcoding.ts
@@ -0,0 +1,373 @@
1import { Job } from 'bull'
2import { copyFile, ensureDir, move, remove, stat } from 'fs-extra'
3import { basename, extname as extnameUtil, join } from 'path'
4import { toEven } from '@server/helpers/core-utils'
5import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
6import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
7import { VideoResolution, VideoStorage } from '../../../shared/models/videos'
8import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
9import {
10 canDoQuickTranscode,
11 getVideoStreamDuration,
12 buildFileMetadata,
13 getVideoStreamFPS,
14 transcodeVOD,
15 TranscodeVODOptions,
16 TranscodeVODOptionsType
17} from '../../helpers/ffmpeg'
18import { CONFIG } from '../../initializers/config'
19import { P2P_MEDIA_LOADER_PEER_VERSION } from '../../initializers/constants'
20import { VideoFileModel } from '../../models/video/video-file'
21import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist'
22import { updateMasterHLSPlaylist, updateSha256VODSegments } from '../hls'
23import {
24 generateHLSMasterPlaylistFilename,
25 generateHlsSha256SegmentsFilename,
26 generateHLSVideoFilename,
27 generateWebTorrentVideoFilename,
28 getHlsResolutionPlaylistFilename
29} from '../paths'
30import { VideoPathManager } from '../video-path-manager'
31import { VideoTranscodingProfilesManager } from './default-transcoding-profiles'
32
33/**
34 *
35 * Functions that run transcoding functions, update the database, cleanup files, create torrent files...
36 * Mainly called by the job queue
37 *
38 */
39
40// Optimize the original video file and replace it. The resolution is not changed.
41function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVideoFile, job?: Job) {
42 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
43 const newExtname = '.mp4'
44
45 return VideoPathManager.Instance.makeAvailableVideoFile(inputVideoFile.withVideoOrPlaylist(video), async videoInputPath => {
46 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
47
48 const transcodeType: TranscodeVODOptionsType = await canDoQuickTranscode(videoInputPath)
49 ? 'quick-transcode'
50 : 'video'
51
52 const resolution = toEven(inputVideoFile.resolution)
53
54 const transcodeOptions: TranscodeVODOptions = {
55 type: transcodeType,
56
57 inputPath: videoInputPath,
58 outputPath: videoTranscodedPath,
59
60 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
61 profile: CONFIG.TRANSCODING.PROFILE,
62
63 resolution,
64
65 job
66 }
67
68 // Could be very long!
69 await transcodeVOD(transcodeOptions)
70
71 // Important to do this before getVideoFilename() to take in account the new filename
72 inputVideoFile.extname = newExtname
73 inputVideoFile.filename = generateWebTorrentVideoFilename(resolution, newExtname)
74 inputVideoFile.storage = VideoStorage.FILE_SYSTEM
75
76 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, inputVideoFile)
77
78 const { videoFile } = await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
79 await remove(videoInputPath)
80
81 return { transcodeType, videoFile }
82 })
83}
84
85// Transcode the original video file to a lower resolution
86// We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
87function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: VideoResolution, isPortrait: boolean, job: Job) {
88 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
89 const extname = '.mp4'
90
91 return VideoPathManager.Instance.makeAvailableVideoFile(video.getMaxQualityFile().withVideoOrPlaylist(video), async videoInputPath => {
92 const newVideoFile = new VideoFileModel({
93 resolution,
94 extname,
95 filename: generateWebTorrentVideoFilename(resolution, extname),
96 size: 0,
97 videoId: video.id
98 })
99
100 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
101 const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename)
102
103 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
104 ? {
105 type: 'only-audio' as 'only-audio',
106
107 inputPath: videoInputPath,
108 outputPath: videoTranscodedPath,
109
110 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
111 profile: CONFIG.TRANSCODING.PROFILE,
112
113 resolution,
114
115 job
116 }
117 : {
118 type: 'video' as 'video',
119 inputPath: videoInputPath,
120 outputPath: videoTranscodedPath,
121
122 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
123 profile: CONFIG.TRANSCODING.PROFILE,
124
125 resolution,
126 isPortraitMode: isPortrait,
127
128 job
129 }
130
131 await transcodeVOD(transcodeOptions)
132
133 return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
134 })
135}
136
137// Merge an image with an audio file to create a video
138function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolution, job: Job) {
139 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
140 const newExtname = '.mp4'
141
142 const inputVideoFile = video.getMinQualityFile()
143
144 return VideoPathManager.Instance.makeAvailableVideoFile(inputVideoFile.withVideoOrPlaylist(video), async audioInputPath => {
145 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
146
147 // If the user updates the video preview during transcoding
148 const previewPath = video.getPreview().getPath()
149 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
150 await copyFile(previewPath, tmpPreviewPath)
151
152 const transcodeOptions = {
153 type: 'merge-audio' as 'merge-audio',
154
155 inputPath: tmpPreviewPath,
156 outputPath: videoTranscodedPath,
157
158 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
159 profile: CONFIG.TRANSCODING.PROFILE,
160
161 audioPath: audioInputPath,
162 resolution,
163
164 job
165 }
166
167 try {
168 await transcodeVOD(transcodeOptions)
169
170 await remove(audioInputPath)
171 await remove(tmpPreviewPath)
172 } catch (err) {
173 await remove(tmpPreviewPath)
174 throw err
175 }
176
177 // Important to do this before getVideoFilename() to take in account the new file extension
178 inputVideoFile.extname = newExtname
179 inputVideoFile.resolution = resolution
180 inputVideoFile.filename = generateWebTorrentVideoFilename(inputVideoFile.resolution, newExtname)
181
182 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, inputVideoFile)
183 // ffmpeg generated a new video file, so update the video duration
184 // See https://trac.ffmpeg.org/ticket/5456
185 video.duration = await getVideoStreamDuration(videoTranscodedPath)
186 await video.save()
187
188 return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
189 })
190}
191
192// Concat TS segments from a live video to a fragmented mp4 HLS playlist
193async function generateHlsPlaylistResolutionFromTS (options: {
194 video: MVideoFullLight
195 concatenatedTsFilePath: string
196 resolution: VideoResolution
197 isPortraitMode: boolean
198 isAAC: boolean
199}) {
200 return generateHlsPlaylistCommon({
201 video: options.video,
202 resolution: options.resolution,
203 isPortraitMode: options.isPortraitMode,
204 inputPath: options.concatenatedTsFilePath,
205 type: 'hls-from-ts' as 'hls-from-ts',
206 isAAC: options.isAAC
207 })
208}
209
210// Generate an HLS playlist from an input file, and update the master playlist
211function generateHlsPlaylistResolution (options: {
212 video: MVideoFullLight
213 videoInputPath: string
214 resolution: VideoResolution
215 copyCodecs: boolean
216 isPortraitMode: boolean
217 job?: Job
218}) {
219 return generateHlsPlaylistCommon({
220 video: options.video,
221 resolution: options.resolution,
222 copyCodecs: options.copyCodecs,
223 isPortraitMode: options.isPortraitMode,
224 inputPath: options.videoInputPath,
225 type: 'hls' as 'hls',
226 job: options.job
227 })
228}
229
230// ---------------------------------------------------------------------------
231
232export {
233 generateHlsPlaylistResolution,
234 generateHlsPlaylistResolutionFromTS,
235 optimizeOriginalVideofile,
236 transcodeNewWebTorrentResolution,
237 mergeAudioVideofile
238}
239
240// ---------------------------------------------------------------------------
241
242async function onWebTorrentVideoFileTranscoding (
243 video: MVideoFullLight,
244 videoFile: MVideoFile,
245 transcodingPath: string,
246 outputPath: string
247) {
248 const stats = await stat(transcodingPath)
249 const fps = await getVideoStreamFPS(transcodingPath)
250 const metadata = await buildFileMetadata(transcodingPath)
251
252 await move(transcodingPath, outputPath, { overwrite: true })
253
254 videoFile.size = stats.size
255 videoFile.fps = fps
256 videoFile.metadata = metadata
257
258 await createTorrentAndSetInfoHash(video, videoFile)
259
260 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
261 video.VideoFiles = await video.$get('VideoFiles')
262
263 return { video, videoFile }
264}
265
266async function generateHlsPlaylistCommon (options: {
267 type: 'hls' | 'hls-from-ts'
268 video: MVideoFullLight
269 inputPath: string
270 resolution: VideoResolution
271 copyCodecs?: boolean
272 isAAC?: boolean
273 isPortraitMode: boolean
274
275 job?: Job
276}) {
277 const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
278 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
279
280 const videoTranscodedBasePath = join(transcodeDirectory, type)
281 await ensureDir(videoTranscodedBasePath)
282
283 const videoFilename = generateHLSVideoFilename(resolution)
284 const resolutionPlaylistFilename = getHlsResolutionPlaylistFilename(videoFilename)
285 const resolutionPlaylistFileTranscodePath = join(videoTranscodedBasePath, resolutionPlaylistFilename)
286
287 const transcodeOptions = {
288 type,
289
290 inputPath,
291 outputPath: resolutionPlaylistFileTranscodePath,
292
293 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
294 profile: CONFIG.TRANSCODING.PROFILE,
295
296 resolution,
297 copyCodecs,
298 isPortraitMode,
299
300 isAAC,
301
302 hlsPlaylist: {
303 videoFilename
304 },
305
306 job
307 }
308
309 await transcodeVOD(transcodeOptions)
310
311 // Create or update the playlist
312 const playlist = await VideoStreamingPlaylistModel.loadOrGenerate(video)
313
314 if (!playlist.playlistFilename) {
315 playlist.playlistFilename = generateHLSMasterPlaylistFilename(video.isLive)
316 }
317
318 if (!playlist.segmentsSha256Filename) {
319 playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(video.isLive)
320 }
321
322 playlist.p2pMediaLoaderInfohashes = []
323 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
324
325 playlist.type = VideoStreamingPlaylistType.HLS
326
327 await playlist.save()
328
329 // Build the new playlist file
330 const extname = extnameUtil(videoFilename)
331 const newVideoFile = new VideoFileModel({
332 resolution,
333 extname,
334 size: 0,
335 filename: videoFilename,
336 fps: -1,
337 videoStreamingPlaylistId: playlist.id
338 })
339
340 const videoFilePath = VideoPathManager.Instance.getFSVideoFileOutputPath(playlist, newVideoFile)
341
342 // Move files from tmp transcoded directory to the appropriate place
343 await ensureDir(VideoPathManager.Instance.getFSHLSOutputPath(video))
344
345 // Move playlist file
346 const resolutionPlaylistPath = VideoPathManager.Instance.getFSHLSOutputPath(video, resolutionPlaylistFilename)
347 await move(resolutionPlaylistFileTranscodePath, resolutionPlaylistPath, { overwrite: true })
348 // Move video file
349 await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true })
350
351 const stats = await stat(videoFilePath)
352
353 newVideoFile.size = stats.size
354 newVideoFile.fps = await getVideoStreamFPS(videoFilePath)
355 newVideoFile.metadata = await buildFileMetadata(videoFilePath)
356
357 await createTorrentAndSetInfoHash(playlist, newVideoFile)
358
359 const savedVideoFile = await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
360
361 const playlistWithFiles = playlist as MStreamingPlaylistFilesVideo
362 playlistWithFiles.VideoFiles = await playlist.$get('VideoFiles')
363 playlist.assignP2PMediaLoaderInfoHashes(video, playlistWithFiles.VideoFiles)
364
365 await playlist.save()
366
367 video.setHLSPlaylist(playlist)
368
369 await updateMasterHLSPlaylist(video, playlistWithFiles)
370 await updateSha256VODSegments(video, playlistWithFiles)
371
372 return { resolutionPlaylistPath, videoFile: savedVideoFile }
373}