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