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