ffmpeg-utils.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. import * as ffmpeg from 'fluent-ffmpeg'
  2. import { dirname, join } from 'path'
  3. import { getTargetBitrate, getMaxBitrate, VideoResolution } from '../../shared/models/videos'
  4. import { FFMPEG_NICE, VIDEO_TRANSCODING_FPS } from '../initializers/constants'
  5. import { processImage } from './image-utils'
  6. import { logger } from './logger'
  7. import { checkFFmpegEncoders } from '../initializers/checker-before-init'
  8. import { readFile, remove, writeFile } from 'fs-extra'
  9. import { CONFIG } from '../initializers/config'
  10. function computeResolutionsToTranscode (videoFileHeight: number) {
  11. const resolutionsEnabled: number[] = []
  12. const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
  13. // Put in the order we want to proceed jobs
  14. const resolutions = [
  15. VideoResolution.H_480P,
  16. VideoResolution.H_360P,
  17. VideoResolution.H_720P,
  18. VideoResolution.H_240P,
  19. VideoResolution.H_1080P,
  20. VideoResolution.H_4K
  21. ]
  22. for (const resolution of resolutions) {
  23. if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
  24. resolutionsEnabled.push(resolution)
  25. }
  26. }
  27. return resolutionsEnabled
  28. }
  29. async function getVideoFileSize (path: string) {
  30. const videoStream = await getVideoStreamFromFile(path)
  31. return {
  32. width: videoStream.width,
  33. height: videoStream.height
  34. }
  35. }
  36. async function getVideoFileResolution (path: string) {
  37. const size = await getVideoFileSize(path)
  38. return {
  39. videoFileResolution: Math.min(size.height, size.width),
  40. isPortraitMode: size.height > size.width
  41. }
  42. }
  43. async function getVideoFileFPS (path: string) {
  44. const videoStream = await getVideoStreamFromFile(path)
  45. for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
  46. const valuesText: string = videoStream[key]
  47. if (!valuesText) continue
  48. const [ frames, seconds ] = valuesText.split('/')
  49. if (!frames || !seconds) continue
  50. const result = parseInt(frames, 10) / parseInt(seconds, 10)
  51. if (result > 0) return Math.round(result)
  52. }
  53. return 0
  54. }
  55. async function getVideoFileBitrate (path: string) {
  56. return new Promise<number>((res, rej) => {
  57. ffmpeg.ffprobe(path, (err, metadata) => {
  58. if (err) return rej(err)
  59. return res(metadata.format.bit_rate)
  60. })
  61. })
  62. }
  63. function getDurationFromVideoFile (path: string) {
  64. return new Promise<number>((res, rej) => {
  65. ffmpeg.ffprobe(path, (err, metadata) => {
  66. if (err) return rej(err)
  67. return res(Math.floor(metadata.format.duration))
  68. })
  69. })
  70. }
  71. async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
  72. const pendingImageName = 'pending-' + imageName
  73. const options = {
  74. filename: pendingImageName,
  75. count: 1,
  76. folder
  77. }
  78. const pendingImagePath = join(folder, pendingImageName)
  79. try {
  80. await new Promise<string>((res, rej) => {
  81. ffmpeg(fromPath, { niceness: FFMPEG_NICE.THUMBNAIL })
  82. .on('error', rej)
  83. .on('end', () => res(imageName))
  84. .thumbnail(options)
  85. })
  86. const destination = join(folder, imageName)
  87. await processImage(pendingImagePath, destination, size)
  88. } catch (err) {
  89. logger.error('Cannot generate image from video %s.', fromPath, { err })
  90. try {
  91. await remove(pendingImagePath)
  92. } catch (err) {
  93. logger.debug('Cannot remove pending image path after generation error.', { err })
  94. }
  95. }
  96. }
  97. type TranscodeOptionsType = 'hls' | 'quick-transcode' | 'video' | 'merge-audio'
  98. interface BaseTranscodeOptions {
  99. type: TranscodeOptionsType
  100. inputPath: string
  101. outputPath: string
  102. resolution: VideoResolution
  103. isPortraitMode?: boolean
  104. }
  105. interface HLSTranscodeOptions extends BaseTranscodeOptions {
  106. type: 'hls'
  107. copyCodecs: boolean
  108. hlsPlaylist: {
  109. videoFilename: string
  110. }
  111. }
  112. interface QuickTranscodeOptions extends BaseTranscodeOptions {
  113. type: 'quick-transcode'
  114. }
  115. interface VideoTranscodeOptions extends BaseTranscodeOptions {
  116. type: 'video'
  117. }
  118. interface MergeAudioTranscodeOptions extends BaseTranscodeOptions {
  119. type: 'merge-audio'
  120. audioPath: string
  121. }
  122. type TranscodeOptions = HLSTranscodeOptions | VideoTranscodeOptions | MergeAudioTranscodeOptions | QuickTranscodeOptions
  123. function transcode (options: TranscodeOptions) {
  124. return new Promise<void>(async (res, rej) => {
  125. try {
  126. let command = ffmpeg(options.inputPath, { niceness: FFMPEG_NICE.TRANSCODING })
  127. .output(options.outputPath)
  128. if (options.type === 'quick-transcode') {
  129. command = await buildQuickTranscodeCommand(command)
  130. } else if (options.type === 'hls') {
  131. command = await buildHLSCommand(command, options)
  132. } else if (options.type === 'merge-audio') {
  133. command = await buildAudioMergeCommand(command, options)
  134. } else {
  135. command = await buildx264Command(command, options)
  136. }
  137. if (CONFIG.TRANSCODING.THREADS > 0) {
  138. // if we don't set any threads ffmpeg will chose automatically
  139. command = command.outputOption('-threads ' + CONFIG.TRANSCODING.THREADS)
  140. }
  141. command
  142. .on('error', (err, stdout, stderr) => {
  143. logger.error('Error in transcoding job.', { stdout, stderr })
  144. return rej(err)
  145. })
  146. .on('end', () => {
  147. return fixHLSPlaylistIfNeeded(options)
  148. .then(() => res())
  149. .catch(err => rej(err))
  150. })
  151. .run()
  152. } catch (err) {
  153. return rej(err)
  154. }
  155. })
  156. }
  157. async function canDoQuickTranscode (path: string): Promise<boolean> {
  158. // NOTE: This could be optimized by running ffprobe only once (but it runs fast anyway)
  159. const videoStream = await getVideoStreamFromFile(path)
  160. const parsedAudio = await audio.get(path)
  161. const fps = await getVideoFileFPS(path)
  162. const bitRate = await getVideoFileBitrate(path)
  163. const resolution = await getVideoFileResolution(path)
  164. // check video params
  165. if (videoStream[ 'codec_name' ] !== 'h264') return false
  166. if (videoStream[ 'pix_fmt' ] !== 'yuv420p') return false
  167. if (fps < VIDEO_TRANSCODING_FPS.MIN || fps > VIDEO_TRANSCODING_FPS.MAX) return false
  168. if (bitRate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)) return false
  169. // check audio params (if audio stream exists)
  170. if (parsedAudio.audioStream) {
  171. if (parsedAudio.audioStream[ 'codec_name' ] !== 'aac') return false
  172. const maxAudioBitrate = audio.bitrate[ 'aac' ](parsedAudio.audioStream[ 'bit_rate' ])
  173. if (maxAudioBitrate !== -1 && parsedAudio.audioStream[ 'bit_rate' ] > maxAudioBitrate) return false
  174. }
  175. return true
  176. }
  177. // ---------------------------------------------------------------------------
  178. export {
  179. getVideoFileSize,
  180. getVideoFileResolution,
  181. getDurationFromVideoFile,
  182. generateImageFromVideoFile,
  183. TranscodeOptions,
  184. TranscodeOptionsType,
  185. transcode,
  186. getVideoFileFPS,
  187. computeResolutionsToTranscode,
  188. audio,
  189. getVideoFileBitrate,
  190. canDoQuickTranscode
  191. }
  192. // ---------------------------------------------------------------------------
  193. async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) {
  194. let fps = await getVideoFileFPS(options.inputPath)
  195. // On small/medium resolutions, limit FPS
  196. if (
  197. options.resolution !== undefined &&
  198. options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
  199. fps > VIDEO_TRANSCODING_FPS.AVERAGE
  200. ) {
  201. fps = VIDEO_TRANSCODING_FPS.AVERAGE
  202. }
  203. command = await presetH264(command, options.inputPath, options.resolution, fps)
  204. if (options.resolution !== undefined) {
  205. // '?x720' or '720x?' for example
  206. const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}`
  207. command = command.size(size)
  208. }
  209. if (fps) {
  210. // Hard FPS limits
  211. if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.MAX
  212. else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN
  213. command = command.withFPS(fps)
  214. }
  215. return command
  216. }
  217. async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) {
  218. command = command.loop(undefined)
  219. command = await presetH264VeryFast(command, options.audioPath, options.resolution)
  220. command = command.input(options.audioPath)
  221. .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error
  222. .outputOption('-tune stillimage')
  223. .outputOption('-shortest')
  224. return command
  225. }
  226. async function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) {
  227. command = await presetCopy(command)
  228. command = command.outputOption('-map_metadata -1') // strip all metadata
  229. .outputOption('-movflags faststart')
  230. return command
  231. }
  232. async function buildHLSCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) {
  233. const videoPath = getHLSVideoPath(options)
  234. if (options.copyCodecs) command = await presetCopy(command)
  235. else command = await buildx264Command(command, options)
  236. command = command.outputOption('-hls_time 4')
  237. .outputOption('-hls_list_size 0')
  238. .outputOption('-hls_playlist_type vod')
  239. .outputOption('-hls_segment_filename ' + videoPath)
  240. .outputOption('-hls_segment_type fmp4')
  241. .outputOption('-f hls')
  242. .outputOption('-hls_flags single_file')
  243. return command
  244. }
  245. function getHLSVideoPath (options: HLSTranscodeOptions) {
  246. return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}`
  247. }
  248. async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) {
  249. if (options.type !== 'hls') return
  250. const fileContent = await readFile(options.outputPath)
  251. const videoFileName = options.hlsPlaylist.videoFilename
  252. const videoFilePath = getHLSVideoPath(options)
  253. // Fix wrong mapping with some ffmpeg versions
  254. const newContent = fileContent.toString()
  255. .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`)
  256. await writeFile(options.outputPath, newContent)
  257. }
  258. function getVideoStreamFromFile (path: string) {
  259. return new Promise<any>((res, rej) => {
  260. ffmpeg.ffprobe(path, (err, metadata) => {
  261. if (err) return rej(err)
  262. const videoStream = metadata.streams.find(s => s.codec_type === 'video')
  263. if (!videoStream) return rej(new Error('Cannot find video stream of ' + path))
  264. return res(videoStream)
  265. })
  266. })
  267. }
  268. /**
  269. * A slightly customised version of the 'veryfast' x264 preset
  270. *
  271. * The veryfast preset is right in the sweet spot of performance
  272. * and quality. Superfast and ultrafast will give you better
  273. * performance, but then quality is noticeably worse.
  274. */
  275. async function presetH264VeryFast (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
  276. let localCommand = await presetH264(command, input, resolution, fps)
  277. localCommand = localCommand.outputOption('-preset:v veryfast')
  278. /*
  279. MAIN reference: https://slhck.info/video/2017/03/01/rate-control.html
  280. Our target situation is closer to a livestream than a stream,
  281. since we want to reduce as much a possible the encoding burden,
  282. although not to the point of a livestream where there is a hard
  283. constraint on the frames per second to be encoded.
  284. */
  285. return localCommand
  286. }
  287. /**
  288. * A toolbox to play with audio
  289. */
  290. namespace audio {
  291. export const get = (option: string) => {
  292. // without position, ffprobe considers the last input only
  293. // we make it consider the first input only
  294. // if you pass a file path to pos, then ffprobe acts on that file directly
  295. return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => {
  296. function parseFfprobe (err: any, data: ffmpeg.FfprobeData) {
  297. if (err) return rej(err)
  298. if ('streams' in data) {
  299. const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
  300. if (audioStream) {
  301. return res({
  302. absolutePath: data.format.filename,
  303. audioStream
  304. })
  305. }
  306. }
  307. return res({ absolutePath: data.format.filename })
  308. }
  309. return ffmpeg.ffprobe(option, parseFfprobe)
  310. })
  311. }
  312. export namespace bitrate {
  313. const baseKbitrate = 384
  314. const toBits = (kbits: number) => kbits * 8000
  315. export const aac = (bitrate: number): number => {
  316. switch (true) {
  317. case bitrate > toBits(baseKbitrate):
  318. return baseKbitrate
  319. default:
  320. return -1 // we interpret it as a signal to copy the audio stream as is
  321. }
  322. }
  323. export const mp3 = (bitrate: number): number => {
  324. /*
  325. a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
  326. That's why, when using aac, we can go to lower kbit/sec. The equivalences
  327. made here are not made to be accurate, especially with good mp3 encoders.
  328. */
  329. switch (true) {
  330. case bitrate <= toBits(192):
  331. return 128
  332. case bitrate <= toBits(384):
  333. return 256
  334. default:
  335. return baseKbitrate
  336. }
  337. }
  338. }
  339. }
  340. /**
  341. * Standard profile, with variable bitrate audio and faststart.
  342. *
  343. * As for the audio, quality '5' is the highest and ensures 96-112kbps/channel
  344. * See https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_vbr
  345. */
  346. async function presetH264 (command: ffmpeg.FfmpegCommand, input: string, resolution: VideoResolution, fps?: number) {
  347. let localCommand = command
  348. .format('mp4')
  349. .videoCodec('libx264')
  350. .outputOption('-level 3.1') // 3.1 is the minimal ressource allocation for our highest supported resolution
  351. .outputOption('-b_strategy 1') // NOTE: b-strategy 1 - heuristic algorythm, 16 is optimal B-frames for it
  352. .outputOption('-bf 16') // NOTE: Why 16: https://github.com/Chocobozzz/PeerTube/pull/774. b-strategy 2 -> B-frames<16
  353. .outputOption('-pix_fmt yuv420p') // allows import of source material with incompatible pixel formats (e.g. MJPEG video)
  354. .outputOption('-map_metadata -1') // strip all metadata
  355. .outputOption('-movflags faststart')
  356. const parsedAudio = await audio.get(input)
  357. if (!parsedAudio.audioStream) {
  358. localCommand = localCommand.noAudio()
  359. } else if ((await checkFFmpegEncoders()).get('libfdk_aac')) { // we favor VBR, if a good AAC encoder is available
  360. localCommand = localCommand
  361. .audioCodec('libfdk_aac')
  362. .audioQuality(5)
  363. } else {
  364. // we try to reduce the ceiling bitrate by making rough matches of bitrates
  365. // of course this is far from perfect, but it might save some space in the end
  366. localCommand = localCommand.audioCodec('aac')
  367. const audioCodecName = parsedAudio.audioStream[ 'codec_name' ]
  368. if (audio.bitrate[ audioCodecName ]) {
  369. const bitrate = audio.bitrate[ audioCodecName ](parsedAudio.audioStream[ 'bit_rate' ])
  370. if (bitrate !== undefined && bitrate !== -1) localCommand = localCommand.audioBitrate(bitrate)
  371. }
  372. }
  373. if (fps) {
  374. // Constrained Encoding (VBV)
  375. // https://slhck.info/video/2017/03/01/rate-control.html
  376. // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
  377. const targetBitrate = getTargetBitrate(resolution, fps, VIDEO_TRANSCODING_FPS)
  378. localCommand = localCommand.outputOptions([ `-maxrate ${targetBitrate}`, `-bufsize ${targetBitrate * 2}` ])
  379. // Keyframe interval of 2 seconds for faster seeking and resolution switching.
  380. // https://streaminglearningcenter.com/blogs/whats-the-right-keyframe-interval.html
  381. // https://superuser.com/a/908325
  382. localCommand = localCommand.outputOption(`-g ${fps * 2}`)
  383. }
  384. return localCommand
  385. }
  386. async function presetCopy (command: ffmpeg.FfmpegCommand): Promise<ffmpeg.FfmpegCommand> {
  387. return command
  388. .format('mp4')
  389. .videoCodec('copy')
  390. .audioCodec('copy')
  391. }