]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-transcoding.ts
Translated using Weblate (Russian)
[github/Chocobozzz/PeerTube.git] / server / lib / video-transcoding.ts
CommitLineData
053aed43 1import { copyFile, ensureDir, move, remove, stat } from 'fs-extra'
d7a25329 2import { basename, extname as extnameUtil, join } from 'path'
053aed43
C
3import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
4import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/types/models'
5import { VideoResolution } from '../../shared/models/videos'
6import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
eba06469
C
7import {
8 canDoQuickTranscode,
9 getDurationFromVideoFile,
ac940348 10 getMetadataFromFile,
eba06469
C
11 getVideoFileFPS,
12 transcode,
13 TranscodeOptions,
14 TranscodeOptionsType
15} from '../helpers/ffmpeg-utils'
098eb377 16import { logger } from '../helpers/logger'
053aed43
C
17import { CONFIG } from '../initializers/config'
18import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
098eb377 19import { VideoFileModel } from '../models/video/video-file'
09209296 20import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
053aed43 21import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls'
d7a25329 22import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths'
098eb377 23
658a47ab
FA
24/**
25 * Optimize the original video file and replace it. The resolution is not changed.
26 */
d7a25329 27async function optimizeOriginalVideofile (video: MVideoWithFile, inputVideoFileArg?: MVideoFile) {
2fbd5e25 28 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
098eb377 29 const newExtname = '.mp4'
9f1ddd24 30
d7a25329
C
31 const inputVideoFile = inputVideoFileArg || video.getMaxQualityFile()
32 const videoInputPath = getVideoFilePath(video, inputVideoFile)
2fbd5e25 33 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
098eb377 34
536598cf
C
35 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
36 ? 'quick-transcode'
37 : 'video'
5ba49f26 38
536598cf 39 const transcodeOptions: TranscodeOptions = {
d7a25329 40 type: transcodeType,
098eb377 41 inputPath: videoInputPath,
09209296 42 outputPath: videoTranscodedPath,
536598cf 43 resolution: inputVideoFile.resolution
098eb377
C
44 }
45
46 // Could be very long!
47 await transcode(transcodeOptions)
48
49 try {
50 await remove(videoInputPath)
51
52 // Important to do this before getVideoFilename() to take in account the new file extension
536598cf 53 inputVideoFile.extname = newExtname
2fbd5e25 54
d7a25329 55 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
098eb377 56
536598cf 57 await onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
098eb377
C
58 } catch (err) {
59 // Auto destruction...
60 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
61
62 throw err
63 }
64}
65
658a47ab
FA
66/**
67 * Transcode the original video file to a lower resolution.
68 */
d7a25329 69async function transcodeNewResolution (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean) {
2fbd5e25 70 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
098eb377
C
71 const extname = '.mp4'
72
73 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
d7a25329 74 const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile())
098eb377
C
75
76 const newVideoFile = new VideoFileModel({
77 resolution,
78 extname,
79 size: 0,
80 videoId: video.id
81 })
d7a25329
C
82 const videoOutputPath = getVideoFilePath(video, newVideoFile)
83 const videoTranscodedPath = join(transcodeDirectory, getVideoFilename(video, newVideoFile))
098eb377 84
5c7d6508 85 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
86 ? {
3a149e9f
C
87 type: 'only-audio' as 'only-audio',
88 inputPath: videoInputPath,
89 outputPath: videoTranscodedPath,
90 resolution
91 }
5c7d6508 92 : {
3a149e9f
C
93 type: 'video' as 'video',
94 inputPath: videoInputPath,
95 outputPath: videoTranscodedPath,
96 resolution,
97 isPortraitMode: isPortrait
98 }
098eb377
C
99
100 await transcode(transcodeOptions)
101
536598cf
C
102 return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
103}
104
d7a25329 105async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution) {
536598cf
C
106 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
107 const newExtname = '.mp4'
108
92e0f42e 109 const inputVideoFile = video.getMinQualityFile()
2fbd5e25 110
d7a25329 111 const audioInputPath = getVideoFilePath(video, inputVideoFile)
536598cf 112 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
098eb377 113
eba06469
C
114 // If the user updates the video preview during transcoding
115 const previewPath = video.getPreview().getPath()
116 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
117 await copyFile(previewPath, tmpPreviewPath)
118
536598cf
C
119 const transcodeOptions = {
120 type: 'merge-audio' as 'merge-audio',
eba06469 121 inputPath: tmpPreviewPath,
536598cf
C
122 outputPath: videoTranscodedPath,
123 audioPath: audioInputPath,
124 resolution
125 }
098eb377 126
eba06469
C
127 try {
128 await transcode(transcodeOptions)
098eb377 129
eba06469
C
130 await remove(audioInputPath)
131 await remove(tmpPreviewPath)
132 } catch (err) {
133 await remove(tmpPreviewPath)
134 throw err
135 }
098eb377 136
536598cf
C
137 // Important to do this before getVideoFilename() to take in account the new file extension
138 inputVideoFile.extname = newExtname
139
d7a25329 140 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
eba06469
C
141 // ffmpeg generated a new video file, so update the video duration
142 // See https://trac.ffmpeg.org/ticket/5456
143 video.duration = await getDurationFromVideoFile(videoTranscodedPath)
144 await video.save()
536598cf
C
145
146 return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
098eb377
C
147}
148
b5b68755
C
149async function generateHlsPlaylist (options: {
150 video: MVideoWithFile
151 videoInputPath: string
152 resolution: VideoResolution
153 copyCodecs: boolean
154 isPortraitMode: boolean
155}) {
156 const { video, videoInputPath, resolution, copyCodecs, isPortraitMode } = options
157
9c6ca37f
C
158 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
159 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
09209296 160
09209296 161 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
d7a25329 162 const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
09209296
C
163
164 const transcodeOptions = {
536598cf 165 type: 'hls' as 'hls',
09209296
C
166 inputPath: videoInputPath,
167 outputPath,
168 resolution,
d7a25329 169 copyCodecs,
09209296 170 isPortraitMode,
4c280004
C
171
172 hlsPlaylist: {
d7a25329 173 videoFilename
4c280004 174 }
09209296
C
175 }
176
d7a25329 177 await transcode(transcodeOptions)
09209296 178
6dd9de95 179 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
09209296 180
d7a25329 181 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
09209296
C
182 videoId: video.id,
183 playlistUrl,
c6c0fa6c 184 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
b5b68755 185 p2pMediaLoaderInfohashes: [],
594d0c6a 186 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
09209296
C
187
188 type: VideoStreamingPlaylistType.HLS
d7a25329
C
189 }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
190 videoStreamingPlaylist.Video = video
191
192 const newVideoFile = new VideoFileModel({
193 resolution,
194 extname: extnameUtil(videoFilename),
195 size: 0,
196 fps: -1,
197 videoStreamingPlaylistId: videoStreamingPlaylist.id
09209296 198 })
d7a25329
C
199
200 const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
201 const stats = await stat(videoFilePath)
202
203 newVideoFile.size = stats.size
204 newVideoFile.fps = await getVideoFileFPS(videoFilePath)
8319d6ae 205 newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
d7a25329
C
206
207 await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
208
c547bbf9 209 await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
e6122097 210 videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
d7a25329 211
b5b68755
C
212 videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
213 playlistUrl, videoStreamingPlaylist.VideoFiles
214 )
215 await videoStreamingPlaylist.save()
216
d7a25329
C
217 video.setHLSPlaylist(videoStreamingPlaylist)
218
219 await updateMasterHLSPlaylist(video)
c6c0fa6c 220 await updateSha256VODSegments(video)
d7a25329
C
221
222 return video
09209296
C
223}
224
536598cf
C
225// ---------------------------------------------------------------------------
226
098eb377 227export {
09209296 228 generateHlsPlaylist,
d7a25329
C
229 optimizeOriginalVideofile,
230 transcodeNewResolution,
536598cf
C
231 mergeAudioVideofile
232}
233
234// ---------------------------------------------------------------------------
235
453e83ea 236async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) {
536598cf
C
237 const stats = await stat(transcodingPath)
238 const fps = await getVideoFileFPS(transcodingPath)
8319d6ae 239 const metadata = await getMetadataFromFile(transcodingPath)
536598cf 240
fb0f7f82 241 await move(transcodingPath, outputPath, { overwrite: true })
536598cf 242
453e83ea
C
243 videoFile.size = stats.size
244 videoFile.fps = fps
8319d6ae 245 videoFile.metadata = metadata
536598cf 246
d7a25329 247 await createTorrentAndSetInfoHash(video, videoFile)
536598cf 248
fb0f7f82
C
249 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
250 video.VideoFiles = await video.$get('VideoFiles')
536598cf
C
251
252 return video
098eb377 253}