]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/ffmpeg-utils.ts
Change transcoding default conf options
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg-utils.ts
CommitLineData
14d3270f 1import * as ffmpeg from 'fluent-ffmpeg'
3fd3ab2d 2import { VideoResolution } from '../../shared/models/videos'
8d468a16 3import { CONFIG } from '../initializers'
14d3270f
C
4
5function getVideoFileHeight (path: string) {
6 return new Promise<number>((res, rej) => {
7 ffmpeg.ffprobe(path, (err, metadata) => {
8 if (err) return rej(err)
9
10 const videoStream = metadata.streams.find(s => s.codec_type === 'video')
11 return res(videoStream.height)
12 })
13 })
14}
15
16function getDurationFromVideoFile (path: string) {
17 return new Promise<number>((res, rej) => {
18 ffmpeg.ffprobe(path, (err, metadata) => {
19 if (err) return rej(err)
20
21 return res(Math.floor(metadata.format.duration))
22 })
23 })
24}
25
164174a6 26function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: string) {
14d3270f
C
27 const options = {
28 filename: imageName,
29 count: 1,
30 folder
31 }
32
33 if (size !== undefined) {
34 options['size'] = size
35 }
36
37 return new Promise<string>((res, rej) => {
38 ffmpeg(fromPath)
39 .on('error', rej)
40 .on('end', () => res(imageName))
41 .thumbnail(options)
42 })
43}
44
45type TranscodeOptions = {
46 inputPath: string
47 outputPath: string
48 resolution?: VideoResolution
49}
50
51function transcode (options: TranscodeOptions) {
52 return new Promise<void>((res, rej) => {
53 let command = ffmpeg(options.inputPath)
54 .output(options.outputPath)
55 .videoCodec('libx264')
56 .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
57 .outputOption('-movflags faststart')
a6218a0b 58 // .outputOption('-crf 18')
14d3270f
C
59
60 if (options.resolution !== undefined) {
a6218a0b 61 const size = `?x${options.resolution}` // '?x720' for example
14d3270f
C
62 command = command.size(size)
63 }
64
65 command.on('error', rej)
66 .on('end', res)
67 .run()
68 })
69}
70
71// ---------------------------------------------------------------------------
72
73export {
74 getVideoFileHeight,
75 getDurationFromVideoFile,
76 generateImageFromVideoFile,
77 transcode
78}