aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/transcoding/web-transcoding.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-04-21 14:55:10 +0200
committerChocobozzz <chocobozzz@cpy.re>2023-05-09 08:57:34 +0200
commit0c9668f77901e7540e2c7045eb0f2974a4842a69 (patch)
tree226d3dd1565b0bb56588897af3b8530e6216e96b /server/lib/transcoding/web-transcoding.ts
parent6bcb854cdea8688a32240bc5719c7d139806e00b (diff)
downloadPeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.tar.gz
PeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.tar.zst
PeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.zip
Implement remote runner jobs in server
Move ffmpeg functions to @shared
Diffstat (limited to 'server/lib/transcoding/web-transcoding.ts')
-rw-r--r--server/lib/transcoding/web-transcoding.ts273
1 files changed, 273 insertions, 0 deletions
diff --git a/server/lib/transcoding/web-transcoding.ts b/server/lib/transcoding/web-transcoding.ts
new file mode 100644
index 000000000..d43d03b2a
--- /dev/null
+++ b/server/lib/transcoding/web-transcoding.ts
@@ -0,0 +1,273 @@
1import { Job } from 'bullmq'
2import { copyFile, move, remove, stat } from 'fs-extra'
3import { basename, join } from 'path'
4import { computeOutputFPS } from '@server/helpers/ffmpeg'
5import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
6import { MVideoFile, MVideoFullLight } from '@server/types/models'
7import { toEven } from '@shared/core-utils'
8import { ffprobePromise, getVideoStreamDuration, getVideoStreamFPS, TranscodeVODOptionsType } from '@shared/ffmpeg'
9import { VideoResolution, VideoStorage } from '@shared/models'
10import { CONFIG } from '../../initializers/config'
11import { VideoFileModel } from '../../models/video/video-file'
12import { generateWebTorrentVideoFilename } from '../paths'
13import { buildFileMetadata } from '../video-file'
14import { VideoPathManager } from '../video-path-manager'
15import { buildFFmpegVOD } from './shared'
16import { computeResolutionsToTranscode } from './transcoding-resolutions'
17
18// Optimize the original video file and replace it. The resolution is not changed.
19export async function optimizeOriginalVideofile (options: {
20 video: MVideoFullLight
21 inputVideoFile: MVideoFile
22 quickTranscode: boolean
23 job: Job
24}) {
25 const { video, inputVideoFile, quickTranscode, job } = options
26
27 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
28 const newExtname = '.mp4'
29
30 // Will be released by our transcodeVOD function once ffmpeg is ran
31 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
32
33 try {
34 await video.reload()
35
36 const fileWithVideoOrPlaylist = inputVideoFile.withVideoOrPlaylist(video)
37
38 const result = await VideoPathManager.Instance.makeAvailableVideoFile(fileWithVideoOrPlaylist, async videoInputPath => {
39 const videoOutputPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
40
41 const transcodeType: TranscodeVODOptionsType = quickTranscode
42 ? 'quick-transcode'
43 : 'video'
44
45 const resolution = buildOriginalFileResolution(inputVideoFile.resolution)
46 const fps = computeOutputFPS({ inputFPS: inputVideoFile.fps, resolution })
47
48 // Could be very long!
49 await buildFFmpegVOD(job).transcode({
50 type: transcodeType,
51
52 inputPath: videoInputPath,
53 outputPath: videoOutputPath,
54
55 inputFileMutexReleaser,
56
57 resolution,
58 fps
59 })
60
61 // Important to do this before getVideoFilename() to take in account the new filename
62 inputVideoFile.resolution = resolution
63 inputVideoFile.extname = newExtname
64 inputVideoFile.filename = generateWebTorrentVideoFilename(resolution, newExtname)
65 inputVideoFile.storage = VideoStorage.FILE_SYSTEM
66
67 const { videoFile } = await onWebTorrentVideoFileTranscoding({
68 video,
69 videoFile: inputVideoFile,
70 videoOutputPath
71 })
72
73 await remove(videoInputPath)
74
75 return { transcodeType, videoFile }
76 })
77
78 return result
79 } finally {
80 inputFileMutexReleaser()
81 }
82}
83
84// Transcode the original video file to a lower resolution compatible with WebTorrent
85export async function transcodeNewWebTorrentResolution (options: {
86 video: MVideoFullLight
87 resolution: VideoResolution
88 fps: number
89 job: Job
90}) {
91 const { video, resolution, fps, job } = options
92
93 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
94 const newExtname = '.mp4'
95
96 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
97
98 try {
99 await video.reload()
100
101 const file = video.getMaxQualityFile().withVideoOrPlaylist(video)
102
103 const result = await VideoPathManager.Instance.makeAvailableVideoFile(file, async videoInputPath => {
104 const newVideoFile = new VideoFileModel({
105 resolution,
106 extname: newExtname,
107 filename: generateWebTorrentVideoFilename(resolution, newExtname),
108 size: 0,
109 videoId: video.id
110 })
111
112 const videoOutputPath = join(transcodeDirectory, newVideoFile.filename)
113
114 const transcodeOptions = {
115 type: 'video' as 'video',
116
117 inputPath: videoInputPath,
118 outputPath: videoOutputPath,
119
120 inputFileMutexReleaser,
121
122 resolution,
123 fps
124 }
125
126 await buildFFmpegVOD(job).transcode(transcodeOptions)
127
128 return onWebTorrentVideoFileTranscoding({ video, videoFile: newVideoFile, videoOutputPath })
129 })
130
131 return result
132 } finally {
133 inputFileMutexReleaser()
134 }
135}
136
137// Merge an image with an audio file to create a video
138export async function mergeAudioVideofile (options: {
139 video: MVideoFullLight
140 resolution: VideoResolution
141 fps: number
142 job: Job
143}) {
144 const { video, resolution, fps, job } = options
145
146 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
147 const newExtname = '.mp4'
148
149 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
150
151 try {
152 await video.reload()
153
154 const inputVideoFile = video.getMinQualityFile()
155
156 const fileWithVideoOrPlaylist = inputVideoFile.withVideoOrPlaylist(video)
157
158 const result = await VideoPathManager.Instance.makeAvailableVideoFile(fileWithVideoOrPlaylist, async audioInputPath => {
159 const videoOutputPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
160
161 // If the user updates the video preview during transcoding
162 const previewPath = video.getPreview().getPath()
163 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
164 await copyFile(previewPath, tmpPreviewPath)
165
166 const transcodeOptions = {
167 type: 'merge-audio' as 'merge-audio',
168
169 inputPath: tmpPreviewPath,
170 outputPath: videoOutputPath,
171
172 inputFileMutexReleaser,
173
174 audioPath: audioInputPath,
175 resolution,
176 fps
177 }
178
179 try {
180 await buildFFmpegVOD(job).transcode(transcodeOptions)
181
182 await remove(audioInputPath)
183 await remove(tmpPreviewPath)
184 } catch (err) {
185 await remove(tmpPreviewPath)
186 throw err
187 }
188
189 // Important to do this before getVideoFilename() to take in account the new file extension
190 inputVideoFile.extname = newExtname
191 inputVideoFile.resolution = resolution
192 inputVideoFile.filename = generateWebTorrentVideoFilename(inputVideoFile.resolution, newExtname)
193
194 // ffmpeg generated a new video file, so update the video duration
195 // See https://trac.ffmpeg.org/ticket/5456
196 video.duration = await getVideoStreamDuration(videoOutputPath)
197 await video.save()
198
199 return onWebTorrentVideoFileTranscoding({
200 video,
201 videoFile: inputVideoFile,
202 videoOutputPath
203 })
204 })
205
206 return result
207 } finally {
208 inputFileMutexReleaser()
209 }
210}
211
212export async function onWebTorrentVideoFileTranscoding (options: {
213 video: MVideoFullLight
214 videoFile: MVideoFile
215 videoOutputPath: string
216}) {
217 const { video, videoFile, videoOutputPath } = options
218
219 const mutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
220
221 try {
222 await video.reload()
223
224 const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, videoFile)
225
226 const stats = await stat(videoOutputPath)
227
228 const probe = await ffprobePromise(videoOutputPath)
229 const fps = await getVideoStreamFPS(videoOutputPath, probe)
230 const metadata = await buildFileMetadata(videoOutputPath, probe)
231
232 await move(videoOutputPath, 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 const oldFile = await VideoFileModel.loadWebTorrentFile({ videoId: video.id, fps: videoFile.fps, resolution: videoFile.resolution })
241 if (oldFile) await video.removeWebTorrentFile(oldFile)
242
243 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
244 video.VideoFiles = await video.$get('VideoFiles')
245
246 return { video, videoFile }
247 } finally {
248 mutexReleaser()
249 }
250}
251
252// ---------------------------------------------------------------------------
253
254function buildOriginalFileResolution (inputResolution: number) {
255 if (CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION === true) {
256 return toEven(inputResolution)
257 }
258
259 const resolutions = computeResolutionsToTranscode({
260 input: inputResolution,
261 type: 'vod',
262 includeInput: false,
263 strictLower: false,
264 // We don't really care about the audio resolution in this context
265 hasAudio: true
266 })
267
268 if (resolutions.length === 0) {
269 return toEven(inputResolution)
270 }
271
272 return Math.max(...resolutions)
273}