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'
14 async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
15 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
16 const newExtname = '.mp4'
18 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
19 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
20 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
22 const transcodeOptions = {
23 inputPath: videoInputPath,
24 outputPath: videoTranscodedPath,
25 resolution: inputVideoFile.resolution
28 // Could be very long!
29 await transcode(transcodeOptions)
32 await remove(videoInputPath)
34 // Important to do this before getVideoFilename() to take in account the new file extension
35 inputVideoFile.set('extname', newExtname)
37 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
38 await move(videoTranscodedPath, videoOutputPath)
39 const stats = await stat(videoOutputPath)
40 const fps = await getVideoFileFPS(videoOutputPath)
42 inputVideoFile.set('size', stats.size)
43 inputVideoFile.set('fps', fps)
45 await video.createTorrentAndSetInfoHash(inputVideoFile)
46 await inputVideoFile.save()
48 // Auto destruction...
49 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
55 async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
56 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
57 const extname = '.mp4'
59 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
60 const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
62 const newVideoFile = new VideoFileModel({
68 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
70 const transcodeOptions = {
71 inputPath: videoInputPath,
72 outputPath: videoOutputPath,
74 isPortraitMode: isPortrait
77 await transcode(transcodeOptions)
79 const stats = await stat(videoOutputPath)
80 const fps = await getVideoFileFPS(videoOutputPath)
82 newVideoFile.set('size', stats.size)
83 newVideoFile.set('fps', fps)
85 await video.createTorrentAndSetInfoHash(newVideoFile)
87 await newVideoFile.save()
89 video.VideoFiles.push(newVideoFile)
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))
96 const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile()))
97 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
99 const transcodeOptions = {
100 inputPath: videoInputPath,
106 videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
110 await transcode(transcodeOptions)
112 await updateMasterHLSPlaylist(video)
113 await updateSha256Segments(video)
115 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
117 await VideoStreamingPlaylistModel.upsert({
120 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
121 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
122 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
124 type: VideoStreamingPlaylistType.HLS
131 transcodeOriginalVideofile