Add ability to generate HLS in CLI
[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   getDurationFromVideoFile,
6   getVideoFileFPS,
7   transcode,
8   TranscodeOptions,
9   TranscodeOptionsType
10 } from '../helpers/ffmpeg-utils'
11 import { copyFile, ensureDir, move, remove, stat } from 'fs-extra'
12 import { logger } from '../helpers/logger'
13 import { VideoResolution } from '../../shared/models/videos'
14 import { VideoFileModel } from '../models/video/video-file'
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'
18 import { CONFIG } from '../initializers/config'
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'
22
23 /**
24  * Optimize the original video file and replace it. The resolution is not changed.
25  */
26 async function optimizeOriginalVideofile (video: MVideoWithFile, inputVideoFileArg?: MVideoFile) {
27   const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
28   const newExtname = '.mp4'
29
30   const inputVideoFile = inputVideoFileArg || video.getMaxQualityFile()
31   const videoInputPath = getVideoFilePath(video, inputVideoFile)
32   const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
33
34   const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
35     ? 'quick-transcode'
36     : 'video'
37
38   const transcodeOptions: TranscodeOptions = {
39     type: transcodeType,
40     inputPath: videoInputPath,
41     outputPath: videoTranscodedPath,
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 = {
85     type: 'video' as 'video',
86     inputPath: videoInputPath,
87     outputPath: videoTranscodedPath,
88     resolution,
89     isPortraitMode: isPortrait
90   }
91
92   await transcode(transcodeOptions)
93
94   return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
95 }
96
97 async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution) {
98   const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
99   const newExtname = '.mp4'
100
101   const inputVideoFile = video.getMaxQualityFile()
102
103   const audioInputPath = getVideoFilePath(video, inputVideoFile)
104   const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
105
106   // If the user updates the video preview during transcoding
107   const previewPath = video.getPreview().getPath()
108   const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
109   await copyFile(previewPath, tmpPreviewPath)
110
111   const transcodeOptions = {
112     type: 'merge-audio' as 'merge-audio',
113     inputPath: tmpPreviewPath,
114     outputPath: videoTranscodedPath,
115     audioPath: audioInputPath,
116     resolution
117   }
118
119   try {
120     await transcode(transcodeOptions)
121
122     await remove(audioInputPath)
123     await remove(tmpPreviewPath)
124   } catch (err) {
125     await remove(tmpPreviewPath)
126     throw err
127   }
128
129   // Important to do this before getVideoFilename() to take in account the new file extension
130   inputVideoFile.extname = newExtname
131
132   const videoOutputPath = getVideoFilePath(video, inputVideoFile)
133   // ffmpeg generated a new video file, so update the video duration
134   // See https://trac.ffmpeg.org/ticket/5456
135   video.duration = await getDurationFromVideoFile(videoTranscodedPath)
136   await video.save()
137
138   return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
139 }
140
141 async function generateHlsPlaylist (video: MVideoWithFile, resolution: VideoResolution, copyCodecs: boolean, isPortraitMode: boolean) {
142   const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
143   await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
144
145   const videoFileInput = copyCodecs
146     ? video.getWebTorrentFile(resolution)
147     : video.getMaxQualityFile()
148
149   const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist()
150   const videoInputPath = getVideoFilePath(videoOrStreamingPlaylist, videoFileInput)
151
152   const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
153   const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
154
155   const transcodeOptions = {
156     type: 'hls' as 'hls',
157     inputPath: videoInputPath,
158     outputPath,
159     resolution,
160     copyCodecs,
161     isPortraitMode,
162
163     hlsPlaylist: {
164       videoFilename
165     }
166   }
167
168   logger.debug('Will run transcode.', { transcodeOptions })
169
170   await transcode(transcodeOptions)
171
172   const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
173
174   const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
175     videoId: video.id,
176     playlistUrl,
177     segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
178     p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
179     p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
180
181     type: VideoStreamingPlaylistType.HLS
182   }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
183   videoStreamingPlaylist.Video = video
184
185   const newVideoFile = new VideoFileModel({
186     resolution,
187     extname: extnameUtil(videoFilename),
188     size: 0,
189     fps: -1,
190     videoStreamingPlaylistId: videoStreamingPlaylist.id
191   })
192
193   const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
194   const stats = await stat(videoFilePath)
195
196   newVideoFile.size = stats.size
197   newVideoFile.fps = await getVideoFileFPS(videoFilePath)
198
199   await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
200
201   const updatedVideoFile = await newVideoFile.save()
202
203   videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles') as VideoFileModel[]
204   videoStreamingPlaylist.VideoFiles.push(updatedVideoFile)
205
206   video.setHLSPlaylist(videoStreamingPlaylist)
207
208   await updateMasterHLSPlaylist(video)
209   await updateSha256Segments(video)
210
211   return video
212 }
213
214 // ---------------------------------------------------------------------------
215
216 export {
217   generateHlsPlaylist,
218   optimizeOriginalVideofile,
219   transcodeNewResolution,
220   mergeAudioVideofile
221 }
222
223 // ---------------------------------------------------------------------------
224
225 async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) {
226   const stats = await stat(transcodingPath)
227   const fps = await getVideoFileFPS(transcodingPath)
228
229   await move(transcodingPath, outputPath)
230
231   videoFile.size = stats.size
232   videoFile.fps = fps
233
234   await createTorrentAndSetInfoHash(video, videoFile)
235
236   const updatedVideoFile = await videoFile.save()
237
238   // Add it if this is a new created file
239   if (video.VideoFiles.some(f => f.id === videoFile.id) === false) {
240     video.VideoFiles.push(updatedVideoFile)
241   }
242
243   return video
244 }