]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-transcoding.ts
Merge branch 'release/1.4.0' into develop
[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'
536598cf 3import { canDoQuickTranscode, getVideoFileFPS, transcode, TranscodeOptions, TranscodeOptionsType } 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'
09209296
C
8import { updateMasterHLSPlaylist, updateSha256Segments } from './hls'
9import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
10import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
6dd9de95 11import { CONFIG } from '../initializers/config'
453e83ea 12import { MVideoFile, MVideoWithFile, MVideoWithFileThumbnail } from '@server/typings/models'
098eb377 13
658a47ab
FA
14/**
15 * Optimize the original video file and replace it. The resolution is not changed.
16 */
453e83ea 17async function optimizeVideofile (video: MVideoWithFile, inputVideoFileArg?: MVideoFile) {
098eb377 18 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
2fbd5e25 19 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
098eb377 20 const newExtname = '.mp4'
9f1ddd24
C
21
22 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
23 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
2fbd5e25 24 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
098eb377 25
536598cf
C
26 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
27 ? 'quick-transcode'
28 : 'video'
5ba49f26 29
536598cf
C
30 const transcodeOptions: TranscodeOptions = {
31 type: transcodeType as any, // FIXME: typing issue
098eb377 32 inputPath: videoInputPath,
09209296 33 outputPath: videoTranscodedPath,
536598cf 34 resolution: inputVideoFile.resolution
098eb377
C
35 }
36
37 // Could be very long!
38 await transcode(transcodeOptions)
39
40 try {
41 await remove(videoInputPath)
42
43 // Important to do this before getVideoFilename() to take in account the new file extension
536598cf 44 inputVideoFile.extname = newExtname
2fbd5e25 45
098eb377 46 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
098eb377 47
536598cf 48 await onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
098eb377
C
49 } catch (err) {
50 // Auto destruction...
51 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
52
53 throw err
54 }
55}
56
658a47ab
FA
57/**
58 * Transcode the original video file to a lower resolution.
59 */
453e83ea 60async function transcodeOriginalVideofile (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean) {
098eb377 61 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
2fbd5e25 62 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
098eb377
C
63 const extname = '.mp4'
64
65 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
66 const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
67
68 const newVideoFile = new VideoFileModel({
69 resolution,
70 extname,
71 size: 0,
72 videoId: video.id
73 })
09209296 74 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
2fbd5e25 75 const videoTranscodedPath = join(transcodeDirectory, video.getVideoFilename(newVideoFile))
098eb377
C
76
77 const transcodeOptions = {
536598cf 78 type: 'video' as 'video',
098eb377 79 inputPath: videoInputPath,
2fbd5e25 80 outputPath: videoTranscodedPath,
098eb377 81 resolution,
09209296 82 isPortraitMode: isPortrait
098eb377
C
83 }
84
85 await transcode(transcodeOptions)
86
536598cf
C
87 return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
88}
89
453e83ea 90async function mergeAudioVideofile (video: MVideoWithFileThumbnail, resolution: VideoResolution) {
536598cf
C
91 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
92 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
93 const newExtname = '.mp4'
94
95 const inputVideoFile = video.getOriginalFile()
2fbd5e25 96
536598cf
C
97 const audioInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
98 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
098eb377 99
536598cf
C
100 const transcodeOptions = {
101 type: 'merge-audio' as 'merge-audio',
102 inputPath: video.getPreview().getPath(),
103 outputPath: videoTranscodedPath,
104 audioPath: audioInputPath,
105 resolution
106 }
098eb377 107
536598cf 108 await transcode(transcodeOptions)
098eb377 109
536598cf 110 await remove(audioInputPath)
098eb377 111
536598cf
C
112 // Important to do this before getVideoFilename() to take in account the new file extension
113 inputVideoFile.extname = newExtname
114
115 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
116
117 return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
098eb377
C
118}
119
453e83ea 120async function generateHlsPlaylist (video: MVideoWithFile, resolution: VideoResolution, isPortraitMode: boolean) {
9c6ca37f
C
121 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
122 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
09209296 123
29d4e137 124 const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getFile(resolution)))
09209296
C
125 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
126
127 const transcodeOptions = {
536598cf 128 type: 'hls' as 'hls',
09209296
C
129 inputPath: videoInputPath,
130 outputPath,
131 resolution,
132 isPortraitMode,
4c280004
C
133
134 hlsPlaylist: {
135 videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
136 }
09209296
C
137 }
138
139 await transcode(transcodeOptions)
140
141 await updateMasterHLSPlaylist(video)
142 await updateSha256Segments(video)
143
6dd9de95 144 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
09209296
C
145
146 await VideoStreamingPlaylistModel.upsert({
147 videoId: video.id,
148 playlistUrl,
6dd9de95 149 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
09209296 150 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
594d0c6a 151 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
09209296
C
152
153 type: VideoStreamingPlaylistType.HLS
154 })
155}
156
536598cf
C
157// ---------------------------------------------------------------------------
158
098eb377 159export {
09209296 160 generateHlsPlaylist,
edb4ffc7 161 optimizeVideofile,
536598cf
C
162 transcodeOriginalVideofile,
163 mergeAudioVideofile
164}
165
166// ---------------------------------------------------------------------------
167
453e83ea 168async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) {
536598cf
C
169 const stats = await stat(transcodingPath)
170 const fps = await getVideoFileFPS(transcodingPath)
171
172 await move(transcodingPath, outputPath)
173
453e83ea
C
174 videoFile.size = stats.size
175 videoFile.fps = fps
536598cf
C
176
177 await video.createTorrentAndSetInfoHash(videoFile)
178
179 const updatedVideoFile = await videoFile.save()
180
181 // Add it if this is a new created file
182 if (video.VideoFiles.some(f => f.id === videoFile.id) === false) {
183 video.VideoFiles.push(updatedVideoFile)
184 }
185
186 return video
098eb377 187}