]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-transcoding.ts
05afb44d1bcf76cecbf533f16ffd98d11fddbaed
[github/Chocobozzz/PeerTube.git] / server / lib / video-transcoding.ts
1 import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER, VIDEO_TRANSCODING_FPS } 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 async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
15 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
16 const newExtname = '.mp4'
17
18 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
19 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
20 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
21
22 const doQuickTranscode = await(canDoQuickTranscode(videoInputPath))
23
24 const transcodeOptions = {
25 inputPath: videoInputPath,
26 outputPath: videoTranscodedPath,
27 resolution: inputVideoFile.resolution,
28 doQuickTranscode
29 }
30
31 // Could be very long!
32 await transcode(transcodeOptions)
33
34 try {
35 await remove(videoInputPath)
36
37 // Important to do this before getVideoFilename() to take in account the new file extension
38 inputVideoFile.set('extname', newExtname)
39
40 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
41 await move(videoTranscodedPath, videoOutputPath)
42 const stats = await stat(videoOutputPath)
43 const fps = await getVideoFileFPS(videoOutputPath)
44
45 inputVideoFile.set('size', stats.size)
46 inputVideoFile.set('fps', fps)
47
48 await video.createTorrentAndSetInfoHash(inputVideoFile)
49 await inputVideoFile.save()
50 } catch (err) {
51 // Auto destruction...
52 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
53
54 throw err
55 }
56 }
57
58 async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
59 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
60 const extname = '.mp4'
61
62 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
63 const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
64
65 const newVideoFile = new VideoFileModel({
66 resolution,
67 extname,
68 size: 0,
69 videoId: video.id
70 })
71 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
72
73 const transcodeOptions = {
74 inputPath: videoInputPath,
75 outputPath: videoOutputPath,
76 resolution,
77 isPortraitMode: isPortrait
78 }
79
80 await transcode(transcodeOptions)
81
82 const stats = await stat(videoOutputPath)
83 const fps = await getVideoFileFPS(videoOutputPath)
84
85 newVideoFile.set('size', stats.size)
86 newVideoFile.set('fps', fps)
87
88 await video.createTorrentAndSetInfoHash(newVideoFile)
89
90 await newVideoFile.save()
91
92 video.VideoFiles.push(newVideoFile)
93 }
94
95 async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
96 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
97 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
98
99 const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile()))
100 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
101
102 const transcodeOptions = {
103 inputPath: videoInputPath,
104 outputPath,
105 resolution,
106 isPortraitMode,
107
108 hlsPlaylist: {
109 videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
110 }
111 }
112
113 await transcode(transcodeOptions)
114
115 await updateMasterHLSPlaylist(video)
116 await updateSha256Segments(video)
117
118 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
119
120 await VideoStreamingPlaylistModel.upsert({
121 videoId: video.id,
122 playlistUrl,
123 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
124 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
125 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
126
127 type: VideoStreamingPlaylistType.HLS
128 })
129 }
130
131 export {
132 generateHlsPlaylist,
133 optimizeVideofile,
134 transcodeOriginalVideofile
135 }