1 import { Job } from 'bullmq'
2 import { copyFile, move, remove, stat } from 'fs-extra'
3 import { basename, join } from 'path'
4 import { computeOutputFPS } from '@server/helpers/ffmpeg'
5 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
6 import { VideoModel } from '@server/models/video/video'
7 import { MVideoFile, MVideoFullLight } from '@server/types/models'
8 import { ffprobePromise, getVideoStreamDuration, getVideoStreamFPS, TranscodeVODOptionsType } from '@shared/ffmpeg'
9 import { VideoResolution, VideoStorage } from '@shared/models'
10 import { CONFIG } from '../../initializers/config'
11 import { VideoFileModel } from '../../models/video/video-file'
12 import { generateWebTorrentVideoFilename } from '../paths'
13 import { buildFileMetadata } from '../video-file'
14 import { VideoPathManager } from '../video-path-manager'
15 import { buildFFmpegVOD } from './shared'
16 import { buildOriginalFileResolution } from './transcoding-resolutions'
18 // Optimize the original video file and replace it. The resolution is not changed.
19 export async function optimizeOriginalVideofile (options: {
20 video: MVideoFullLight
21 inputVideoFile: MVideoFile
22 quickTranscode: boolean
25 const { video, inputVideoFile, quickTranscode, job } = options
27 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
28 const newExtname = '.mp4'
30 // Will be released by our transcodeVOD function once ffmpeg is ran
31 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
35 await inputVideoFile.reload()
37 const fileWithVideoOrPlaylist = inputVideoFile.withVideoOrPlaylist(video)
39 const result = await VideoPathManager.Instance.makeAvailableVideoFile(fileWithVideoOrPlaylist, async videoInputPath => {
40 const videoOutputPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
42 const transcodeType: TranscodeVODOptionsType = quickTranscode
46 const resolution = buildOriginalFileResolution(inputVideoFile.resolution)
47 const fps = computeOutputFPS({ inputFPS: inputVideoFile.fps, resolution })
49 // Could be very long!
50 await buildFFmpegVOD(job).transcode({
53 inputPath: videoInputPath,
54 outputPath: videoOutputPath,
56 inputFileMutexReleaser,
62 // Important to do this before getVideoFilename() to take in account the new filename
63 inputVideoFile.resolution = resolution
64 inputVideoFile.extname = newExtname
65 inputVideoFile.filename = generateWebTorrentVideoFilename(resolution, newExtname)
66 inputVideoFile.storage = VideoStorage.FILE_SYSTEM
68 const { videoFile } = await onWebTorrentVideoFileTranscoding({
70 videoFile: inputVideoFile,
74 await remove(videoInputPath)
76 return { transcodeType, videoFile }
81 inputFileMutexReleaser()
85 // Transcode the original video file to a lower resolution compatible with WebTorrent
86 export async function transcodeNewWebTorrentResolution (options: {
87 video: MVideoFullLight
88 resolution: VideoResolution
92 const { video: videoArg, resolution, fps, job } = options
94 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
95 const newExtname = '.mp4'
97 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(videoArg.uuid)
100 const video = await VideoModel.loadFull(videoArg.uuid)
101 const file = video.getMaxQualityFile().withVideoOrPlaylist(video)
103 const result = await VideoPathManager.Instance.makeAvailableVideoFile(file, async videoInputPath => {
104 const newVideoFile = new VideoFileModel({
107 filename: generateWebTorrentVideoFilename(resolution, newExtname),
112 const videoOutputPath = join(transcodeDirectory, newVideoFile.filename)
114 const transcodeOptions = {
115 type: 'video' as 'video',
117 inputPath: videoInputPath,
118 outputPath: videoOutputPath,
120 inputFileMutexReleaser,
126 await buildFFmpegVOD(job).transcode(transcodeOptions)
128 return onWebTorrentVideoFileTranscoding({ video, videoFile: newVideoFile, videoOutputPath })
133 inputFileMutexReleaser()
137 // Merge an image with an audio file to create a video
138 export async function mergeAudioVideofile (options: {
139 video: MVideoFullLight
140 resolution: VideoResolution
144 const { video: videoArg, resolution, fps, job } = options
146 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
147 const newExtname = '.mp4'
149 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(videoArg.uuid)
152 const video = await VideoModel.loadFull(videoArg.uuid)
153 const inputVideoFile = video.getMinQualityFile()
155 const fileWithVideoOrPlaylist = inputVideoFile.withVideoOrPlaylist(video)
157 const result = await VideoPathManager.Instance.makeAvailableVideoFile(fileWithVideoOrPlaylist, async audioInputPath => {
158 const videoOutputPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
160 // If the user updates the video preview during transcoding
161 const previewPath = video.getPreview().getPath()
162 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
163 await copyFile(previewPath, tmpPreviewPath)
165 const transcodeOptions = {
166 type: 'merge-audio' as 'merge-audio',
168 inputPath: tmpPreviewPath,
169 outputPath: videoOutputPath,
171 inputFileMutexReleaser,
173 audioPath: audioInputPath,
179 await buildFFmpegVOD(job).transcode(transcodeOptions)
181 await remove(audioInputPath)
182 await remove(tmpPreviewPath)
184 await remove(tmpPreviewPath)
188 // Important to do this before getVideoFilename() to take in account the new file extension
189 inputVideoFile.extname = newExtname
190 inputVideoFile.resolution = resolution
191 inputVideoFile.filename = generateWebTorrentVideoFilename(inputVideoFile.resolution, newExtname)
193 // ffmpeg generated a new video file, so update the video duration
194 // See https://trac.ffmpeg.org/ticket/5456
195 video.duration = await getVideoStreamDuration(videoOutputPath)
198 return onWebTorrentVideoFileTranscoding({
200 videoFile: inputVideoFile,
207 inputFileMutexReleaser()
211 export async function onWebTorrentVideoFileTranscoding (options: {
212 video: MVideoFullLight
213 videoFile: MVideoFile
214 videoOutputPath: string
216 const { video, videoFile, videoOutputPath } = options
218 const mutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
223 const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, videoFile)
225 const stats = await stat(videoOutputPath)
227 const probe = await ffprobePromise(videoOutputPath)
228 const fps = await getVideoStreamFPS(videoOutputPath, probe)
229 const metadata = await buildFileMetadata(videoOutputPath, probe)
231 await move(videoOutputPath, outputPath, { overwrite: true })
233 videoFile.size = stats.size
235 videoFile.metadata = metadata
237 await createTorrentAndSetInfoHash(video, videoFile)
239 const oldFile = await VideoFileModel.loadWebTorrentFile({ videoId: video.id, fps: videoFile.fps, resolution: videoFile.resolution })
240 if (oldFile) await video.removeWebTorrentFile(oldFile)
242 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
243 video.VideoFiles = await video.$get('VideoFiles')
245 return { video, videoFile }