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