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