1 import { CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY } from '../initializers'
2 import { extname, join } from 'path'
3 import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils'
4 import { copy, 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'
13 async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
14 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
15 const newExtname = '.mp4'
17 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
18 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
19 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
21 const transcodeOptions = {
22 inputPath: videoInputPath,
23 outputPath: videoTranscodedPath,
24 resolution: inputVideoFile.resolution
27 // Could be very long!
28 await transcode(transcodeOptions)
31 await remove(videoInputPath)
33 // Important to do this before getVideoFilename() to take in account the new file extension
34 inputVideoFile.set('extname', newExtname)
36 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
37 await move(videoTranscodedPath, videoOutputPath)
38 const stats = await stat(videoOutputPath)
39 const fps = await getVideoFileFPS(videoOutputPath)
41 inputVideoFile.set('size', stats.size)
42 inputVideoFile.set('fps', fps)
44 await video.createTorrentAndSetInfoHash(inputVideoFile)
45 await inputVideoFile.save()
47 // Auto destruction...
48 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
54 async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
55 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
56 const extname = '.mp4'
58 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
59 const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
61 const newVideoFile = new VideoFileModel({
67 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
69 const transcodeOptions = {
70 inputPath: videoInputPath,
71 outputPath: videoOutputPath,
73 isPortraitMode: isPortrait
76 await transcode(transcodeOptions)
78 const stats = await stat(videoOutputPath)
79 const fps = await getVideoFileFPS(videoOutputPath)
81 newVideoFile.set('size', stats.size)
82 newVideoFile.set('fps', fps)
84 await video.createTorrentAndSetInfoHash(newVideoFile)
86 await newVideoFile.save()
88 video.VideoFiles.push(newVideoFile)
91 async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
92 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
93 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
95 const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile()))
96 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
98 const transcodeOptions = {
99 inputPath: videoInputPath,
105 videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
109 await transcode(transcodeOptions)
111 await updateMasterHLSPlaylist(video)
112 await updateSha256Segments(video)
114 const playlistUrl = CONFIG.WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
116 await VideoStreamingPlaylistModel.upsert({
119 segmentsSha256Url: CONFIG.WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
120 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
122 type: VideoStreamingPlaylistType.HLS
126 async function importVideoFile (video: VideoModel, inputFilePath: string) {
127 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
128 const { size } = await stat(inputFilePath)
129 const fps = await getVideoFileFPS(inputFilePath)
131 let updatedVideoFile = new VideoFileModel({
132 resolution: videoFileResolution,
133 extname: extname(inputFilePath),
139 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
141 if (currentVideoFile) {
142 // Remove old file and old torrent
143 await video.removeFile(currentVideoFile)
144 await video.removeTorrent(currentVideoFile)
145 // Remove the old video file from the array
146 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
148 // Update the database
149 currentVideoFile.set('extname', updatedVideoFile.extname)
150 currentVideoFile.set('size', updatedVideoFile.size)
151 currentVideoFile.set('fps', updatedVideoFile.fps)
153 updatedVideoFile = currentVideoFile
156 const outputPath = video.getVideoFilePath(updatedVideoFile)
157 await copy(inputFilePath, outputPath)
159 await video.createTorrentAndSetInfoHash(updatedVideoFile)
161 await updatedVideoFile.save()
163 video.VideoFiles.push(updatedVideoFile)
169 transcodeOriginalVideofile,