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