]>
Commit | Line | Data |
---|---|---|
7ed2c1a4 | 1 | import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants' |
d7a25329 | 2 | import { basename, extname as extnameUtil, join } from 'path' |
eba06469 C |
3 | import { |
4 | canDoQuickTranscode, | |
5 | getDurationFromVideoFile, | |
6 | getVideoFileFPS, | |
7 | transcode, | |
8 | TranscodeOptions, | |
9 | TranscodeOptionsType | |
10 | } from '../helpers/ffmpeg-utils' | |
11 | import { copyFile, ensureDir, move, remove, stat } from 'fs-extra' | |
098eb377 C |
12 | import { logger } from '../helpers/logger' |
13 | import { VideoResolution } from '../../shared/models/videos' | |
14 | import { VideoFileModel } from '../models/video/video-file' | |
09209296 C |
15 | import { updateMasterHLSPlaylist, updateSha256Segments } from './hls' |
16 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' | |
17 | import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type' | |
6dd9de95 | 18 | import { CONFIG } from '../initializers/config' |
d7a25329 C |
19 | import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/typings/models' |
20 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | |
21 | import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths' | |
098eb377 | 22 | |
658a47ab FA |
23 | /** |
24 | * Optimize the original video file and replace it. The resolution is not changed. | |
25 | */ | |
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, |
098eb377 | 40 | inputPath: videoInputPath, |
09209296 | 41 | outputPath: videoTranscodedPath, |
536598cf | 42 | resolution: inputVideoFile.resolution |
098eb377 C |
43 | } |
44 | ||
45 | // Could be very long! | |
46 | await transcode(transcodeOptions) | |
47 | ||
48 | try { | |
49 | await remove(videoInputPath) | |
50 | ||
51 | // Important to do this before getVideoFilename() to take in account the new file extension | |
536598cf | 52 | inputVideoFile.extname = newExtname |
2fbd5e25 | 53 | |
d7a25329 | 54 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) |
098eb377 | 55 | |
536598cf | 56 | await onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) |
098eb377 C |
57 | } catch (err) { |
58 | // Auto destruction... | |
59 | video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err })) | |
60 | ||
61 | throw err | |
62 | } | |
63 | } | |
64 | ||
658a47ab FA |
65 | /** |
66 | * Transcode the original video file to a lower resolution. | |
67 | */ | |
d7a25329 | 68 | async function transcodeNewResolution (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean) { |
2fbd5e25 | 69 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
098eb377 C |
70 | const extname = '.mp4' |
71 | ||
72 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed | |
d7a25329 | 73 | const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile()) |
098eb377 C |
74 | |
75 | const newVideoFile = new VideoFileModel({ | |
76 | resolution, | |
77 | extname, | |
78 | size: 0, | |
79 | videoId: video.id | |
80 | }) | |
d7a25329 C |
81 | const videoOutputPath = getVideoFilePath(video, newVideoFile) |
82 | const videoTranscodedPath = join(transcodeDirectory, getVideoFilename(video, newVideoFile)) | |
098eb377 | 83 | |
5c7d6508 | 84 | const transcodeOptions = resolution === VideoResolution.H_NOVIDEO |
85 | ? { | |
3a149e9f C |
86 | type: 'only-audio' as 'only-audio', |
87 | inputPath: videoInputPath, | |
88 | outputPath: videoTranscodedPath, | |
89 | resolution | |
90 | } | |
5c7d6508 | 91 | : { |
3a149e9f C |
92 | type: 'video' as 'video', |
93 | inputPath: videoInputPath, | |
94 | outputPath: videoTranscodedPath, | |
95 | resolution, | |
96 | isPortraitMode: isPortrait | |
97 | } | |
098eb377 C |
98 | |
99 | await transcode(transcodeOptions) | |
100 | ||
536598cf C |
101 | return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath) |
102 | } | |
103 | ||
d7a25329 | 104 | async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution) { |
536598cf C |
105 | const transcodeDirectory = CONFIG.STORAGE.TMP_DIR |
106 | const newExtname = '.mp4' | |
107 | ||
92e0f42e | 108 | const inputVideoFile = video.getMinQualityFile() |
2fbd5e25 | 109 | |
d7a25329 | 110 | const audioInputPath = getVideoFilePath(video, inputVideoFile) |
536598cf | 111 | const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname) |
098eb377 | 112 | |
eba06469 C |
113 | // If the user updates the video preview during transcoding |
114 | const previewPath = video.getPreview().getPath() | |
115 | const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath)) | |
116 | await copyFile(previewPath, tmpPreviewPath) | |
117 | ||
536598cf C |
118 | const transcodeOptions = { |
119 | type: 'merge-audio' as 'merge-audio', | |
eba06469 | 120 | inputPath: tmpPreviewPath, |
536598cf C |
121 | outputPath: videoTranscodedPath, |
122 | audioPath: audioInputPath, | |
123 | resolution | |
124 | } | |
098eb377 | 125 | |
eba06469 C |
126 | try { |
127 | await transcode(transcodeOptions) | |
098eb377 | 128 | |
eba06469 C |
129 | await remove(audioInputPath) |
130 | await remove(tmpPreviewPath) | |
131 | } catch (err) { | |
132 | await remove(tmpPreviewPath) | |
133 | throw err | |
134 | } | |
098eb377 | 135 | |
536598cf C |
136 | // Important to do this before getVideoFilename() to take in account the new file extension |
137 | inputVideoFile.extname = newExtname | |
138 | ||
d7a25329 | 139 | const videoOutputPath = getVideoFilePath(video, inputVideoFile) |
eba06469 C |
140 | // ffmpeg generated a new video file, so update the video duration |
141 | // See https://trac.ffmpeg.org/ticket/5456 | |
142 | video.duration = await getDurationFromVideoFile(videoTranscodedPath) | |
143 | await video.save() | |
536598cf C |
144 | |
145 | return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath) | |
098eb377 C |
146 | } |
147 | ||
d7a25329 | 148 | async function generateHlsPlaylist (video: MVideoWithFile, resolution: VideoResolution, copyCodecs: boolean, isPortraitMode: boolean) { |
9c6ca37f C |
149 | const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) |
150 | await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)) | |
09209296 | 151 | |
d7a25329 C |
152 | const videoFileInput = copyCodecs |
153 | ? video.getWebTorrentFile(resolution) | |
154 | : video.getMaxQualityFile() | |
155 | ||
156 | const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist() | |
157 | const videoInputPath = getVideoFilePath(videoOrStreamingPlaylist, videoFileInput) | |
158 | ||
09209296 | 159 | const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)) |
d7a25329 | 160 | const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution) |
09209296 C |
161 | |
162 | const transcodeOptions = { | |
536598cf | 163 | type: 'hls' as 'hls', |
09209296 C |
164 | inputPath: videoInputPath, |
165 | outputPath, | |
166 | resolution, | |
d7a25329 | 167 | copyCodecs, |
09209296 | 168 | isPortraitMode, |
4c280004 C |
169 | |
170 | hlsPlaylist: { | |
d7a25329 | 171 | videoFilename |
4c280004 | 172 | } |
09209296 C |
173 | } |
174 | ||
d7a25329 | 175 | logger.debug('Will run transcode.', { transcodeOptions }) |
09209296 | 176 | |
d7a25329 | 177 | await transcode(transcodeOptions) |
09209296 | 178 | |
6dd9de95 | 179 | const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) |
09209296 | 180 | |
d7a25329 | 181 | const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ |
09209296 C |
182 | videoId: video.id, |
183 | playlistUrl, | |
6dd9de95 | 184 | segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid), |
09209296 | 185 | p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles), |
594d0c6a | 186 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, |
09209296 C |
187 | |
188 | type: VideoStreamingPlaylistType.HLS | |
d7a25329 C |
189 | }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ] |
190 | videoStreamingPlaylist.Video = video | |
191 | ||
192 | const newVideoFile = new VideoFileModel({ | |
193 | resolution, | |
194 | extname: extnameUtil(videoFilename), | |
195 | size: 0, | |
196 | fps: -1, | |
197 | videoStreamingPlaylistId: videoStreamingPlaylist.id | |
09209296 | 198 | }) |
d7a25329 C |
199 | |
200 | const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile) | |
201 | const stats = await stat(videoFilePath) | |
202 | ||
203 | newVideoFile.size = stats.size | |
204 | newVideoFile.fps = await getVideoFileFPS(videoFilePath) | |
205 | ||
206 | await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile) | |
207 | ||
49c3bf6f | 208 | await newVideoFile.save() |
d7a25329 | 209 | videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles') as VideoFileModel[] |
d7a25329 C |
210 | |
211 | video.setHLSPlaylist(videoStreamingPlaylist) | |
212 | ||
213 | await updateMasterHLSPlaylist(video) | |
214 | await updateSha256Segments(video) | |
215 | ||
216 | return video | |
09209296 C |
217 | } |
218 | ||
536598cf C |
219 | // --------------------------------------------------------------------------- |
220 | ||
098eb377 | 221 | export { |
09209296 | 222 | generateHlsPlaylist, |
d7a25329 C |
223 | optimizeOriginalVideofile, |
224 | transcodeNewResolution, | |
536598cf C |
225 | mergeAudioVideofile |
226 | } | |
227 | ||
228 | // --------------------------------------------------------------------------- | |
229 | ||
453e83ea | 230 | async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) { |
536598cf C |
231 | const stats = await stat(transcodingPath) |
232 | const fps = await getVideoFileFPS(transcodingPath) | |
233 | ||
234 | await move(transcodingPath, outputPath) | |
235 | ||
453e83ea C |
236 | videoFile.size = stats.size |
237 | videoFile.fps = fps | |
536598cf | 238 | |
d7a25329 | 239 | await createTorrentAndSetInfoHash(video, videoFile) |
536598cf C |
240 | |
241 | const updatedVideoFile = await videoFile.save() | |
242 | ||
243 | // Add it if this is a new created file | |
244 | if (video.VideoFiles.some(f => f.id === videoFile.id) === false) { | |
245 | video.VideoFiles.push(updatedVideoFile) | |
246 | } | |
247 | ||
248 | return video | |
098eb377 | 249 | } |