diff options
Diffstat (limited to 'server/lib/transcoding')
-rw-r--r-- | server/lib/transcoding/video-transcoding-profiles.ts | 256 | ||||
-rw-r--r-- | server/lib/transcoding/video-transcoding.ts | 358 |
2 files changed, 614 insertions, 0 deletions
diff --git a/server/lib/transcoding/video-transcoding-profiles.ts b/server/lib/transcoding/video-transcoding-profiles.ts new file mode 100644 index 000000000..c5ea72a5f --- /dev/null +++ b/server/lib/transcoding/video-transcoding-profiles.ts | |||
@@ -0,0 +1,256 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { AvailableEncoders, EncoderOptionsBuilder, getTargetBitrate, VideoResolution } from '../../../shared/models/videos' | ||
3 | import { buildStreamSuffix, resetSupportedEncoders } from '../../helpers/ffmpeg-utils' | ||
4 | import { | ||
5 | canDoQuickAudioTranscode, | ||
6 | ffprobePromise, | ||
7 | getAudioStream, | ||
8 | getMaxAudioBitrate, | ||
9 | getVideoFileBitrate, | ||
10 | getVideoStreamFromFile | ||
11 | } from '../../helpers/ffprobe-utils' | ||
12 | import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants' | ||
13 | |||
14 | /** | ||
15 | * | ||
16 | * Available encoders and profiles for the transcoding jobs | ||
17 | * These functions are used by ffmpeg-utils that will get the encoders and options depending on the chosen profile | ||
18 | * | ||
19 | */ | ||
20 | |||
21 | // Resources: | ||
22 | // * https://slhck.info/video/2017/03/01/rate-control.html | ||
23 | // * https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate | ||
24 | |||
25 | const defaultX264VODOptionsBuilder: EncoderOptionsBuilder = async ({ input, resolution, fps }) => { | ||
26 | const targetBitrate = await buildTargetBitrate({ input, resolution, fps }) | ||
27 | if (!targetBitrate) return { outputOptions: [ ] } | ||
28 | |||
29 | return { | ||
30 | outputOptions: [ | ||
31 | `-preset veryfast`, | ||
32 | `-r ${fps}`, | ||
33 | `-maxrate ${targetBitrate}`, | ||
34 | `-bufsize ${targetBitrate * 2}` | ||
35 | ] | ||
36 | } | ||
37 | } | ||
38 | |||
39 | const defaultX264LiveOptionsBuilder: EncoderOptionsBuilder = async ({ resolution, fps, streamNum }) => { | ||
40 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | ||
41 | |||
42 | return { | ||
43 | outputOptions: [ | ||
44 | `-preset veryfast`, | ||
45 | `${buildStreamSuffix('-r:v', streamNum)} ${fps}`, | ||
46 | `${buildStreamSuffix('-b:v', streamNum)} ${targetBitrate}`, | ||
47 | `-maxrate ${targetBitrate}`, | ||
48 | `-bufsize ${targetBitrate * 2}` | ||
49 | ] | ||
50 | } | ||
51 | } | ||
52 | |||
53 | const defaultAACOptionsBuilder: EncoderOptionsBuilder = async ({ input, streamNum }) => { | ||
54 | const probe = await ffprobePromise(input) | ||
55 | |||
56 | if (await canDoQuickAudioTranscode(input, probe)) { | ||
57 | logger.debug('Copy audio stream %s by AAC encoder.', input) | ||
58 | return { copy: true, outputOptions: [ ] } | ||
59 | } | ||
60 | |||
61 | const parsedAudio = await getAudioStream(input, probe) | ||
62 | |||
63 | // We try to reduce the ceiling bitrate by making rough matches of bitrates | ||
64 | // Of course this is far from perfect, but it might save some space in the end | ||
65 | |||
66 | const audioCodecName = parsedAudio.audioStream['codec_name'] | ||
67 | |||
68 | const bitrate = getMaxAudioBitrate(audioCodecName, parsedAudio.bitrate) | ||
69 | |||
70 | logger.debug('Calculating audio bitrate of %s by AAC encoder.', input, { bitrate: parsedAudio.bitrate, audioCodecName }) | ||
71 | |||
72 | if (bitrate !== undefined && bitrate !== -1) { | ||
73 | return { outputOptions: [ buildStreamSuffix('-b:a', streamNum), bitrate + 'k' ] } | ||
74 | } | ||
75 | |||
76 | return { outputOptions: [ ] } | ||
77 | } | ||
78 | |||
79 | const defaultLibFDKAACVODOptionsBuilder: EncoderOptionsBuilder = ({ streamNum }) => { | ||
80 | return { outputOptions: [ buildStreamSuffix('-q:a', streamNum), '5' ] } | ||
81 | } | ||
82 | |||
83 | // Used to get and update available encoders | ||
84 | class VideoTranscodingProfilesManager { | ||
85 | private static instance: VideoTranscodingProfilesManager | ||
86 | |||
87 | // 1 === less priority | ||
88 | private readonly encodersPriorities = { | ||
89 | vod: this.buildDefaultEncodersPriorities(), | ||
90 | live: this.buildDefaultEncodersPriorities() | ||
91 | } | ||
92 | |||
93 | private readonly availableEncoders = { | ||
94 | vod: { | ||
95 | libx264: { | ||
96 | default: defaultX264VODOptionsBuilder | ||
97 | }, | ||
98 | aac: { | ||
99 | default: defaultAACOptionsBuilder | ||
100 | }, | ||
101 | libfdk_aac: { | ||
102 | default: defaultLibFDKAACVODOptionsBuilder | ||
103 | } | ||
104 | }, | ||
105 | live: { | ||
106 | libx264: { | ||
107 | default: defaultX264LiveOptionsBuilder | ||
108 | }, | ||
109 | aac: { | ||
110 | default: defaultAACOptionsBuilder | ||
111 | } | ||
112 | } | ||
113 | } | ||
114 | |||
115 | private availableProfiles = { | ||
116 | vod: [] as string[], | ||
117 | live: [] as string[] | ||
118 | } | ||
119 | |||
120 | private constructor () { | ||
121 | this.buildAvailableProfiles() | ||
122 | } | ||
123 | |||
124 | getAvailableEncoders (): AvailableEncoders { | ||
125 | return { | ||
126 | available: this.availableEncoders, | ||
127 | encodersToTry: { | ||
128 | vod: { | ||
129 | video: this.getEncodersByPriority('vod', 'video'), | ||
130 | audio: this.getEncodersByPriority('vod', 'audio') | ||
131 | }, | ||
132 | live: { | ||
133 | video: this.getEncodersByPriority('live', 'video'), | ||
134 | audio: this.getEncodersByPriority('live', 'audio') | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | } | ||
139 | |||
140 | getAvailableProfiles (type: 'vod' | 'live') { | ||
141 | return this.availableProfiles[type] | ||
142 | } | ||
143 | |||
144 | addProfile (options: { | ||
145 | type: 'vod' | 'live' | ||
146 | encoder: string | ||
147 | profile: string | ||
148 | builder: EncoderOptionsBuilder | ||
149 | }) { | ||
150 | const { type, encoder, profile, builder } = options | ||
151 | |||
152 | const encoders = this.availableEncoders[type] | ||
153 | |||
154 | if (!encoders[encoder]) encoders[encoder] = {} | ||
155 | encoders[encoder][profile] = builder | ||
156 | |||
157 | this.buildAvailableProfiles() | ||
158 | } | ||
159 | |||
160 | removeProfile (options: { | ||
161 | type: 'vod' | 'live' | ||
162 | encoder: string | ||
163 | profile: string | ||
164 | }) { | ||
165 | const { type, encoder, profile } = options | ||
166 | |||
167 | delete this.availableEncoders[type][encoder][profile] | ||
168 | this.buildAvailableProfiles() | ||
169 | } | ||
170 | |||
171 | addEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) { | ||
172 | this.encodersPriorities[type][streamType].push({ name: encoder, priority }) | ||
173 | |||
174 | resetSupportedEncoders() | ||
175 | } | ||
176 | |||
177 | removeEncoderPriority (type: 'vod' | 'live', streamType: 'audio' | 'video', encoder: string, priority: number) { | ||
178 | this.encodersPriorities[type][streamType] = this.encodersPriorities[type][streamType] | ||
179 | .filter(o => o.name !== encoder && o.priority !== priority) | ||
180 | |||
181 | resetSupportedEncoders() | ||
182 | } | ||
183 | |||
184 | private getEncodersByPriority (type: 'vod' | 'live', streamType: 'audio' | 'video') { | ||
185 | return this.encodersPriorities[type][streamType] | ||
186 | .sort((e1, e2) => { | ||
187 | if (e1.priority > e2.priority) return -1 | ||
188 | else if (e1.priority === e2.priority) return 0 | ||
189 | |||
190 | return 1 | ||
191 | }) | ||
192 | .map(e => e.name) | ||
193 | } | ||
194 | |||
195 | private buildAvailableProfiles () { | ||
196 | for (const type of [ 'vod', 'live' ]) { | ||
197 | const result = new Set() | ||
198 | |||
199 | const encoders = this.availableEncoders[type] | ||
200 | |||
201 | for (const encoderName of Object.keys(encoders)) { | ||
202 | for (const profile of Object.keys(encoders[encoderName])) { | ||
203 | result.add(profile) | ||
204 | } | ||
205 | } | ||
206 | |||
207 | this.availableProfiles[type] = Array.from(result) | ||
208 | } | ||
209 | |||
210 | logger.debug('Available transcoding profiles built.', { availableProfiles: this.availableProfiles }) | ||
211 | } | ||
212 | |||
213 | private buildDefaultEncodersPriorities () { | ||
214 | return { | ||
215 | video: [ | ||
216 | { name: 'libx264', priority: 100 } | ||
217 | ], | ||
218 | |||
219 | // Try the first one, if not available try the second one etc | ||
220 | audio: [ | ||
221 | // we favor VBR, if a good AAC encoder is available | ||
222 | { name: 'libfdk_aac', priority: 200 }, | ||
223 | { name: 'aac', priority: 100 } | ||
224 | ] | ||
225 | } | ||
226 | } | ||
227 | |||
228 | static get Instance () { | ||
229 | return this.instance || (this.instance = new this()) | ||
230 | } | ||
231 | } | ||
232 | |||
233 | // --------------------------------------------------------------------------- | ||
234 | |||
235 | export { | ||
236 | VideoTranscodingProfilesManager | ||
237 | } | ||
238 | |||
239 | // --------------------------------------------------------------------------- | ||
240 | async function buildTargetBitrate (options: { | ||
241 | input: string | ||
242 | resolution: VideoResolution | ||
243 | fps: number | ||
244 | }) { | ||
245 | const { input, resolution, fps } = options | ||
246 | const probe = await ffprobePromise(input) | ||
247 | |||
248 | const videoStream = await getVideoStreamFromFile(input, probe) | ||
249 | if (!videoStream) return undefined | ||
250 | |||
251 | const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS) | ||
252 | |||
253 | // Don't transcode to an higher bitrate than the original file | ||
254 | const fileBitrate = await getVideoFileBitrate(input, probe) | ||
255 | return Math.min(targetBitrate, fileBitrate) | ||
256 | } | ||
diff --git a/server/lib/transcoding/video-transcoding.ts b/server/lib/transcoding/video-transcoding.ts new file mode 100644 index 000000000..5df192575 --- /dev/null +++ b/server/lib/transcoding/video-transcoding.ts | |||
@@ -0,0 +1,358 @@ | |||
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 { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | ||
5 | import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoFullLight } from '@server/types/models' | ||
6 | import { VideoResolution } from '../../../shared/models/videos' | ||
7 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' | ||
8 | import { transcode, TranscodeOptions, TranscodeOptionsType } from '../../helpers/ffmpeg-utils' | ||
9 | import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../../helpers/ffprobe-utils' | ||
10 | import { logger } from '../../helpers/logger' | ||
11 | import { CONFIG } from '../../initializers/config' | ||
12 | import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../../initializers/constants' | ||
13 | import { VideoFileModel } from '../../models/video/video-file' | ||
14 | import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist' | ||
15 | import { updateMasterHLSPlaylist, updateSha256VODSegments } from '../hls' | ||
16 | import { generateVideoFilename, generateVideoStreamingPlaylistName, getVideoFilePath } from '../video-paths' | ||
17 | import { VideoTranscodingProfilesManager } from './video-transcoding-profiles' | ||
18 | |||
19 | /** | ||
20 | * | ||
21 | * Functions that run transcoding functions, update the database, cleanup files, create torrent files... | ||
22 | * Mainly called by the job queue | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | // Optimize the original video file and replace it. The resolution is not changed. | ||
27 | async function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVideoFile, job?: Job) { | ||
28 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR | ||
29 | const newExtname = '.mp4' | ||
30 | |||
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: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | ||
45 | profile: CONFIG.TRANSCODING.PROFILE, | ||
46 | |||
47 | resolution: inputVideoFile.resolution, | ||
48 | |||
49 | job | ||
50 | } | ||
51 | |||
52 | // Could be very long! | ||
53 | await transcode(transcodeOptions) | ||
54 | |||
55 | try { | ||
56 | await remove(videoInputPath) | ||
57 | |||
58 | // Important to do this before getVideoFilename() to take in account the new filename | ||
59 | inputVideoFile.extname = newExtname | ||
60 | inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname) | ||
61 | |||
62 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) | ||
63 | |||
64 | await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) | ||
65 | |||
66 | return transcodeType | ||
67 | } catch (err) { | ||
68 | // Auto destruction... | ||
69 | video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err })) | ||
70 | |||
71 | throw err | ||
72 | } | ||
73 | } | ||
74 | |||
75 | // Transcode the original video file to a lower resolution. | ||
76 | async function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: VideoResolution, isPortrait: boolean, job: Job) { | ||
77 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR | ||
78 | const extname = '.mp4' | ||
79 | |||
80 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed | ||
81 | const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile()) | ||
82 | |||
83 | const newVideoFile = new VideoFileModel({ | ||
84 | resolution, | ||
85 | extname, | ||
86 | filename: generateVideoFilename(video, false, resolution, extname), | ||
87 | size: 0, | ||
88 | videoId: video.id | ||
89 | }) | ||
90 | |||
91 | const videoOutputPath = getVideoFilePath(video, newVideoFile) | ||
92 | const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename) | ||
93 | |||
94 | const transcodeOptions = resolution === VideoResolution.H_NOVIDEO | ||
95 | ? { | ||
96 | type: 'only-audio' as 'only-audio', | ||
97 | |||
98 | inputPath: videoInputPath, | ||
99 | outputPath: videoTranscodedPath, | ||
100 | |||
101 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | ||
102 | profile: CONFIG.TRANSCODING.PROFILE, | ||
103 | |||
104 | resolution, | ||
105 | |||
106 | job | ||
107 | } | ||
108 | : { | ||
109 | type: 'video' as 'video', | ||
110 | inputPath: videoInputPath, | ||
111 | outputPath: videoTranscodedPath, | ||
112 | |||
113 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | ||
114 | profile: CONFIG.TRANSCODING.PROFILE, | ||
115 | |||
116 | resolution, | ||
117 | isPortraitMode: isPortrait, | ||
118 | |||
119 | job | ||
120 | } | ||
121 | |||
122 | await transcode(transcodeOptions) | ||
123 | |||
124 | return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath) | ||
125 | } | ||
126 | |||
127 | // Merge an image with an audio file to create a video | ||
128 | async function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolution, job: Job) { | ||
129 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR | ||
130 | const newExtname = '.mp4' | ||
131 | |||
132 | const inputVideoFile = video.getMinQualityFile() | ||
133 | |||
134 | const audioInputPath = getVideoFilePath(video, inputVideoFile) | ||
135 | const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) | ||
136 | |||
137 | // If the user updates the video preview during transcoding | ||
138 | const previewPath = video.getPreview().getPath() | ||
139 | const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath)) | ||
140 | await copyFile(previewPath, tmpPreviewPath) | ||
141 | |||
142 | const transcodeOptions = { | ||
143 | type: 'merge-audio' as 'merge-audio', | ||
144 | |||
145 | inputPath: tmpPreviewPath, | ||
146 | outputPath: videoTranscodedPath, | ||
147 | |||
148 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | ||
149 | profile: CONFIG.TRANSCODING.PROFILE, | ||
150 | |||
151 | audioPath: audioInputPath, | ||
152 | resolution, | ||
153 | |||
154 | job | ||
155 | } | ||
156 | |||
157 | try { | ||
158 | await transcode(transcodeOptions) | ||
159 | |||
160 | await remove(audioInputPath) | ||
161 | await remove(tmpPreviewPath) | ||
162 | } catch (err) { | ||
163 | await remove(tmpPreviewPath) | ||
164 | throw err | ||
165 | } | ||
166 | |||
167 | // Important to do this before getVideoFilename() to take in account the new file extension | ||
168 | inputVideoFile.extname = newExtname | ||
169 | inputVideoFile.filename = generateVideoFilename(video, false, inputVideoFile.resolution, newExtname) | ||
170 | |||
171 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) | ||
172 | // ffmpeg generated a new video file, so update the video duration | ||
173 | // See https://trac.ffmpeg.org/ticket/5456 | ||
174 | video.duration = await getDurationFromVideoFile(videoTranscodedPath) | ||
175 | await video.save() | ||
176 | |||
177 | return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) | ||
178 | } | ||
179 | |||
180 | // Concat TS segments from a live video to a fragmented mp4 HLS playlist | ||
181 | async function generateHlsPlaylistResolutionFromTS (options: { | ||
182 | video: MVideoFullLight | ||
183 | concatenatedTsFilePath: string | ||
184 | resolution: VideoResolution | ||
185 | isPortraitMode: boolean | ||
186 | isAAC: boolean | ||
187 | }) { | ||
188 | return generateHlsPlaylistCommon({ | ||
189 | video: options.video, | ||
190 | resolution: options.resolution, | ||
191 | isPortraitMode: options.isPortraitMode, | ||
192 | inputPath: options.concatenatedTsFilePath, | ||
193 | type: 'hls-from-ts' as 'hls-from-ts', | ||
194 | isAAC: options.isAAC | ||
195 | }) | ||
196 | } | ||
197 | |||
198 | // Generate an HLS playlist from an input file, and update the master playlist | ||
199 | function generateHlsPlaylistResolution (options: { | ||
200 | video: MVideoFullLight | ||
201 | videoInputPath: string | ||
202 | resolution: VideoResolution | ||
203 | copyCodecs: boolean | ||
204 | isPortraitMode: boolean | ||
205 | job?: Job | ||
206 | }) { | ||
207 | return generateHlsPlaylistCommon({ | ||
208 | video: options.video, | ||
209 | resolution: options.resolution, | ||
210 | copyCodecs: options.copyCodecs, | ||
211 | isPortraitMode: options.isPortraitMode, | ||
212 | inputPath: options.videoInputPath, | ||
213 | type: 'hls' as 'hls', | ||
214 | job: options.job | ||
215 | }) | ||
216 | } | ||
217 | |||
218 | // --------------------------------------------------------------------------- | ||
219 | |||
220 | export { | ||
221 | generateHlsPlaylistResolution, | ||
222 | generateHlsPlaylistResolutionFromTS, | ||
223 | optimizeOriginalVideofile, | ||
224 | transcodeNewWebTorrentResolution, | ||
225 | mergeAudioVideofile | ||
226 | } | ||
227 | |||
228 | // --------------------------------------------------------------------------- | ||
229 | |||
230 | async function onWebTorrentVideoFileTranscoding ( | ||
231 | video: MVideoFullLight, | ||
232 | videoFile: MVideoFile, | ||
233 | transcodingPath: string, | ||
234 | outputPath: string | ||
235 | ) { | ||
236 | const stats = await stat(transcodingPath) | ||
237 | const fps = await getVideoFileFPS(transcodingPath) | ||
238 | const metadata = await getMetadataFromFile(transcodingPath) | ||
239 | |||
240 | await move(transcodingPath, outputPath, { overwrite: true }) | ||
241 | |||
242 | videoFile.size = stats.size | ||
243 | videoFile.fps = fps | ||
244 | videoFile.metadata = metadata | ||
245 | |||
246 | await createTorrentAndSetInfoHash(video, videoFile) | ||
247 | |||
248 | await VideoFileModel.customUpsert(videoFile, 'video', undefined) | ||
249 | video.VideoFiles = await video.$get('VideoFiles') | ||
250 | |||
251 | return video | ||
252 | } | ||
253 | |||
254 | async function generateHlsPlaylistCommon (options: { | ||
255 | type: 'hls' | 'hls-from-ts' | ||
256 | video: MVideoFullLight | ||
257 | inputPath: string | ||
258 | resolution: VideoResolution | ||
259 | copyCodecs?: boolean | ||
260 | isAAC?: boolean | ||
261 | isPortraitMode: boolean | ||
262 | |||
263 | job?: Job | ||
264 | }) { | ||
265 | const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options | ||
266 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR | ||
267 | |||
268 | const videoTranscodedBasePath = join(transcodeDirectory, type) | ||
269 | await ensureDir(videoTranscodedBasePath) | ||
270 | |||
271 | const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution) | ||
272 | const playlistFilename = VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution) | ||
273 | const playlistFileTranscodePath = join(videoTranscodedBasePath, playlistFilename) | ||
274 | |||
275 | const transcodeOptions = { | ||
276 | type, | ||
277 | |||
278 | inputPath, | ||
279 | outputPath: playlistFileTranscodePath, | ||
280 | |||
281 | availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(), | ||
282 | profile: CONFIG.TRANSCODING.PROFILE, | ||
283 | |||
284 | resolution, | ||
285 | copyCodecs, | ||
286 | isPortraitMode, | ||
287 | |||
288 | isAAC, | ||
289 | |||
290 | hlsPlaylist: { | ||
291 | videoFilename | ||
292 | }, | ||
293 | |||
294 | job | ||
295 | } | ||
296 | |||
297 | await transcode(transcodeOptions) | ||
298 | |||
299 | const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) | ||
300 | |||
301 | // Create or update the playlist | ||
302 | const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ | ||
303 | videoId: video.id, | ||
304 | playlistUrl, | ||
305 | segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive), | ||
306 | p2pMediaLoaderInfohashes: [], | ||
307 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, | ||
308 | |||
309 | type: VideoStreamingPlaylistType.HLS | ||
310 | }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ] | ||
311 | videoStreamingPlaylist.Video = video | ||
312 | |||
313 | // Build the new playlist file | ||
314 | const extname = extnameUtil(videoFilename) | ||
315 | const newVideoFile = new VideoFileModel({ | ||
316 | resolution, | ||
317 | extname, | ||
318 | size: 0, | ||
319 | filename: generateVideoFilename(video, true, resolution, extname), | ||
320 | fps: -1, | ||
321 | videoStreamingPlaylistId: videoStreamingPlaylist.id | ||
322 | }) | ||
323 | |||
324 | const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile) | ||
325 | |||
326 | // Move files from tmp transcoded directory to the appropriate place | ||
327 | const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) | ||
328 | await ensureDir(baseHlsDirectory) | ||
329 | |||
330 | // Move playlist file | ||
331 | const playlistPath = join(baseHlsDirectory, playlistFilename) | ||
332 | await move(playlistFileTranscodePath, playlistPath, { overwrite: true }) | ||
333 | // Move video file | ||
334 | await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true }) | ||
335 | |||
336 | const stats = await stat(videoFilePath) | ||
337 | |||
338 | newVideoFile.size = stats.size | ||
339 | newVideoFile.fps = await getVideoFileFPS(videoFilePath) | ||
340 | newVideoFile.metadata = await getMetadataFromFile(videoFilePath) | ||
341 | |||
342 | await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile) | ||
343 | |||
344 | await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined) | ||
345 | videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles') | ||
346 | |||
347 | videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes( | ||
348 | playlistUrl, videoStreamingPlaylist.VideoFiles | ||
349 | ) | ||
350 | await videoStreamingPlaylist.save() | ||
351 | |||
352 | video.setHLSPlaylist(videoStreamingPlaylist) | ||
353 | |||
354 | await updateMasterHLSPlaylist(video) | ||
355 | await updateSha256VODSegments(video) | ||
356 | |||
357 | return playlistPath | ||
358 | } | ||