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'
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 doQuickTranscode = await(canDoQuickTranscode(videoInputPath))
24 const transcodeOptions = {
25 inputPath: videoInputPath,
26 outputPath: videoTranscodedPath,
27 resolution: inputVideoFile.resolution,
31 // Could be very long!
32 await transcode(transcodeOptions)
35 await remove(videoInputPath)
37 // Important to do this before getVideoFilename() to take in account the new file extension
38 inputVideoFile.set('extname', newExtname)
40 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
41 await move(videoTranscodedPath, videoOutputPath)
42 const stats = await stat(videoOutputPath)
43 const fps = await getVideoFileFPS(videoOutputPath)
45 inputVideoFile.set('size', stats.size)
46 inputVideoFile.set('fps', fps)
48 await video.createTorrentAndSetInfoHash(inputVideoFile)
49 await inputVideoFile.save()
51 // Auto destruction...
52 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
58 async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
59 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
60 const extname = '.mp4'
62 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
63 const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
65 const newVideoFile = new VideoFileModel({
71 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
73 const transcodeOptions = {
74 inputPath: videoInputPath,
75 outputPath: videoOutputPath,
77 isPortraitMode: isPortrait
80 await transcode(transcodeOptions)
82 const stats = await stat(videoOutputPath)
83 const fps = await getVideoFileFPS(videoOutputPath)
85 newVideoFile.set('size', stats.size)
86 newVideoFile.set('fps', fps)
88 await video.createTorrentAndSetInfoHash(newVideoFile)
90 await newVideoFile.save()
92 video.VideoFiles.push(newVideoFile)
95 async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
96 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
97 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
99 const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile()))
100 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
102 const transcodeOptions = {
103 inputPath: videoInputPath,
109 videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
113 await transcode(transcodeOptions)
115 await updateMasterHLSPlaylist(video)
116 await updateSha256Segments(video)
118 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
120 await VideoStreamingPlaylistModel.upsert({
123 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
124 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
125 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
127 type: VideoStreamingPlaylistType.HLS
134 transcodeOriginalVideofile