+transcodeOriginalVideofile = function (this: VideoInstance, resolution: VideoResolution) {
+ 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 = (Video['sequelize'].models.VideoFile as VideoFileModel).build({
+ resolution,
+ extname,
+ size: 0,
+ videoId: this.id
+ })
+ const videoOutputPath = join(videosDirectory, this.getVideoFilename(newVideoFile))
+ const resolutionWidthSizes = {
+ 1: '240x?',
+ 2: '360x?',
+ 3: '480x?',
+ 4: '720x?',
+ 5: '1080x?'
+ }
+
+ return new Promise<void>((res, rej) => {
+ ffmpeg(videoInputPath)
+ .output(videoOutputPath)
+ .videoCodec('libx264')
+ .size(resolutionWidthSizes[resolution])
+ .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
+ .outputOption('-movflags faststart')
+ .on('error', rej)
+ .on('end', () => {
+ return statPromise(videoOutputPath)
+ .then(stats => {
+ newVideoFile.set('size', stats.size)
+
+ return undefined
+ })
+ .then(() => {
+ return this.createTorrentAndSetInfoHash(newVideoFile)
+ })
+ .then(() => {
+ return newVideoFile.save()
+ })
+ .then(() => {
+ return this.VideoFiles.push(newVideoFile)
+ })
+ .then(() => {
+ return res()
+ })
+ .catch(rej)
+ })
+ .run()
+ })
+}
+
+getOriginalFileHeight = function (this: VideoInstance) {
+ const originalFilePath = this.getVideoFilePath(this.getOriginalFile())
+
+ return new Promise<number>((res, rej) => {
+ ffmpeg.ffprobe(originalFilePath, (err, metadata) => {
+ if (err) return rej(err)
+
+ const videoStream = metadata.streams.find(s => s.codec_type === 'video')
+ return res(videoStream.height)
+ })
+ })
+}
+
+removeThumbnail = function (this: VideoInstance) {
+ const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
+ return unlinkPromise(thumbnailPath)
+}
+
+removePreview = function (this: VideoInstance) {
+ // Same name than video thumbnail
+ return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName())
+}
+
+removeFile = function (this: VideoInstance, videoFile: VideoFileInstance) {
+ const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile))
+ return unlinkPromise(filePath)
+}
+
+removeTorrent = function (this: VideoInstance, videoFile: VideoFileInstance) {
+ const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
+ return unlinkPromise(torrentPath)
+}
+