1 import { copyFile, ensureDir, move, remove, stat } from 'fs-extra'
2 import { basename, extname as extnameUtil, join } from 'path'
3 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
4 import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/types/models'
5 import { VideoResolution } from '../../shared/models/videos'
6 import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
7 import { transcode, TranscodeOptions, TranscodeOptionsType } from '../helpers/ffmpeg-utils'
8 import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../helpers/ffprobe-utils'
9 import { logger } from '../helpers/logger'
10 import { CONFIG } from '../initializers/config'
11 import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
12 import { VideoFileModel } from '../models/video/video-file'
13 import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
14 import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls'
15 import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths'
16 import { availableEncoders } from './video-transcoding-profiles'
19 * Optimize the original video file and replace it. The resolution is not changed.
21 async function optimizeOriginalVideofile (video: MVideoWithFile, inputVideoFileArg?: MVideoFile) {
22 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
23 const newExtname = '.mp4'
25 const inputVideoFile = inputVideoFileArg || video.getMaxQualityFile()
26 const videoInputPath = getVideoFilePath(video, inputVideoFile)
27 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
29 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
33 const transcodeOptions: TranscodeOptions = {
36 inputPath: videoInputPath,
37 outputPath: videoTranscodedPath,
42 resolution: inputVideoFile.resolution
45 // Could be very long!
46 await transcode(transcodeOptions)
49 await remove(videoInputPath)
51 // Important to do this before getVideoFilename() to take in account the new file extension
52 inputVideoFile.extname = newExtname
54 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
56 await onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
58 // Auto destruction...
59 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
66 * Transcode the original video file to a lower resolution.
68 async function transcodeNewResolution (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean) {
69 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
70 const extname = '.mp4'
72 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
73 const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile())
75 const newVideoFile = new VideoFileModel({
81 const videoOutputPath = getVideoFilePath(video, newVideoFile)
82 const videoTranscodedPath = join(transcodeDirectory, getVideoFilename(video, newVideoFile))
84 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
86 type: 'only-audio' as 'only-audio',
88 inputPath: videoInputPath,
89 outputPath: videoTranscodedPath,
97 type: 'video' as 'video',
98 inputPath: videoInputPath,
99 outputPath: videoTranscodedPath,
105 isPortraitMode: isPortrait
108 await transcode(transcodeOptions)
110 return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
113 async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution) {
114 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
115 const newExtname = '.mp4'
117 const inputVideoFile = video.getMinQualityFile()
119 const audioInputPath = getVideoFilePath(video, inputVideoFile)
120 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
122 // If the user updates the video preview during transcoding
123 const previewPath = video.getPreview().getPath()
124 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
125 await copyFile(previewPath, tmpPreviewPath)
127 const transcodeOptions = {
128 type: 'merge-audio' as 'merge-audio',
130 inputPath: tmpPreviewPath,
131 outputPath: videoTranscodedPath,
136 audioPath: audioInputPath,
141 await transcode(transcodeOptions)
143 await remove(audioInputPath)
144 await remove(tmpPreviewPath)
146 await remove(tmpPreviewPath)
150 // Important to do this before getVideoFilename() to take in account the new file extension
151 inputVideoFile.extname = newExtname
153 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
154 // ffmpeg generated a new video file, so update the video duration
155 // See https://trac.ffmpeg.org/ticket/5456
156 video.duration = await getDurationFromVideoFile(videoTranscodedPath)
159 return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
162 async function generateHlsPlaylist (options: {
163 video: MVideoWithFile
164 videoInputPath: string
165 resolution: VideoResolution
167 isPortraitMode: boolean
169 const { video, videoInputPath, resolution, copyCodecs, isPortraitMode } = options
171 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
172 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
174 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
175 const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
177 const transcodeOptions = {
178 type: 'hls' as 'hls',
180 inputPath: videoInputPath,
195 await transcode(transcodeOptions)
197 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
199 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
202 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
203 p2pMediaLoaderInfohashes: [],
204 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
206 type: VideoStreamingPlaylistType.HLS
207 }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
208 videoStreamingPlaylist.Video = video
210 const newVideoFile = new VideoFileModel({
212 extname: extnameUtil(videoFilename),
215 videoStreamingPlaylistId: videoStreamingPlaylist.id
218 const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
219 const stats = await stat(videoFilePath)
221 newVideoFile.size = stats.size
222 newVideoFile.fps = await getVideoFileFPS(videoFilePath)
223 newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
225 await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
227 await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
228 videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
230 videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
231 playlistUrl, videoStreamingPlaylist.VideoFiles
233 await videoStreamingPlaylist.save()
235 video.setHLSPlaylist(videoStreamingPlaylist)
237 await updateMasterHLSPlaylist(video)
238 await updateSha256VODSegments(video)
243 // ---------------------------------------------------------------------------
247 optimizeOriginalVideofile,
248 transcodeNewResolution,
252 // ---------------------------------------------------------------------------
254 async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) {
255 const stats = await stat(transcodingPath)
256 const fps = await getVideoFileFPS(transcodingPath)
257 const metadata = await getMetadataFromFile(transcodingPath)
259 await move(transcodingPath, outputPath, { overwrite: true })
261 videoFile.size = stats.size
263 videoFile.metadata = metadata
265 await createTorrentAndSetInfoHash(video, videoFile)
267 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
268 video.VideoFiles = await video.$get('VideoFiles')