d6b6b251a931994c0918e5584f05c1871096583b
[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 { join } from 'path'
3 import { getVideoFileFPS, transcode, canDoQuickTranscode } from '../helpers/ffmpeg-utils'
4 import { ensureDir, move, remove, stat } from 'fs-extra'
5 import { logger } from '../helpers/logger'
6 import { VideoResolution } from '../../shared/models/videos'
7 import { VideoFileModel } from '../models/video/video-file'
8 import { VideoModel } from '../models/video/video'
9 import { updateMasterHLSPlaylist, updateSha256Segments } from './hls'
10 import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
11 import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
12 import { CONFIG } from '../initializers/config'
13
14 /**
15  * Optimize the original video file and replace it. The resolution is not changed.
16  */
17 async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
18   const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
19   const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
20   const newExtname = '.mp4'
21
22   const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
23   const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
24   const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
25
26   const doQuickTranscode = await(canDoQuickTranscode(videoInputPath))
27
28   const transcodeOptions = {
29     inputPath: videoInputPath,
30     outputPath: videoTranscodedPath,
31     resolution: inputVideoFile.resolution,
32     doQuickTranscode
33   }
34
35   // Could be very long!
36   await transcode(transcodeOptions)
37
38   try {
39     await remove(videoInputPath)
40
41     // Important to do this before getVideoFilename() to take in account the new file extension
42     inputVideoFile.set('extname', newExtname)
43
44     const stats = await stat(videoTranscodedPath)
45     const fps = await getVideoFileFPS(videoTranscodedPath)
46
47     const videoOutputPath = video.getVideoFilePath(inputVideoFile)
48     await move(videoTranscodedPath, videoOutputPath)
49
50     inputVideoFile.set('size', stats.size)
51     inputVideoFile.set('fps', fps)
52
53     await video.createTorrentAndSetInfoHash(inputVideoFile)
54     await inputVideoFile.save()
55   } catch (err) {
56     // Auto destruction...
57     video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
58
59     throw err
60   }
61 }
62
63 /**
64  * Transcode the original video file to a lower resolution.
65  */
66 async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
67   const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
68   const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
69   const extname = '.mp4'
70
71   // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
72   const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
73
74   const newVideoFile = new VideoFileModel({
75     resolution,
76     extname,
77     size: 0,
78     videoId: video.id
79   })
80   const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
81   const videoTranscodedPath = join(transcodeDirectory, video.getVideoFilename(newVideoFile))
82
83   const transcodeOptions = {
84     inputPath: videoInputPath,
85     outputPath: videoTranscodedPath,
86     resolution,
87     isPortraitMode: isPortrait
88   }
89
90   await transcode(transcodeOptions)
91
92   const stats = await stat(videoTranscodedPath)
93   const fps = await getVideoFileFPS(videoTranscodedPath)
94
95   await move(videoTranscodedPath, videoOutputPath)
96
97   newVideoFile.set('size', stats.size)
98   newVideoFile.set('fps', fps)
99
100   await video.createTorrentAndSetInfoHash(newVideoFile)
101
102   await newVideoFile.save()
103
104   video.VideoFiles.push(newVideoFile)
105 }
106
107 async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
108   const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
109   await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
110
111   const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile()))
112   const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
113
114   const transcodeOptions = {
115     inputPath: videoInputPath,
116     outputPath,
117     resolution,
118     isPortraitMode,
119
120     hlsPlaylist: {
121       videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
122     }
123   }
124
125   await transcode(transcodeOptions)
126
127   await updateMasterHLSPlaylist(video)
128   await updateSha256Segments(video)
129
130   const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
131
132   await VideoStreamingPlaylistModel.upsert({
133     videoId: video.id,
134     playlistUrl,
135     segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
136     p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
137     p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
138
139     type: VideoStreamingPlaylistType.HLS
140   })
141 }
142
143 export {
144   generateHlsPlaylist,
145   optimizeVideofile,
146   transcodeOriginalVideofile
147 }