1 import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
2 import { basename, extname as extnameUtil, join } from 'path'
5 getDurationFromVideoFile,
11 } from '../helpers/ffmpeg-utils'
12 import { copyFile, ensureDir, move, remove, stat } from 'fs-extra'
13 import { logger } from '../helpers/logger'
14 import { VideoResolution } from '../../shared/models/videos'
15 import { VideoFileModel } from '../models/video/video-file'
16 import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls'
17 import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
18 import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
19 import { CONFIG } from '../initializers/config'
20 import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/types/models'
21 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
22 import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths'
23 import { spawn } from 'child_process'
26 * Optimize the original video file and replace it. The resolution is not changed.
28 async function optimizeOriginalVideofile (video: MVideoWithFile, inputVideoFileArg?: MVideoFile) {
29 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
30 const newExtname = '.mp4'
32 const inputVideoFile = inputVideoFileArg || video.getMaxQualityFile()
33 const videoInputPath = getVideoFilePath(video, inputVideoFile)
34 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
36 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
40 const transcodeOptions: TranscodeOptions = {
42 inputPath: videoInputPath,
43 outputPath: videoTranscodedPath,
44 resolution: inputVideoFile.resolution
47 // Could be very long!
48 await transcode(transcodeOptions)
51 await remove(videoInputPath)
53 // Important to do this before getVideoFilename() to take in account the new file extension
54 inputVideoFile.extname = newExtname
56 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
58 await onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
60 // Auto destruction...
61 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
68 * Transcode the original video file to a lower resolution.
70 async function transcodeNewResolution (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean) {
71 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
72 const extname = '.mp4'
74 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
75 const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile())
77 const newVideoFile = new VideoFileModel({
83 const videoOutputPath = getVideoFilePath(video, newVideoFile)
84 const videoTranscodedPath = join(transcodeDirectory, getVideoFilename(video, newVideoFile))
86 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
88 type: 'only-audio' as 'only-audio',
89 inputPath: videoInputPath,
90 outputPath: videoTranscodedPath,
94 type: 'video' as 'video',
95 inputPath: videoInputPath,
96 outputPath: videoTranscodedPath,
98 isPortraitMode: isPortrait
101 await transcode(transcodeOptions)
103 return onVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
106 async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution) {
107 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
108 const newExtname = '.mp4'
110 const inputVideoFile = video.getMinQualityFile()
112 const audioInputPath = getVideoFilePath(video, inputVideoFile)
113 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
115 // If the user updates the video preview during transcoding
116 const previewPath = video.getPreview().getPath()
117 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
118 await copyFile(previewPath, tmpPreviewPath)
120 const transcodeOptions = {
121 type: 'merge-audio' as 'merge-audio',
122 inputPath: tmpPreviewPath,
123 outputPath: videoTranscodedPath,
124 audioPath: audioInputPath,
129 await transcode(transcodeOptions)
131 await remove(audioInputPath)
132 await remove(tmpPreviewPath)
134 await remove(tmpPreviewPath)
138 // Important to do this before getVideoFilename() to take in account the new file extension
139 inputVideoFile.extname = newExtname
141 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
142 // ffmpeg generated a new video file, so update the video duration
143 // See https://trac.ffmpeg.org/ticket/5456
144 video.duration = await getDurationFromVideoFile(videoTranscodedPath)
147 return onVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
150 async function generateHlsPlaylist (options: {
151 video: MVideoWithFile
152 videoInputPath: string
153 resolution: VideoResolution
155 isPortraitMode: boolean
157 const { video, videoInputPath, resolution, copyCodecs, isPortraitMode } = options
159 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
160 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
162 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
163 const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
165 const transcodeOptions = {
166 type: 'hls' as 'hls',
167 inputPath: videoInputPath,
178 logger.debug('Will run transcode.', { transcodeOptions })
180 await transcode(transcodeOptions)
182 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
184 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
187 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
188 p2pMediaLoaderInfohashes: [],
189 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
191 type: VideoStreamingPlaylistType.HLS
192 }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
193 videoStreamingPlaylist.Video = video
195 const newVideoFile = new VideoFileModel({
197 extname: extnameUtil(videoFilename),
200 videoStreamingPlaylistId: videoStreamingPlaylist.id
203 const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
204 const stats = await stat(videoFilePath)
206 newVideoFile.size = stats.size
207 newVideoFile.fps = await getVideoFileFPS(videoFilePath)
208 newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
210 await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
212 await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
213 videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
215 videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
216 playlistUrl, videoStreamingPlaylist.VideoFiles
218 await videoStreamingPlaylist.save()
220 video.setHLSPlaylist(videoStreamingPlaylist)
222 await updateMasterHLSPlaylist(video)
223 await updateSha256VODSegments(video)
228 // ---------------------------------------------------------------------------
232 optimizeOriginalVideofile,
233 transcodeNewResolution,
237 // ---------------------------------------------------------------------------
239 async function onVideoFileTranscoding (video: MVideoWithFile, videoFile: MVideoFile, transcodingPath: string, outputPath: string) {
240 const stats = await stat(transcodingPath)
241 const fps = await getVideoFileFPS(transcodingPath)
242 const metadata = await getMetadataFromFile(transcodingPath)
244 await move(transcodingPath, outputPath, { overwrite: true })
246 videoFile.size = stats.size
248 videoFile.metadata = metadata
250 await createTorrentAndSetInfoHash(video, videoFile)
252 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
253 video.VideoFiles = await video.$get('VideoFiles')