]>
Commit | Line | Data |
---|---|---|
3851e732 | 1 | import { copyFile, ensureDir, move, remove, stat } from 'fs-extra' |
d7a25329 | 2 | import { basename, extname as extnameUtil, join } from 'path' |
053aed43 C |
3 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' |
4 | import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/types/models' | |
5a547f69 | 5 | import { VideoResolution } from '../../shared/models/videos' |
053aed43 | 6 | import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type' |
5a547f69 C |
7 | import { transcode, TranscodeOptions, TranscodeOptionsType } from '../helpers/ffmpeg-utils' |
8 | import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../helpers/ffprobe-utils' | |
098eb377 | 9 | import { logger } from '../helpers/logger' |
053aed43 | 10 | import { CONFIG } from '../initializers/config' |
5a547f69 | 11 | import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants' |
098eb377 | 12 | import { VideoFileModel } from '../models/video/video-file' |
09209296 | 13 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' |
053aed43 | 14 | import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls' |
d7a25329 | 15 | import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths' |
5a547f69 | 16 | import { availableEncoders } from './video-transcoding-profiles' |
098eb377 | 17 | |
658a47ab | 18 | /** |
6b67897e C |
19 | * |
20 | * Functions that run transcoding functions, update the database, cleanup files, create torrent files... | |
21 | * Mainly called by the job queue | |
22 | * | |
658a47ab | 23 | */ |
6b67897e C |
24 | |
25 | // Optimize the original video file and replace it. The resolution is not changed. | |
d7a25329 | 26 | async function optimizeOriginalVideofile (video: MVideoWithFile, inputVideoFileArg?: MVideoFile) { |
2fbd5e25 | 27 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
098eb377 | 28 | const newExtname = '.mp4' |
9f1ddd24 | 29 | |
d7a25329 C |
30 | const inputVideoFile = inputVideoFileArg || video.getMaxQualityFile() |
31 | const videoInputPath = getVideoFilePath(video, inputVideoFile) | |
2fbd5e25 | 32 | const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) |
098eb377 | 33 | |
536598cf C |
34 | const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath) |
35 | ? 'quick-transcode' | |
36 | : 'video' | |
5ba49f26 | 37 | |
536598cf | 38 | const transcodeOptions: TranscodeOptions = { |
d7a25329 | 39 | type: transcodeType, |
9252a33d | 40 | |
098eb377 | 41 | inputPath: videoInputPath, |
09209296 | 42 | outputPath: videoTranscodedPath, |
9252a33d C |
43 | |
44 | availableEncoders, | |
45 | profile: 'default', | |
46 | ||
536598cf | 47 | resolution: inputVideoFile.resolution |
098eb377 C |
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 | |
536598cf | 57 | inputVideoFile.extname = newExtname |
2fbd5e25 | 58 | |
d7a25329 | 59 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) |
098eb377 | 60 | |
536598cf | 61 | await onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) |
236841a1 C |
62 | |
63 | return transcodeType | |
098eb377 C |
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 | ||
6b67897e | 72 | // Transcode the original video file to a lower resolution. |
d7a25329 | 73 | async function transcodeNewResolution (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean) { |
2fbd5e25 | 74 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
098eb377 C |
75 | const extname = '.mp4' |
76 | ||
77 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed | |
d7a25329 | 78 | const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile()) |
098eb377 C |
79 | |
80 | const newVideoFile = new VideoFileModel({ | |
81 | resolution, | |
82 | extname, | |
83 | size: 0, | |
84 | videoId: video.id | |
85 | }) | |
d7a25329 C |
86 | const videoOutputPath = getVideoFilePath(video, newVideoFile) |
87 | const videoTranscodedPath = join(transcodeDirectory, getVideoFilename(video, newVideoFile)) | |
098eb377 | 88 | |
5c7d6508 | 89 | const transcodeOptions = resolution === VideoResolution.H_NOVIDEO |
90 | ? { | |
3a149e9f | 91 | type: 'only-audio' as 'only-audio', |
9252a33d | 92 | |
3a149e9f C |
93 | inputPath: videoInputPath, |
94 | outputPath: videoTranscodedPath, | |
9252a33d C |
95 | |
96 | availableEncoders, | |
97 | profile: 'default', | |
98 | ||
3a149e9f C |
99 | resolution |
100 | } | |
5c7d6508 | 101 | : { |
3a149e9f C |
102 | type: 'video' as 'video', |
103 | inputPath: videoInputPath, | |
104 | outputPath: videoTranscodedPath, | |
9252a33d C |
105 | |
106 | availableEncoders, | |
107 | profile: 'default', | |
108 | ||
3a149e9f C |
109 | resolution, |
110 | isPortraitMode: isPortrait | |
111 | } | |
098eb377 C |
112 | |
113 | await transcode(transcodeOptions) | |
114 | ||
536598cf C |
115 | return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath) |
116 | } | |
117 | ||
6b67897e | 118 | // Merge an image with an audio file to create a video |
d7a25329 | 119 | async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution) { |
536598cf C |
120 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
121 | const newExtname = '.mp4' | |
122 | ||
92e0f42e | 123 | const inputVideoFile = video.getMinQualityFile() |
2fbd5e25 | 124 | |
d7a25329 | 125 | const audioInputPath = getVideoFilePath(video, inputVideoFile) |
536598cf | 126 | const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) |
098eb377 | 127 | |
eba06469 C |
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 | ||
536598cf C |
133 | const transcodeOptions = { |
134 | type: 'merge-audio' as 'merge-audio', | |
9252a33d | 135 | |
eba06469 | 136 | inputPath: tmpPreviewPath, |
536598cf | 137 | outputPath: videoTranscodedPath, |
9252a33d C |
138 | |
139 | availableEncoders, | |
140 | profile: 'default', | |
141 | ||
536598cf C |
142 | audioPath: audioInputPath, |
143 | resolution | |
144 | } | |
098eb377 | 145 | |
eba06469 C |
146 | try { |
147 | await transcode(transcodeOptions) | |
098eb377 | 148 | |
eba06469 C |
149 | await remove(audioInputPath) |
150 | await remove(tmpPreviewPath) | |
151 | } catch (err) { | |
152 | await remove(tmpPreviewPath) | |
153 | throw err | |
154 | } | |
098eb377 | 155 | |
536598cf C |
156 | // Important to do this before getVideoFilename() to take in account the new file extension |
157 | inputVideoFile.extname = newExtname | |
158 | ||
d7a25329 | 159 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) |
eba06469 C |
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() | |
536598cf C |
164 | |
165 | return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) | |
098eb377 C |
166 | } |
167 | ||
2650d6d4 C |
168 | // Concat TS segments from a live video to a fragmented mp4 HLS playlist |
169 | async function generateHlsPlaylistFromTS (options: { | |
170 | video: MVideoWithFile | |
3851e732 | 171 | concatenatedTsFilePath: string |
2650d6d4 C |
172 | resolution: VideoResolution |
173 | isPortraitMode: boolean | |
e772bdf1 | 174 | isAAC: boolean |
2650d6d4 | 175 | }) { |
3851e732 C |
176 | return generateHlsPlaylistCommon({ |
177 | video: options.video, | |
178 | resolution: options.resolution, | |
179 | isPortraitMode: options.isPortraitMode, | |
180 | inputPath: options.concatenatedTsFilePath, | |
e772bdf1 C |
181 | type: 'hls-from-ts' as 'hls-from-ts', |
182 | isAAC: options.isAAC | |
3851e732 | 183 | }) |
2650d6d4 C |
184 | } |
185 | ||
6b67897e | 186 | // Generate an HLS playlist from an input file, and update the master playlist |
2650d6d4 | 187 | function generateHlsPlaylist (options: { |
b5b68755 C |
188 | video: MVideoWithFile |
189 | videoInputPath: string | |
190 | resolution: VideoResolution | |
191 | copyCodecs: boolean | |
192 | isPortraitMode: boolean | |
193 | }) { | |
2650d6d4 C |
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 | // --------------------------------------------------------------------------- | |
205 | ||
206 | export { | |
207 | generateHlsPlaylist, | |
208 | generateHlsPlaylistFromTS, | |
209 | optimizeOriginalVideofile, | |
210 | transcodeNewResolution, | |
211 | mergeAudioVideofile | |
212 | } | |
213 | ||
214 | // --------------------------------------------------------------------------- | |
215 | ||
216 | async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) { | |
217 | const stats = await stat(transcodingPath) | |
218 | const fps = await getVideoFileFPS(transcodingPath) | |
219 | const metadata = await getMetadataFromFile(transcodingPath) | |
220 | ||
221 | await move(transcodingPath, outputPath, { overwrite: true }) | |
222 | ||
223 | videoFile.size = stats.size | |
224 | videoFile.fps = fps | |
225 | videoFile.metadata = metadata | |
226 | ||
227 | await createTorrentAndSetInfoHash(video, videoFile) | |
228 | ||
229 | await VideoFileModel.customUpsert(videoFile, 'video', undefined) | |
230 | video.VideoFiles = await video.$get('VideoFiles') | |
231 | ||
232 | return video | |
233 | } | |
234 | ||
235 | async function generateHlsPlaylistCommon (options: { | |
236 | type: 'hls' | 'hls-from-ts' | |
237 | video: MVideoWithFile | |
238 | inputPath: string | |
239 | resolution: VideoResolution | |
240 | copyCodecs?: boolean | |
e772bdf1 | 241 | isAAC?: boolean |
2650d6d4 C |
242 | isPortraitMode: boolean |
243 | }) { | |
e772bdf1 | 244 | const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC } = options |
b5b68755 | 245 | |
9c6ca37f C |
246 | const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) |
247 | await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)) | |
09209296 | 248 | |
09209296 | 249 | const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)) |
d7a25329 | 250 | const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution) |
09209296 C |
251 | |
252 | const transcodeOptions = { | |
2650d6d4 | 253 | type, |
9252a33d | 254 | |
2650d6d4 | 255 | inputPath, |
09209296 | 256 | outputPath, |
9252a33d C |
257 | |
258 | availableEncoders, | |
259 | profile: 'default', | |
260 | ||
09209296 | 261 | resolution, |
d7a25329 | 262 | copyCodecs, |
09209296 | 263 | isPortraitMode, |
4c280004 | 264 | |
e772bdf1 C |
265 | isAAC, |
266 | ||
4c280004 | 267 | hlsPlaylist: { |
d7a25329 | 268 | videoFilename |
4c280004 | 269 | } |
09209296 C |
270 | } |
271 | ||
d7a25329 | 272 | await transcode(transcodeOptions) |
09209296 | 273 | |
6dd9de95 | 274 | const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) |
09209296 | 275 | |
d7a25329 | 276 | const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ |
09209296 C |
277 | videoId: video.id, |
278 | playlistUrl, | |
c6c0fa6c | 279 | segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive), |
b5b68755 | 280 | p2pMediaLoaderInfohashes: [], |
594d0c6a | 281 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, |
09209296 C |
282 | |
283 | type: VideoStreamingPlaylistType.HLS | |
d7a25329 C |
284 | }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ] |
285 | videoStreamingPlaylist.Video = video | |
286 | ||
287 | const newVideoFile = new VideoFileModel({ | |
288 | resolution, | |
289 | extname: extnameUtil(videoFilename), | |
290 | size: 0, | |
291 | fps: -1, | |
292 | videoStreamingPlaylistId: videoStreamingPlaylist.id | |
09209296 | 293 | }) |
d7a25329 C |
294 | |
295 | const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile) | |
296 | const stats = await stat(videoFilePath) | |
297 | ||
298 | newVideoFile.size = stats.size | |
299 | newVideoFile.fps = await getVideoFileFPS(videoFilePath) | |
8319d6ae | 300 | newVideoFile.metadata = await getMetadataFromFile(videoFilePath) |
d7a25329 C |
301 | |
302 | await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile) | |
303 | ||
c547bbf9 | 304 | await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined) |
e6122097 | 305 | videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles') |
d7a25329 | 306 | |
b5b68755 C |
307 | videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes( |
308 | playlistUrl, videoStreamingPlaylist.VideoFiles | |
309 | ) | |
310 | await videoStreamingPlaylist.save() | |
311 | ||
d7a25329 C |
312 | video.setHLSPlaylist(videoStreamingPlaylist) |
313 | ||
314 | await updateMasterHLSPlaylist(video) | |
c6c0fa6c | 315 | await updateSha256VODSegments(video) |
d7a25329 | 316 | |
2650d6d4 | 317 | return outputPath |
098eb377 | 318 | } |