1 import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
2 import { join } from 'path'
3 import { getVideoFileFPS, transcode, canDoQuickTranscode } 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'
15 * Optimize the original video file and replace it. The resolution is not changed.
17 async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
18 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
19 const newExtname = '.mp4'
21 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
22 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
23 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
25 const doQuickTranscode = await(canDoQuickTranscode(videoInputPath))
27 const transcodeOptions = {
28 inputPath: videoInputPath,
29 outputPath: videoTranscodedPath,
30 resolution: inputVideoFile.resolution,
34 // Could be very long!
35 await transcode(transcodeOptions)
38 await remove(videoInputPath)
40 // Important to do this before getVideoFilename() to take in account the new file extension
41 inputVideoFile.set('extname', newExtname)
43 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
44 await move(videoTranscodedPath, videoOutputPath)
45 const stats = await stat(videoOutputPath)
46 const fps = await getVideoFileFPS(videoOutputPath)
48 inputVideoFile.set('size', stats.size)
49 inputVideoFile.set('fps', fps)
51 await video.createTorrentAndSetInfoHash(inputVideoFile)
52 await inputVideoFile.save()
54 // Auto destruction...
55 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
62 * Transcode the original video file to a lower resolution.
64 async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
65 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
66 const extname = '.mp4'
68 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
69 const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
71 const newVideoFile = new VideoFileModel({
77 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
79 const transcodeOptions = {
80 inputPath: videoInputPath,
81 outputPath: videoOutputPath,
83 isPortraitMode: isPortrait
86 await transcode(transcodeOptions)
88 const stats = await stat(videoOutputPath)
89 const fps = await getVideoFileFPS(videoOutputPath)
91 newVideoFile.set('size', stats.size)
92 newVideoFile.set('fps', fps)
94 await video.createTorrentAndSetInfoHash(newVideoFile)
96 await newVideoFile.save()
98 video.VideoFiles.push(newVideoFile)
101 async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
102 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
103 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
105 const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile()))
106 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
108 const transcodeOptions = {
109 inputPath: videoInputPath,
115 videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
119 await transcode(transcodeOptions)
121 await updateMasterHLSPlaylist(video)
122 await updateSha256Segments(video)
124 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
126 await VideoStreamingPlaylistModel.upsert({
129 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
130 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
131 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
133 type: VideoStreamingPlaylistType.HLS
140 transcodeOriginalVideofile