import { checkFFmpegEncoders } from '../initializers/checker-before-init'
import { readFile, remove, writeFile } from 'fs-extra'
import { CONFIG } from '../initializers/config'
+import { VideoFileMetadata } from '@shared/models/videos/video-file-metadata'
/**
* A toolbox to play with audio
return 0
}
-async function getVideoFileBitrate (path: string) {
- return new Promise<number>((res, rej) => {
+async function getMetadataFromFile <T> (path: string, cb = metadata => metadata) {
+ return new Promise<T>((res, rej) => {
ffmpeg.ffprobe(path, (err, metadata) => {
if (err) return rej(err)
- return res(metadata.format.bit_rate)
+ return res(cb(new VideoFileMetadata(metadata)))
})
})
}
+async function getVideoFileBitrate (path: string) {
+ return getMetadataFromFile<number>(path, metadata => metadata.format.bit_rate)
+}
+
function getDurationFromVideoFile (path: string) {
- return new Promise<number>((res, rej) => {
- ffmpeg.ffprobe(path, (err, metadata) => {
- if (err) return rej(err)
+ return getMetadataFromFile<number>(path, metadata => Math.floor(metadata.format.duration))
+}
- return res(Math.floor(metadata.format.duration))
- })
- })
+function getVideoStreamFromFile (path: string) {
+ return getMetadataFromFile<any>(path, metadata => metadata.streams.find(s => s.codec_type === 'video') || null)
}
async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
getAudioStreamCodec,
getVideoStreamSize,
getVideoFileResolution,
+ getMetadataFromFile,
getDurationFromVideoFile,
generateImageFromVideoFile,
TranscodeOptions,
await writeFile(options.outputPath, newContent)
}
-function getVideoStreamFromFile (path: string) {
- return new Promise<any>((res, rej) => {
- ffmpeg.ffprobe(path, (err, metadata) => {
- if (err) return rej(err)
-
- const videoStream = metadata.streams.find(s => s.codec_type === 'video')
- return res(videoStream || null)
- })
- })
-}
-
/**
* A slightly customised version of the 'veryfast' x264 preset
*