]>
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) |
098eb377 C |
62 | } catch (err) { |
63 | // Auto destruction... | |
64 | video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err })) | |
65 | ||
66 | throw err | |
67 | } | |
68 | } | |
69 | ||
6b67897e | 70 | // Transcode the original video file to a lower resolution. |
d7a25329 | 71 | async function transcodeNewResolution (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean) { |
2fbd5e25 | 72 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
098eb377 C |
73 | const extname = '.mp4' |
74 | ||
75 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed | |
d7a25329 | 76 | const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile()) |
098eb377 C |
77 | |
78 | const newVideoFile = new VideoFileModel({ | |
79 | resolution, | |
80 | extname, | |
81 | size: 0, | |
82 | videoId: video.id | |
83 | }) | |
d7a25329 C |
84 | const videoOutputPath = getVideoFilePath(video, newVideoFile) |
85 | const videoTranscodedPath = join(transcodeDirectory, getVideoFilename(video, newVideoFile)) | |
098eb377 | 86 | |
5c7d6508 | 87 | const transcodeOptions = resolution === VideoResolution.H_NOVIDEO |
88 | ? { | |
3a149e9f | 89 | type: 'only-audio' as 'only-audio', |
9252a33d | 90 | |
3a149e9f C |
91 | inputPath: videoInputPath, |
92 | outputPath: videoTranscodedPath, | |
9252a33d C |
93 | |
94 | availableEncoders, | |
95 | profile: 'default', | |
96 | ||
3a149e9f C |
97 | resolution |
98 | } | |
5c7d6508 | 99 | : { |
3a149e9f C |
100 | type: 'video' as 'video', |
101 | inputPath: videoInputPath, | |
102 | outputPath: videoTranscodedPath, | |
9252a33d C |
103 | |
104 | availableEncoders, | |
105 | profile: 'default', | |
106 | ||
3a149e9f C |
107 | resolution, |
108 | isPortraitMode: isPortrait | |
109 | } | |
098eb377 C |
110 | |
111 | await transcode(transcodeOptions) | |
112 | ||
536598cf C |
113 | return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath) |
114 | } | |
115 | ||
6b67897e | 116 | // Merge an image with an audio file to create a video |
d7a25329 | 117 | async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution) { |
536598cf C |
118 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
119 | const newExtname = '.mp4' | |
120 | ||
92e0f42e | 121 | const inputVideoFile = video.getMinQualityFile() |
2fbd5e25 | 122 | |
d7a25329 | 123 | const audioInputPath = getVideoFilePath(video, inputVideoFile) |
536598cf | 124 | const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) |
098eb377 | 125 | |
eba06469 C |
126 | // If the user updates the video preview during transcoding |
127 | const previewPath = video.getPreview().getPath() | |
128 | const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath)) | |
129 | await copyFile(previewPath, tmpPreviewPath) | |
130 | ||
536598cf C |
131 | const transcodeOptions = { |
132 | type: 'merge-audio' as 'merge-audio', | |
9252a33d | 133 | |
eba06469 | 134 | inputPath: tmpPreviewPath, |
536598cf | 135 | outputPath: videoTranscodedPath, |
9252a33d C |
136 | |
137 | availableEncoders, | |
138 | profile: 'default', | |
139 | ||
536598cf C |
140 | audioPath: audioInputPath, |
141 | resolution | |
142 | } | |
098eb377 | 143 | |
eba06469 C |
144 | try { |
145 | await transcode(transcodeOptions) | |
098eb377 | 146 | |
eba06469 C |
147 | await remove(audioInputPath) |
148 | await remove(tmpPreviewPath) | |
149 | } catch (err) { | |
150 | await remove(tmpPreviewPath) | |
151 | throw err | |
152 | } | |
098eb377 | 153 | |
536598cf C |
154 | // Important to do this before getVideoFilename() to take in account the new file extension |
155 | inputVideoFile.extname = newExtname | |
156 | ||
d7a25329 | 157 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) |
eba06469 C |
158 | // ffmpeg generated a new video file, so update the video duration |
159 | // See https://trac.ffmpeg.org/ticket/5456 | |
160 | video.duration = await getDurationFromVideoFile(videoTranscodedPath) | |
161 | await video.save() | |
536598cf C |
162 | |
163 | return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) | |
098eb377 C |
164 | } |
165 | ||
2650d6d4 C |
166 | // Concat TS segments from a live video to a fragmented mp4 HLS playlist |
167 | async function generateHlsPlaylistFromTS (options: { | |
168 | video: MVideoWithFile | |
3851e732 | 169 | concatenatedTsFilePath: string |
2650d6d4 C |
170 | resolution: VideoResolution |
171 | isPortraitMode: boolean | |
e772bdf1 | 172 | isAAC: boolean |
2650d6d4 | 173 | }) { |
3851e732 C |
174 | return generateHlsPlaylistCommon({ |
175 | video: options.video, | |
176 | resolution: options.resolution, | |
177 | isPortraitMode: options.isPortraitMode, | |
178 | inputPath: options.concatenatedTsFilePath, | |
e772bdf1 C |
179 | type: 'hls-from-ts' as 'hls-from-ts', |
180 | isAAC: options.isAAC | |
3851e732 | 181 | }) |
2650d6d4 C |
182 | } |
183 | ||
6b67897e | 184 | // Generate an HLS playlist from an input file, and update the master playlist |
2650d6d4 | 185 | function generateHlsPlaylist (options: { |
b5b68755 C |
186 | video: MVideoWithFile |
187 | videoInputPath: string | |
188 | resolution: VideoResolution | |
189 | copyCodecs: boolean | |
190 | isPortraitMode: boolean | |
191 | }) { | |
2650d6d4 C |
192 | return generateHlsPlaylistCommon({ |
193 | video: options.video, | |
194 | resolution: options.resolution, | |
195 | copyCodecs: options.copyCodecs, | |
196 | isPortraitMode: options.isPortraitMode, | |
197 | inputPath: options.videoInputPath, | |
198 | type: 'hls' as 'hls' | |
199 | }) | |
200 | } | |
201 | ||
202 | // --------------------------------------------------------------------------- | |
203 | ||
204 | export { | |
205 | generateHlsPlaylist, | |
206 | generateHlsPlaylistFromTS, | |
207 | optimizeOriginalVideofile, | |
208 | transcodeNewResolution, | |
209 | mergeAudioVideofile | |
210 | } | |
211 | ||
212 | // --------------------------------------------------------------------------- | |
213 | ||
214 | async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) { | |
215 | const stats = await stat(transcodingPath) | |
216 | const fps = await getVideoFileFPS(transcodingPath) | |
217 | const metadata = await getMetadataFromFile(transcodingPath) | |
218 | ||
219 | await move(transcodingPath, outputPath, { overwrite: true }) | |
220 | ||
221 | videoFile.size = stats.size | |
222 | videoFile.fps = fps | |
223 | videoFile.metadata = metadata | |
224 | ||
225 | await createTorrentAndSetInfoHash(video, videoFile) | |
226 | ||
227 | await VideoFileModel.customUpsert(videoFile, 'video', undefined) | |
228 | video.VideoFiles = await video.$get('VideoFiles') | |
229 | ||
230 | return video | |
231 | } | |
232 | ||
233 | async function generateHlsPlaylistCommon (options: { | |
234 | type: 'hls' | 'hls-from-ts' | |
235 | video: MVideoWithFile | |
236 | inputPath: string | |
237 | resolution: VideoResolution | |
238 | copyCodecs?: boolean | |
e772bdf1 | 239 | isAAC?: boolean |
2650d6d4 C |
240 | isPortraitMode: boolean |
241 | }) { | |
e772bdf1 | 242 | const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC } = options |
b5b68755 | 243 | |
9c6ca37f C |
244 | const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) |
245 | await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)) | |
09209296 | 246 | |
09209296 | 247 | const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)) |
d7a25329 | 248 | const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution) |
09209296 C |
249 | |
250 | const transcodeOptions = { | |
2650d6d4 | 251 | type, |
9252a33d | 252 | |
2650d6d4 | 253 | inputPath, |
09209296 | 254 | outputPath, |
9252a33d C |
255 | |
256 | availableEncoders, | |
257 | profile: 'default', | |
258 | ||
09209296 | 259 | resolution, |
d7a25329 | 260 | copyCodecs, |
09209296 | 261 | isPortraitMode, |
4c280004 | 262 | |
e772bdf1 C |
263 | isAAC, |
264 | ||
4c280004 | 265 | hlsPlaylist: { |
d7a25329 | 266 | videoFilename |
4c280004 | 267 | } |
09209296 C |
268 | } |
269 | ||
d7a25329 | 270 | await transcode(transcodeOptions) |
09209296 | 271 | |
6dd9de95 | 272 | const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) |
09209296 | 273 | |
d7a25329 | 274 | const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ |
09209296 C |
275 | videoId: video.id, |
276 | playlistUrl, | |
c6c0fa6c | 277 | segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive), |
b5b68755 | 278 | p2pMediaLoaderInfohashes: [], |
594d0c6a | 279 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, |
09209296 C |
280 | |
281 | type: VideoStreamingPlaylistType.HLS | |
d7a25329 C |
282 | }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ] |
283 | videoStreamingPlaylist.Video = video | |
284 | ||
285 | const newVideoFile = new VideoFileModel({ | |
286 | resolution, | |
287 | extname: extnameUtil(videoFilename), | |
288 | size: 0, | |
289 | fps: -1, | |
290 | videoStreamingPlaylistId: videoStreamingPlaylist.id | |
09209296 | 291 | }) |
d7a25329 C |
292 | |
293 | const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile) | |
294 | const stats = await stat(videoFilePath) | |
295 | ||
296 | newVideoFile.size = stats.size | |
297 | newVideoFile.fps = await getVideoFileFPS(videoFilePath) | |
8319d6ae | 298 | newVideoFile.metadata = await getMetadataFromFile(videoFilePath) |
d7a25329 C |
299 | |
300 | await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile) | |
301 | ||
c547bbf9 | 302 | await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined) |
e6122097 | 303 | videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles') |
d7a25329 | 304 | |
b5b68755 C |
305 | videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes( |
306 | playlistUrl, videoStreamingPlaylist.VideoFiles | |
307 | ) | |
308 | await videoStreamingPlaylist.save() | |
309 | ||
d7a25329 C |
310 | video.setHLSPlaylist(videoStreamingPlaylist) |
311 | ||
312 | await updateMasterHLSPlaylist(video) | |
c6c0fa6c | 313 | await updateSha256VODSegments(video) |
d7a25329 | 314 | |
2650d6d4 | 315 | return outputPath |
098eb377 | 316 | } |