]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-transcoding.ts
Fallback HLS to webtorrent
[github/Chocobozzz/PeerTube.git] / server / lib / video-transcoding.ts
CommitLineData
09209296 1import { CONFIG, HLS_PLAYLIST_DIRECTORY } from '../initializers'
9f1ddd24 2import { extname, join } from 'path'
098eb377 3import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils'
09209296 4import { copy, ensureDir, move, remove, stat } from 'fs-extra'
098eb377
C
5import { logger } from '../helpers/logger'
6import { VideoResolution } from '../../shared/models/videos'
7import { VideoFileModel } from '../models/video/video-file'
8import { VideoModel } from '../models/video/video'
09209296
C
9import { updateMasterHLSPlaylist, updateSha256Segments } from './hls'
10import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
11import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
098eb377 12
9f1ddd24 13async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
098eb377
C
14 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
15 const newExtname = '.mp4'
9f1ddd24
C
16
17 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
18 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
098eb377
C
19 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
20
21 const transcodeOptions = {
22 inputPath: videoInputPath,
09209296
C
23 outputPath: videoTranscodedPath,
24 resolution: inputVideoFile.resolution
098eb377
C
25 }
26
27 // Could be very long!
28 await transcode(transcodeOptions)
29
30 try {
31 await remove(videoInputPath)
32
33 // Important to do this before getVideoFilename() to take in account the new file extension
34 inputVideoFile.set('extname', newExtname)
35
36 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
f481c4f9 37 await move(videoTranscodedPath, videoOutputPath)
098eb377
C
38 const stats = await stat(videoOutputPath)
39 const fps = await getVideoFileFPS(videoOutputPath)
40
41 inputVideoFile.set('size', stats.size)
42 inputVideoFile.set('fps', fps)
43
44 await video.createTorrentAndSetInfoHash(inputVideoFile)
45 await inputVideoFile.save()
46 } catch (err) {
47 // Auto destruction...
48 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
49
50 throw err
51 }
52}
53
09209296 54async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
098eb377
C
55 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
56 const extname = '.mp4'
57
58 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
59 const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
60
61 const newVideoFile = new VideoFileModel({
62 resolution,
63 extname,
64 size: 0,
65 videoId: video.id
66 })
09209296 67 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
098eb377
C
68
69 const transcodeOptions = {
70 inputPath: videoInputPath,
71 outputPath: videoOutputPath,
72 resolution,
09209296 73 isPortraitMode: isPortrait
098eb377
C
74 }
75
76 await transcode(transcodeOptions)
77
78 const stats = await stat(videoOutputPath)
79 const fps = await getVideoFileFPS(videoOutputPath)
80
81 newVideoFile.set('size', stats.size)
82 newVideoFile.set('fps', fps)
83
84 await video.createTorrentAndSetInfoHash(newVideoFile)
85
86 await newVideoFile.save()
87
88 video.VideoFiles.push(newVideoFile)
89}
90
09209296
C
91async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
92 const baseHlsDirectory = join(HLS_PLAYLIST_DIRECTORY, video.uuid)
93 await ensureDir(join(HLS_PLAYLIST_DIRECTORY, video.uuid))
94
95 const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile()))
96 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
97
98 const transcodeOptions = {
99 inputPath: videoInputPath,
100 outputPath,
101 resolution,
102 isPortraitMode,
103 generateHlsPlaylist: true
104 }
105
106 await transcode(transcodeOptions)
107
108 await updateMasterHLSPlaylist(video)
109 await updateSha256Segments(video)
110
111 const playlistUrl = CONFIG.WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
112
113 await VideoStreamingPlaylistModel.upsert({
114 videoId: video.id,
115 playlistUrl,
116 segmentsSha256Url: CONFIG.WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
117 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
118
119 type: VideoStreamingPlaylistType.HLS
120 })
121}
122
098eb377
C
123async function importVideoFile (video: VideoModel, inputFilePath: string) {
124 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
125 const { size } = await stat(inputFilePath)
126 const fps = await getVideoFileFPS(inputFilePath)
127
128 let updatedVideoFile = new VideoFileModel({
129 resolution: videoFileResolution,
130 extname: extname(inputFilePath),
131 size,
132 fps,
133 videoId: video.id
134 })
135
136 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
137
138 if (currentVideoFile) {
139 // Remove old file and old torrent
140 await video.removeFile(currentVideoFile)
141 await video.removeTorrent(currentVideoFile)
142 // Remove the old video file from the array
143 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
144
145 // Update the database
146 currentVideoFile.set('extname', updatedVideoFile.extname)
147 currentVideoFile.set('size', updatedVideoFile.size)
148 currentVideoFile.set('fps', updatedVideoFile.fps)
149
150 updatedVideoFile = currentVideoFile
151 }
152
153 const outputPath = video.getVideoFilePath(updatedVideoFile)
154 await copy(inputFilePath, outputPath)
155
156 await video.createTorrentAndSetInfoHash(updatedVideoFile)
157
158 await updatedVideoFile.save()
159
160 video.VideoFiles.push(updatedVideoFile)
161}
162
163export {
09209296 164 generateHlsPlaylist,
edb4ffc7 165 optimizeVideofile,
098eb377
C
166 transcodeOriginalVideofile,
167 importVideoFile
168}