]>
Commit | Line | Data |
---|---|---|
06aad801 | 1 | import { ffprobe, FfprobeData } from 'fluent-ffmpeg' |
44e702de | 2 | import { VideoFileMetadata, VideoResolution } from '@shared/models/videos' |
06aad801 | 3 | |
4 | /** | |
5 | * | |
6 | * Helpers to run ffprobe and extract data from the JSON output | |
7 | * | |
8 | */ | |
9 | ||
10 | function ffprobePromise (path: string) { | |
11 | return new Promise<FfprobeData>((res, rej) => { | |
12 | ffprobe(path, (err, data) => { | |
13 | if (err) return rej(err) | |
14 | ||
15 | return res(data) | |
16 | }) | |
17 | }) | |
18 | } | |
19 | ||
c729caf6 C |
20 | // --------------------------------------------------------------------------- |
21 | // Audio | |
22 | // --------------------------------------------------------------------------- | |
23 | ||
f686f5ed C |
24 | const imageCodecs = new Set([ |
25 | 'ansi', 'apng', 'bintext', 'bmp', 'brender_pix', 'dpx', 'exr', 'fits', 'gem', 'gif', 'jpeg2000', 'jpgls', 'mjpeg', 'mjpegb', 'msp2', | |
26 | 'pam', 'pbm', 'pcx', 'pfm', 'pgm', 'pgmyuv', 'pgx', 'photocd', 'pictor', 'png', 'ppm', 'psd', 'sgi', 'sunrast', 'svg', 'targa', 'tiff', | |
27 | 'txd', 'webp', 'xbin', 'xbm', 'xface', 'xpm', 'xwd' | |
28 | ]) | |
29 | ||
482b2623 | 30 | async function isAudioFile (path: string, existingProbe?: FfprobeData) { |
c729caf6 | 31 | const videoStream = await getVideoStream(path, existingProbe) |
f686f5ed C |
32 | if (!videoStream) return true |
33 | ||
34 | if (imageCodecs.has(videoStream.codec_name)) return true | |
482b2623 | 35 | |
f686f5ed | 36 | return false |
482b2623 C |
37 | } |
38 | ||
c729caf6 C |
39 | async function hasAudioStream (path: string, existingProbe?: FfprobeData) { |
40 | const { audioStream } = await getAudioStream(path, existingProbe) | |
41 | ||
42 | return !!audioStream | |
43 | } | |
44 | ||
06aad801 | 45 | async function getAudioStream (videoPath: string, existingProbe?: FfprobeData) { |
46 | // without position, ffprobe considers the last input only | |
47 | // we make it consider the first input only | |
48 | // if you pass a file path to pos, then ffprobe acts on that file directly | |
49 | const data = existingProbe || await ffprobePromise(videoPath) | |
50 | ||
51 | if (Array.isArray(data.streams)) { | |
52 | const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio') | |
53 | ||
54 | if (audioStream) { | |
55 | return { | |
56 | absolutePath: data.format.filename, | |
57 | audioStream, | |
58 | bitrate: parseInt(audioStream['bit_rate'] + '', 10) | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | return { absolutePath: data.format.filename } | |
64 | } | |
65 | ||
66 | function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) { | |
67 | const maxKBitrate = 384 | |
68 | const kToBits = (kbits: number) => kbits * 1000 | |
69 | ||
70 | // If we did not manage to get the bitrate, use an average value | |
71 | if (!bitrate) return 256 | |
72 | ||
73 | if (type === 'aac') { | |
74 | switch (true) { | |
75 | case bitrate > kToBits(maxKBitrate): | |
76 | return maxKBitrate | |
77 | ||
78 | default: | |
79 | return -1 // we interpret it as a signal to copy the audio stream as is | |
80 | } | |
81 | } | |
82 | ||
83 | /* | |
84 | a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac. | |
85 | That's why, when using aac, we can go to lower kbit/sec. The equivalences | |
86 | made here are not made to be accurate, especially with good mp3 encoders. | |
87 | */ | |
88 | switch (true) { | |
89 | case bitrate <= kToBits(192): | |
90 | return 128 | |
91 | ||
92 | case bitrate <= kToBits(384): | |
93 | return 256 | |
94 | ||
95 | default: | |
96 | return maxKBitrate | |
97 | } | |
98 | } | |
99 | ||
c729caf6 C |
100 | // --------------------------------------------------------------------------- |
101 | // Video | |
102 | // --------------------------------------------------------------------------- | |
06aad801 | 103 | |
c729caf6 C |
104 | async function getVideoStreamDimensionsInfo (path: string, existingProbe?: FfprobeData) { |
105 | const videoStream = await getVideoStream(path, existingProbe) | |
44e702de C |
106 | if (!videoStream) { |
107 | return { | |
108 | width: 0, | |
109 | height: 0, | |
110 | ratio: 0, | |
111 | resolution: VideoResolution.H_NOVIDEO, | |
112 | isPortraitMode: false | |
113 | } | |
114 | } | |
06aad801 | 115 | |
116 | return { | |
c729caf6 C |
117 | width: videoStream.width, |
118 | height: videoStream.height, | |
119 | ratio: Math.max(videoStream.height, videoStream.width) / Math.min(videoStream.height, videoStream.width), | |
120 | resolution: Math.min(videoStream.height, videoStream.width), | |
121 | isPortraitMode: videoStream.height > videoStream.width | |
06aad801 | 122 | } |
123 | } | |
124 | ||
c729caf6 C |
125 | async function getVideoStreamFPS (path: string, existingProbe?: FfprobeData) { |
126 | const videoStream = await getVideoStream(path, existingProbe) | |
127 | if (!videoStream) return 0 | |
06aad801 | 128 | |
129 | for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) { | |
130 | const valuesText: string = videoStream[key] | |
131 | if (!valuesText) continue | |
132 | ||
133 | const [ frames, seconds ] = valuesText.split('/') | |
134 | if (!frames || !seconds) continue | |
135 | ||
136 | const result = parseInt(frames, 10) / parseInt(seconds, 10) | |
137 | if (result > 0) return Math.round(result) | |
138 | } | |
139 | ||
140 | return 0 | |
141 | } | |
142 | ||
c729caf6 | 143 | async function buildFileMetadata (path: string, existingProbe?: FfprobeData) { |
06aad801 | 144 | const metadata = existingProbe || await ffprobePromise(path) |
145 | ||
146 | return new VideoFileMetadata(metadata) | |
147 | } | |
148 | ||
c729caf6 C |
149 | async function getVideoStreamBitrate (path: string, existingProbe?: FfprobeData): Promise<number> { |
150 | const metadata = await buildFileMetadata(path, existingProbe) | |
06aad801 | 151 | |
152 | let bitrate = metadata.format.bit_rate as number | |
153 | if (bitrate && !isNaN(bitrate)) return bitrate | |
154 | ||
c729caf6 | 155 | const videoStream = await getVideoStream(path, existingProbe) |
06aad801 | 156 | if (!videoStream) return undefined |
157 | ||
158 | bitrate = videoStream?.bit_rate | |
159 | if (bitrate && !isNaN(bitrate)) return bitrate | |
160 | ||
161 | return undefined | |
162 | } | |
163 | ||
c729caf6 C |
164 | async function getVideoStreamDuration (path: string, existingProbe?: FfprobeData) { |
165 | const metadata = await buildFileMetadata(path, existingProbe) | |
06aad801 | 166 | |
167 | return Math.round(metadata.format.duration) | |
168 | } | |
169 | ||
c729caf6 C |
170 | async function getVideoStream (path: string, existingProbe?: FfprobeData) { |
171 | const metadata = await buildFileMetadata(path, existingProbe) | |
06aad801 | 172 | |
c729caf6 | 173 | return metadata.streams.find(s => s.codec_type === 'video') |
06aad801 | 174 | } |
175 | ||
176 | // --------------------------------------------------------------------------- | |
177 | ||
178 | export { | |
c729caf6 C |
179 | getVideoStreamDimensionsInfo, |
180 | buildFileMetadata, | |
06aad801 | 181 | getMaxAudioBitrate, |
c729caf6 C |
182 | getVideoStream, |
183 | getVideoStreamDuration, | |
06aad801 | 184 | getAudioStream, |
c729caf6 | 185 | getVideoStreamFPS, |
482b2623 | 186 | isAudioFile, |
06aad801 | 187 | ffprobePromise, |
c729caf6 C |
188 | getVideoStreamBitrate, |
189 | hasAudioStream | |
06aad801 | 190 | } |