]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-transcoding.ts
aaad219ddda5692defc2187f91d55c51207a8666
[github/Chocobozzz/PeerTube.git] / server / lib / video-transcoding.ts
1 import { copyFile, ensureDir, move, remove, stat } from 'fs-extra'
2 import { basename, extname as extnameUtil, join } from 'path'
3 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
4 import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/types/models'
5 import { VideoResolution } from '../../shared/models/videos'
6 import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
7 import { transcode, TranscodeOptions, TranscodeOptionsType } from '../helpers/ffmpeg-utils'
8 import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../helpers/ffprobe-utils'
9 import { logger } from '../helpers/logger'
10 import { CONFIG } from '../initializers/config'
11 import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
12 import { VideoFileModel } from '../models/video/video-file'
13 import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
14 import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls'
15 import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths'
16 import { availableEncoders } from './video-transcoding-profiles'
17
18 /**
19 * Optimize the original video file and replace it. The resolution is not changed.
20 */
21 async function optimizeOriginalVideofile (video: MVideoWithFile, inputVideoFileArg?: MVideoFile) {
22 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
23 const newExtname = '.mp4'
24
25 const inputVideoFile = inputVideoFileArg || video.getMaxQualityFile()
26 const videoInputPath = getVideoFilePath(video, inputVideoFile)
27 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
28
29 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
30 ? 'quick-transcode'
31 : 'video'
32
33 const transcodeOptions: TranscodeOptions = {
34 type: transcodeType,
35
36 inputPath: videoInputPath,
37 outputPath: videoTranscodedPath,
38
39 availableEncoders,
40 profile: 'default',
41
42 resolution: inputVideoFile.resolution
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
52 inputVideoFile.extname = newExtname
53
54 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
55
56 await onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
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
65 /**
66 * Transcode the original video file to a lower resolution.
67 */
68 async function transcodeNewResolution (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean) {
69 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
70 const extname = '.mp4'
71
72 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
73 const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile())
74
75 const newVideoFile = new VideoFileModel({
76 resolution,
77 extname,
78 size: 0,
79 videoId: video.id
80 })
81 const videoOutputPath = getVideoFilePath(video, newVideoFile)
82 const videoTranscodedPath = join(transcodeDirectory, getVideoFilename(video, newVideoFile))
83
84 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
85 ? {
86 type: 'only-audio' as 'only-audio',
87
88 inputPath: videoInputPath,
89 outputPath: videoTranscodedPath,
90
91 availableEncoders,
92 profile: 'default',
93
94 resolution
95 }
96 : {
97 type: 'video' as 'video',
98 inputPath: videoInputPath,
99 outputPath: videoTranscodedPath,
100
101 availableEncoders,
102 profile: 'default',
103
104 resolution,
105 isPortraitMode: isPortrait
106 }
107
108 await transcode(transcodeOptions)
109
110 return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
111 }
112
113 async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution) {
114 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
115 const newExtname = '.mp4'
116
117 const inputVideoFile = video.getMinQualityFile()
118
119 const audioInputPath = getVideoFilePath(video, inputVideoFile)
120 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
121
122 // If the user updates the video preview during transcoding
123 const previewPath = video.getPreview().getPath()
124 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
125 await copyFile(previewPath, tmpPreviewPath)
126
127 const transcodeOptions = {
128 type: 'merge-audio' as 'merge-audio',
129
130 inputPath: tmpPreviewPath,
131 outputPath: videoTranscodedPath,
132
133 availableEncoders,
134 profile: 'default',
135
136 audioPath: audioInputPath,
137 resolution
138 }
139
140 try {
141 await transcode(transcodeOptions)
142
143 await remove(audioInputPath)
144 await remove(tmpPreviewPath)
145 } catch (err) {
146 await remove(tmpPreviewPath)
147 throw err
148 }
149
150 // Important to do this before getVideoFilename() to take in account the new file extension
151 inputVideoFile.extname = newExtname
152
153 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
154 // ffmpeg generated a new video file, so update the video duration
155 // See https://trac.ffmpeg.org/ticket/5456
156 video.duration = await getDurationFromVideoFile(videoTranscodedPath)
157 await video.save()
158
159 return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
160 }
161
162 async function generateHlsPlaylist (options: {
163 video: MVideoWithFile
164 videoInputPath: string
165 resolution: VideoResolution
166 copyCodecs: boolean
167 isPortraitMode: boolean
168 }) {
169 const { video, videoInputPath, resolution, copyCodecs, isPortraitMode } = options
170
171 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
172 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
173
174 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
175 const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
176
177 const transcodeOptions = {
178 type: 'hls' as 'hls',
179
180 inputPath: videoInputPath,
181 outputPath,
182
183 availableEncoders,
184 profile: 'default',
185
186 resolution,
187 copyCodecs,
188 isPortraitMode,
189
190 hlsPlaylist: {
191 videoFilename
192 }
193 }
194
195 await transcode(transcodeOptions)
196
197 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
198
199 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
200 videoId: video.id,
201 playlistUrl,
202 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
203 p2pMediaLoaderInfohashes: [],
204 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
205
206 type: VideoStreamingPlaylistType.HLS
207 }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
208 videoStreamingPlaylist.Video = video
209
210 const newVideoFile = new VideoFileModel({
211 resolution,
212 extname: extnameUtil(videoFilename),
213 size: 0,
214 fps: -1,
215 videoStreamingPlaylistId: videoStreamingPlaylist.id
216 })
217
218 const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
219 const stats = await stat(videoFilePath)
220
221 newVideoFile.size = stats.size
222 newVideoFile.fps = await getVideoFileFPS(videoFilePath)
223 newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
224
225 await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
226
227 await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
228 videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
229
230 videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
231 playlistUrl, videoStreamingPlaylist.VideoFiles
232 )
233 await videoStreamingPlaylist.save()
234
235 video.setHLSPlaylist(videoStreamingPlaylist)
236
237 await updateMasterHLSPlaylist(video)
238 await updateSha256VODSegments(video)
239
240 return video
241 }
242
243 // ---------------------------------------------------------------------------
244
245 export {
246 generateHlsPlaylist,
247 optimizeOriginalVideofile,
248 transcodeNewResolution,
249 mergeAudioVideofile
250 }
251
252 // ---------------------------------------------------------------------------
253
254 async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) {
255 const stats = await stat(transcodingPath)
256 const fps = await getVideoFileFPS(transcodingPath)
257 const metadata = await getMetadataFromFile(transcodingPath)
258
259 await move(transcodingPath, outputPath, { overwrite: true })
260
261 videoFile.size = stats.size
262 videoFile.fps = fps
263 videoFile.metadata = metadata
264
265 await createTorrentAndSetInfoHash(video, videoFile)
266
267 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
268 video.VideoFiles = await video.$get('VideoFiles')
269
270 return video
271 }