]>
Commit | Line | Data |
---|---|---|
053aed43 | 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 | ||
6b67897e | 166 | // Generate an HLS playlist from an input file, and update the master playlist |
b5b68755 C |
167 | async function generateHlsPlaylist (options: { |
168 | video: MVideoWithFile | |
169 | videoInputPath: string | |
170 | resolution: VideoResolution | |
171 | copyCodecs: boolean | |
172 | isPortraitMode: boolean | |
173 | }) { | |
174 | const { video, videoInputPath, resolution, copyCodecs, isPortraitMode } = options | |
175 | ||
9c6ca37f C |
176 | const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) |
177 | await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)) | |
09209296 | 178 | |
09209296 | 179 | const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)) |
d7a25329 | 180 | const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution) |
09209296 C |
181 | |
182 | const transcodeOptions = { | |
536598cf | 183 | type: 'hls' as 'hls', |
9252a33d | 184 | |
09209296 C |
185 | inputPath: videoInputPath, |
186 | outputPath, | |
9252a33d C |
187 | |
188 | availableEncoders, | |
189 | profile: 'default', | |
190 | ||
09209296 | 191 | resolution, |
d7a25329 | 192 | copyCodecs, |
09209296 | 193 | isPortraitMode, |
4c280004 C |
194 | |
195 | hlsPlaylist: { | |
d7a25329 | 196 | videoFilename |
4c280004 | 197 | } |
09209296 C |
198 | } |
199 | ||
d7a25329 | 200 | await transcode(transcodeOptions) |
09209296 | 201 | |
6dd9de95 | 202 | const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) |
09209296 | 203 | |
d7a25329 | 204 | const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ |
09209296 C |
205 | videoId: video.id, |
206 | playlistUrl, | |
c6c0fa6c | 207 | segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive), |
b5b68755 | 208 | p2pMediaLoaderInfohashes: [], |
594d0c6a | 209 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, |
09209296 C |
210 | |
211 | type: VideoStreamingPlaylistType.HLS | |
d7a25329 C |
212 | }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ] |
213 | videoStreamingPlaylist.Video = video | |
214 | ||
215 | const newVideoFile = new VideoFileModel({ | |
216 | resolution, | |
217 | extname: extnameUtil(videoFilename), | |
218 | size: 0, | |
219 | fps: -1, | |
220 | videoStreamingPlaylistId: videoStreamingPlaylist.id | |
09209296 | 221 | }) |
d7a25329 C |
222 | |
223 | const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile) | |
224 | const stats = await stat(videoFilePath) | |
225 | ||
226 | newVideoFile.size = stats.size | |
227 | newVideoFile.fps = await getVideoFileFPS(videoFilePath) | |
8319d6ae | 228 | newVideoFile.metadata = await getMetadataFromFile(videoFilePath) |
d7a25329 C |
229 | |
230 | await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile) | |
231 | ||
c547bbf9 | 232 | await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined) |
e6122097 | 233 | videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles') |
d7a25329 | 234 | |
b5b68755 C |
235 | videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes( |
236 | playlistUrl, videoStreamingPlaylist.VideoFiles | |
237 | ) | |
238 | await videoStreamingPlaylist.save() | |
239 | ||
d7a25329 C |
240 | video.setHLSPlaylist(videoStreamingPlaylist) |
241 | ||
242 | await updateMasterHLSPlaylist(video) | |
c6c0fa6c | 243 | await updateSha256VODSegments(video) |
d7a25329 C |
244 | |
245 | return video | |
09209296 C |
246 | } |
247 | ||
536598cf C |
248 | // --------------------------------------------------------------------------- |
249 | ||
098eb377 | 250 | export { |
09209296 | 251 | generateHlsPlaylist, |
d7a25329 C |
252 | optimizeOriginalVideofile, |
253 | transcodeNewResolution, | |
536598cf C |
254 | mergeAudioVideofile |
255 | } | |
256 | ||
257 | // --------------------------------------------------------------------------- | |
258 | ||
453e83ea | 259 | async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) { |
536598cf C |
260 | const stats = await stat(transcodingPath) |
261 | const fps = await getVideoFileFPS(transcodingPath) | |
8319d6ae | 262 | const metadata = await getMetadataFromFile(transcodingPath) |
536598cf | 263 | |
fb0f7f82 | 264 | await move(transcodingPath, outputPath, { overwrite: true }) |
536598cf | 265 | |
453e83ea C |
266 | videoFile.size = stats.size |
267 | videoFile.fps = fps | |
8319d6ae | 268 | videoFile.metadata = metadata |
536598cf | 269 | |
d7a25329 | 270 | await createTorrentAndSetInfoHash(video, videoFile) |
536598cf | 271 | |
fb0f7f82 C |
272 | await VideoFileModel.customUpsert(videoFile, 'video', undefined) |
273 | video.VideoFiles = await video.$get('VideoFiles') | |
536598cf C |
274 | |
275 | return video | |
098eb377 | 276 | } |