1 import { FfmpegCommand } from 'fluent-ffmpeg'
2 import { getFFmpegCommandWrapperOptions } from '@server/helpers/ffmpeg'
3 import { logger } from '@server/helpers/logger'
4 import { CONFIG } from '@server/initializers/config'
5 import { VIDEO_LIVE } from '@server/initializers/constants'
6 import { VideoTranscodingProfilesManager } from '@server/lib/transcoding/default-transcoding-profiles'
7 import { FFmpegLive } from '@shared/ffmpeg'
8 import { getLiveSegmentTime } from '../../live-utils'
9 import { AbstractTranscodingWrapper } from './abstract-transcoding-wrapper'
11 export class FFmpegTranscodingWrapper extends AbstractTranscodingWrapper {
12 private ffmpegCommand: FfmpegCommand
16 this.ffmpegCommand = CONFIG.LIVE.TRANSCODING.ENABLED
17 ? await this.buildFFmpegLive().getLiveTranscodingCommand({
18 inputUrl: this.inputLocalUrl,
20 outPath: this.outDirectory,
21 masterPlaylistName: this.streamingPlaylist.playlistFilename,
23 segmentListSize: this.segmentListSize,
24 segmentDuration: this.segmentDuration,
26 toTranscode: this.toTranscode,
28 bitrate: this.bitrate,
31 hasAudio: this.hasAudio
33 : this.buildFFmpegLive().getLiveMuxingCommand({
34 inputUrl: this.inputLocalUrl,
35 outPath: this.outDirectory,
37 masterPlaylistName: this.streamingPlaylist.playlistFilename,
39 segmentListSize: VIDEO_LIVE.SEGMENTS_LIST_SIZE,
40 segmentDuration: getLiveSegmentTime(this.videoLive.latencyMode)
43 logger.info('Running local live muxing/transcoding for %s.', this.videoUUID, this.lTags())
45 this.ffmpegCommand.run()
47 let ffmpegShellCommand: string
48 this.ffmpegCommand.on('start', cmdline => {
49 ffmpegShellCommand = cmdline
51 logger.debug('Running ffmpeg command for live', { ffmpegShellCommand, ...this.lTags() })
54 this.ffmpegCommand.on('error', (err, stdout, stderr) => {
55 this.onFFmpegError({ err, stdout, stderr, ffmpegShellCommand })
58 this.ffmpegCommand.on('end', () => {
62 this.ffmpegCommand.run()
66 // Nothing to do, ffmpeg will automatically exit
69 private onFFmpegError (options: {
73 ffmpegShellCommand: string
75 const { err, stdout, stderr, ffmpegShellCommand } = options
77 // Don't care that we killed the ffmpeg process
78 if (err?.message?.includes('Exiting normally')) return
80 logger.error('FFmpeg transcoding error.', { err, stdout, stderr, ffmpegShellCommand, ...this.lTags() })
82 this.emit('error', { err })
85 private onFFmpegEnded () {
86 if (this.ended) return
92 private buildFFmpegLive () {
93 return new FFmpegLive(getFFmpegCommandWrapperOptions('live', VideoTranscodingProfilesManager.Instance.getAvailableEncoders()))