]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-transcoding.ts
Redundancy and search tests in parallel too
[github/Chocobozzz/PeerTube.git] / server / lib / video-transcoding.ts
CommitLineData
74dc3bca 1import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
30842128
C
2import { join } from 'path'
3import { getVideoFileFPS, transcode } from '../helpers/ffmpeg-utils'
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
9f1ddd24 14async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
098eb377
C
15 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
16 const newExtname = '.mp4'
9f1ddd24
C
17
18 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
19 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
098eb377
C
20 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
21
22 const transcodeOptions = {
23 inputPath: videoInputPath,
09209296
C
24 outputPath: videoTranscodedPath,
25 resolution: inputVideoFile.resolution
098eb377
C
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)
f481c4f9 38 await move(videoTranscodedPath, videoOutputPath)
098eb377
C
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
09209296 55async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
098eb377
C
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 })
09209296 68 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
098eb377
C
69
70 const transcodeOptions = {
71 inputPath: videoInputPath,
72 outputPath: videoOutputPath,
73 resolution,
09209296 74 isPortraitMode: isPortrait
098eb377
C
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
09209296 92async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
9c6ca37f
C
93 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
94 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
09209296
C
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,
4c280004
C
104
105 hlsPlaylist: {
106 videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
107 }
09209296
C
108 }
109
110 await transcode(transcodeOptions)
111
112 await updateMasterHLSPlaylist(video)
113 await updateSha256Segments(video)
114
6dd9de95 115 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
09209296
C
116
117 await VideoStreamingPlaylistModel.upsert({
118 videoId: video.id,
119 playlistUrl,
6dd9de95 120 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
09209296 121 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
594d0c6a 122 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
09209296
C
123
124 type: VideoStreamingPlaylistType.HLS
125 })
126}
127
098eb377 128export {
09209296 129 generateHlsPlaylist,
edb4ffc7 130 optimizeVideofile,
30842128 131 transcodeOriginalVideofile
098eb377 132}