- async optimizeOriginalVideofile () {
- const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
- const newExtname = '.mp4'
- const inputVideoFile = this.getOriginalFile()
- const videoInputPath = join(videosDirectory, this.getVideoFilename(inputVideoFile))
- const videoOutputPath = join(videosDirectory, this.id + '-transcoded' + newExtname)
-
- const transcodeOptions = {
- inputPath: videoInputPath,
- outputPath: videoOutputPath
- }
-
- // Could be very long!
- await transcode(transcodeOptions)
-
- try {
- await unlinkPromise(videoInputPath)
-
- // Important to do this before getVideoFilename() to take in account the new file extension
- inputVideoFile.set('extname', newExtname)
-
- await renamePromise(videoOutputPath, this.getVideoFilePath(inputVideoFile))
- const stats = await statPromise(this.getVideoFilePath(inputVideoFile))
-
- inputVideoFile.set('size', stats.size)
-
- await this.createTorrentAndSetInfoHash(inputVideoFile)
- await inputVideoFile.save()
-
- } catch (err) {
- // Auto destruction...
- this.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
-
- throw err
- }
- }
-
- async transcodeOriginalVideofile (resolution: VideoResolution, isPortraitMode: boolean) {
- const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
- const extname = '.mp4'
-
- // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
- const videoInputPath = join(videosDirectory, this.getVideoFilename(this.getOriginalFile()))
-
- const newVideoFile = new VideoFileModel({
- resolution,
- extname,
- size: 0,
- videoId: this.id
- })
- const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
-
- const transcodeOptions = {
- inputPath: videoInputPath,
- outputPath: videoOutputPath,
- resolution,
- isPortraitMode
- }
-
- await transcode(transcodeOptions)
-
- const stats = await statPromise(videoOutputPath)
-
- newVideoFile.set('size', stats.size)
-
- await this.createTorrentAndSetInfoHash(newVideoFile)
-
- await newVideoFile.save()
-
- this.VideoFiles.push(newVideoFile)
- }
-
- async importVideoFile (inputFilePath: string) {
- let updatedVideoFile = new VideoFileModel({
- resolution: (await getVideoFileResolution(inputFilePath)).videoFileResolution,
- extname: extname(inputFilePath),
- size: (await statPromise(inputFilePath)).size,
- videoId: this.id
- })
-
- const currentVideoFile = this.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
-
- if (currentVideoFile) {
- // Remove old file and old torrent
- await this.removeFile(currentVideoFile)
- await this.removeTorrent(currentVideoFile)
- // Remove the old video file from the array
- this.VideoFiles = this.VideoFiles.filter(f => f !== currentVideoFile)
-
- // Update the database
- currentVideoFile.set('extname', updatedVideoFile.extname)
- currentVideoFile.set('size', updatedVideoFile.size)
-
- updatedVideoFile = currentVideoFile
- }
-
- const outputPath = this.getVideoFilePath(updatedVideoFile)
- await copyFilePromise(inputFilePath, outputPath)
-
- await this.createTorrentAndSetInfoHash(updatedVideoFile)
-
- await updatedVideoFile.save()
-
- this.VideoFiles.push(updatedVideoFile)
- }
-