]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/ffmpeg/ffprobe-utils.ts
Add ability to delete a specific video file
[github/Chocobozzz/PeerTube.git] / server / helpers / ffmpeg / ffprobe-utils.ts
CommitLineData
06aad801 1import { FfprobeData } from 'fluent-ffmpeg'
679c12e6 2import { getMaxBitrate } from '@shared/core-utils'
06aad801 3import {
1bb4c9ab 4 buildFileMetadata,
06aad801 5 ffprobePromise,
06aad801 6 getAudioStream,
7 getMaxAudioBitrate,
c729caf6 8 getVideoStream,
1bb4c9ab 9 getVideoStreamBitrate,
c729caf6 10 getVideoStreamDimensionsInfo,
1bb4c9ab
C
11 getVideoStreamDuration,
12 getVideoStreamFPS,
c729caf6 13 hasAudioStream
06aad801 14} from '@shared/extra-utils/ffprobe'
c729caf6
C
15import { VideoResolution, VideoTranscodingFPS } from '@shared/models'
16import { CONFIG } from '../../initializers/config'
17import { VIDEO_TRANSCODING_FPS } from '../../initializers/constants'
18import { logger } from '../logger'
daf6e480 19
6b67897e
C
20/**
21 *
22 * Helpers to run ffprobe and extract data from the JSON output
23 *
24 */
25
c729caf6
C
26// ---------------------------------------------------------------------------
27// Codecs
28// ---------------------------------------------------------------------------
daf6e480 29
c729caf6
C
30async function getVideoStreamCodec (path: string) {
31 const videoStream = await getVideoStream(path)
daf6e480
C
32 if (!videoStream) return ''
33
34 const videoCodec = videoStream.codec_tag_string
35
78995146 36 if (videoCodec === 'vp09') return 'vp09.00.50.08'
08370f62 37 if (videoCodec === 'hev1') return 'hev1.1.6.L93.B0'
78995146 38
daf6e480 39 const baseProfileMatrix = {
78995146
C
40 avc1: {
41 High: '6400',
42 Main: '4D40',
43 Baseline: '42E0'
44 },
45 av01: {
46 High: '1',
47 Main: '0',
48 Professional: '2'
49 }
daf6e480
C
50 }
51
78995146 52 let baseProfile = baseProfileMatrix[videoCodec][videoStream.profile]
daf6e480
C
53 if (!baseProfile) {
54 logger.warn('Cannot get video profile codec of %s.', path, { videoStream })
78995146
C
55 baseProfile = baseProfileMatrix[videoCodec]['High'] // Fallback
56 }
57
58 if (videoCodec === 'av01') {
59 const level = videoStream.level
60
61 // Guess the tier indicator and bit depth
62 return `${videoCodec}.${baseProfile}.${level}M.08`
daf6e480
C
63 }
64
78995146 65 // Default, h264 codec
daf6e480
C
66 let level = videoStream.level.toString(16)
67 if (level.length === 1) level = `0${level}`
68
69 return `${videoCodec}.${baseProfile}${level}`
70}
71
41fb13c3 72async function getAudioStreamCodec (path: string, existingProbe?: FfprobeData) {
daf6e480
C
73 const { audioStream } = await getAudioStream(path, existingProbe)
74
75 if (!audioStream) return ''
76
78995146
C
77 const audioCodecName = audioStream.codec_name
78
79 if (audioCodecName === 'opus') return 'opus'
80 if (audioCodecName === 'vorbis') return 'vorbis'
81 if (audioCodecName === 'aac') return 'mp4a.40.2'
3d401c1b 82 if (audioCodecName === 'mp3') return 'mp4a.40.34'
daf6e480
C
83
84 logger.warn('Cannot get audio codec of %s.', path, { audioStream })
85
86 return 'mp4a.40.2' // Fallback
87}
88
c729caf6
C
89// ---------------------------------------------------------------------------
90// Resolutions
91// ---------------------------------------------------------------------------
92
ad5db104 93function computeLowerResolutionsToTranscode (videoFileResolution: number, type: 'vod' | 'live') {
daf6e480
C
94 const configResolutions = type === 'vod'
95 ? CONFIG.TRANSCODING.RESOLUTIONS
96 : CONFIG.LIVE.TRANSCODING.RESOLUTIONS
97
98 const resolutionsEnabled: number[] = []
99
100 // Put in the order we want to proceed jobs
ad5db104 101 const resolutions: VideoResolution[] = [
daf6e480
C
102 VideoResolution.H_NOVIDEO,
103 VideoResolution.H_480P,
104 VideoResolution.H_360P,
105 VideoResolution.H_720P,
106 VideoResolution.H_240P,
8dd754c7 107 VideoResolution.H_144P,
daf6e480 108 VideoResolution.H_1080P,
b7085c71 109 VideoResolution.H_1440P,
daf6e480
C
110 VideoResolution.H_4K
111 ]
112
113 for (const resolution of resolutions) {
114 if (configResolutions[resolution + 'p'] === true && videoFileResolution > resolution) {
115 resolutionsEnabled.push(resolution)
116 }
117 }
118
119 return resolutionsEnabled
120}
121
c729caf6
C
122// ---------------------------------------------------------------------------
123// Can quick transcode
124// ---------------------------------------------------------------------------
125
daf6e480 126async function canDoQuickTranscode (path: string): Promise<boolean> {
ffd970fa
C
127 if (CONFIG.TRANSCODING.PROFILE !== 'default') return false
128
daf6e480
C
129 const probe = await ffprobePromise(path)
130
5a547f69
C
131 return await canDoQuickVideoTranscode(path, probe) &&
132 await canDoQuickAudioTranscode(path, probe)
133}
134
c729caf6
C
135async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
136 const parsedAudio = await getAudioStream(path, probe)
137
138 if (!parsedAudio.audioStream) return true
139
140 if (parsedAudio.audioStream['codec_name'] !== 'aac') return false
141
142 const audioBitrate = parsedAudio.bitrate
143 if (!audioBitrate) return false
144
145 const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate)
146 if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false
147
148 const channelLayout = parsedAudio.audioStream['channel_layout']
149 // Causes playback issues with Chrome
150 if (!channelLayout || channelLayout === 'unknown') return false
151
152 return true
153}
154
41fb13c3 155async function canDoQuickVideoTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
c729caf6
C
156 const videoStream = await getVideoStream(path, probe)
157 const fps = await getVideoStreamFPS(path, probe)
158 const bitRate = await getVideoStreamBitrate(path, probe)
159 const resolutionData = await getVideoStreamDimensionsInfo(path, probe)
daf6e480 160
33ff70ba
C
161 // If ffprobe did not manage to guess the bitrate
162 if (!bitRate) return false
163
daf6e480 164 // check video params
c729caf6 165 if (!videoStream) return false
daf6e480
C
166 if (videoStream['codec_name'] !== 'h264') return false
167 if (videoStream['pix_fmt'] !== 'yuv420p') return false
168 if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false
679c12e6 169 if (bitRate > getMaxBitrate({ ...resolutionData, fps })) return false
daf6e480 170
5a547f69
C
171 return true
172}
173
c729caf6
C
174// ---------------------------------------------------------------------------
175// Framerate
176// ---------------------------------------------------------------------------
177
679c12e6 178function getClosestFramerateStandard <K extends keyof Pick<VideoTranscodingFPS, 'HD_STANDARD' | 'STANDARD'>> (fps: number, type: K) {
daf6e480
C
179 return VIDEO_TRANSCODING_FPS[type].slice(0)
180 .sort((a, b) => fps % a - fps % b)[0]
181}
182
884d2c39
C
183function computeFPS (fpsArg: number, resolution: VideoResolution) {
184 let fps = fpsArg
185
186 if (
187 // On small/medium resolutions, limit FPS
188 resolution !== undefined &&
189 resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
190 fps > VIDEO_TRANSCODING_FPS.AVERAGE
191 ) {
192 // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value
193 fps = getClosestFramerateStandard(fps, 'STANDARD')
194 }
195
196 // Hard FPS limits
197 if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD')
f7bb2bb5
C
198
199 if (fps < VIDEO_TRANSCODING_FPS.MIN) {
200 throw new Error(`Cannot compute FPS because ${fps} is lower than our minimum value ${VIDEO_TRANSCODING_FPS.MIN}`)
201 }
884d2c39
C
202
203 return fps
204}
205
daf6e480
C
206// ---------------------------------------------------------------------------
207
208export {
c729caf6
C
209 // Re export ffprobe utils
210 getVideoStreamDimensionsInfo,
211 buildFileMetadata,
daf6e480 212 getMaxAudioBitrate,
c729caf6
C
213 getVideoStream,
214 getVideoStreamDuration,
daf6e480 215 getAudioStream,
c729caf6
C
216 hasAudioStream,
217 getVideoStreamFPS,
5a547f69 218 ffprobePromise,
c729caf6
C
219 getVideoStreamBitrate,
220
221 getVideoStreamCodec,
222 getAudioStreamCodec,
223
224 computeFPS,
daf6e480 225 getClosestFramerateStandard,
c729caf6 226
ad5db104 227 computeLowerResolutionsToTranscode,
c729caf6 228
5a547f69
C
229 canDoQuickTranscode,
230 canDoQuickVideoTranscode,
231 canDoQuickAudioTranscode
daf6e480 232}