]>
Commit | Line | Data |
---|---|---|
14d3270f | 1 | import * as ffmpeg from 'fluent-ffmpeg' |
6fdc553a | 2 | import { join } from 'path' |
3fd3ab2d | 3 | import { VideoResolution } from '../../shared/models/videos' |
a7977280 | 4 | import { CONFIG, VIDEO_TRANSCODING_FPS } from '../initializers' |
6fdc553a | 5 | import { unlinkPromise } from './core-utils' |
26670720 | 6 | import { processImage } from './image-utils' |
6fdc553a | 7 | import { logger } from './logger' |
14d3270f | 8 | |
056aa7f2 | 9 | async function getVideoFileResolution (path: string) { |
73c69591 | 10 | const videoStream = await getVideoFileStream(path) |
056aa7f2 C |
11 | |
12 | return { | |
13 | videoFileResolution: Math.min(videoStream.height, videoStream.width), | |
14 | isPortraitMode: videoStream.height > videoStream.width | |
15 | } | |
73c69591 | 16 | } |
14d3270f | 17 | |
73c69591 C |
18 | async function getVideoFileFPS (path: string) { |
19 | const videoStream = await getVideoFileStream(path) | |
20 | ||
21 | for (const key of [ 'r_frame_rate' , 'avg_frame_rate' ]) { | |
22 | const valuesText: string = videoStream[key] | |
23 | if (!valuesText) continue | |
24 | ||
25 | const [ frames, seconds ] = valuesText.split('/') | |
26 | if (!frames || !seconds) continue | |
27 | ||
28 | const result = parseInt(frames, 10) / parseInt(seconds, 10) | |
3a6f351b | 29 | if (result > 0) return Math.round(result) |
73c69591 C |
30 | } |
31 | ||
32 | return 0 | |
14d3270f C |
33 | } |
34 | ||
35 | function getDurationFromVideoFile (path: string) { | |
36 | return new Promise<number>((res, rej) => { | |
37 | ffmpeg.ffprobe(path, (err, metadata) => { | |
38 | if (err) return rej(err) | |
39 | ||
40 | return res(Math.floor(metadata.format.duration)) | |
41 | }) | |
42 | }) | |
43 | } | |
44 | ||
26670720 C |
45 | async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) { |
46 | const pendingImageName = 'pending-' + imageName | |
47 | ||
14d3270f | 48 | const options = { |
26670720 | 49 | filename: pendingImageName, |
14d3270f C |
50 | count: 1, |
51 | folder | |
52 | } | |
53 | ||
26670720 | 54 | const pendingImagePath = join(folder, pendingImageName) |
6fdc553a C |
55 | |
56 | try { | |
57 | await new Promise<string>((res, rej) => { | |
58 | ffmpeg(fromPath) | |
59 | .on('error', rej) | |
60 | .on('end', () => res(imageName)) | |
61 | .thumbnail(options) | |
62 | }) | |
63 | ||
64 | const destination = join(folder, imageName) | |
65 | await processImage({ path: pendingImagePath }, destination, size) | |
66 | } catch (err) { | |
d5b7d911 | 67 | logger.error('Cannot generate image from video %s.', fromPath, { err }) |
6fdc553a C |
68 | |
69 | try { | |
70 | await unlinkPromise(pendingImagePath) | |
71 | } catch (err) { | |
d5b7d911 | 72 | logger.debug('Cannot remove pending image path after generation error.', { err }) |
6fdc553a C |
73 | } |
74 | } | |
14d3270f C |
75 | } |
76 | ||
77 | type TranscodeOptions = { | |
78 | inputPath: string | |
79 | outputPath: string | |
80 | resolution?: VideoResolution | |
056aa7f2 | 81 | isPortraitMode?: boolean |
14d3270f C |
82 | } |
83 | ||
84 | function transcode (options: TranscodeOptions) { | |
73c69591 | 85 | return new Promise<void>(async (res, rej) => { |
14d3270f C |
86 | let command = ffmpeg(options.inputPath) |
87 | .output(options.outputPath) | |
88 | .videoCodec('libx264') | |
89 | .outputOption('-threads ' + CONFIG.TRANSCODING.THREADS) | |
90 | .outputOption('-movflags faststart') | |
602a81a2 AL |
91 | .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it |
92 | .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16 | |
a6218a0b | 93 | // .outputOption('-crf 18') |
14d3270f | 94 | |
3a6f351b | 95 | let fps = await getVideoFileFPS(options.inputPath) |
14d3270f | 96 | if (options.resolution !== undefined) { |
056aa7f2 C |
97 | // '?x720' or '720x?' for example |
98 | const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}` | |
14d3270f | 99 | command = command.size(size) |
3a6f351b C |
100 | |
101 | // On small/medium resolutions, limit FPS | |
102 | if ( | |
103 | options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && | |
104 | fps > VIDEO_TRANSCODING_FPS.AVERAGE | |
105 | ) { | |
106 | fps = VIDEO_TRANSCODING_FPS.AVERAGE | |
107 | } | |
108 | } | |
109 | ||
110 | if (fps) { | |
111 | // Hard FPS limits | |
112 | if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.MAX | |
113 | else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN | |
114 | ||
115 | command = command.withFPS(fps) | |
14d3270f C |
116 | } |
117 | ||
747b2990 C |
118 | command |
119 | .on('error', (err, stdout, stderr) => { | |
120 | logger.error('Error in transcoding job.', { stdout, stderr }) | |
121 | return rej(err) | |
122 | }) | |
123 | .on('end', res) | |
124 | .run() | |
14d3270f C |
125 | }) |
126 | } | |
127 | ||
128 | // --------------------------------------------------------------------------- | |
129 | ||
130 | export { | |
056aa7f2 | 131 | getVideoFileResolution, |
14d3270f C |
132 | getDurationFromVideoFile, |
133 | generateImageFromVideoFile, | |
73c69591 C |
134 | transcode, |
135 | getVideoFileFPS | |
136 | } | |
137 | ||
138 | // --------------------------------------------------------------------------- | |
139 | ||
140 | function getVideoFileStream (path: string) { | |
141 | return new Promise<any>((res, rej) => { | |
142 | ffmpeg.ffprobe(path, (err, metadata) => { | |
143 | if (err) return rej(err) | |
144 | ||
145 | const videoStream = metadata.streams.find(s => s.codec_type === 'video') | |
146 | if (!videoStream) throw new Error('Cannot find video stream of ' + path) | |
147 | ||
148 | return res(videoStream) | |
149 | }) | |
150 | }) | |
14d3270f | 151 | } |