]>
Commit | Line | Data |
---|---|---|
0c9668f7 C |
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' | |
10 | ||
11 | export class FFmpegTranscodingWrapper extends AbstractTranscodingWrapper { | |
12 | private ffmpegCommand: FfmpegCommand | |
ef2e6aab C |
13 | |
14 | private aborted = false | |
15 | private errored = false | |
0c9668f7 C |
16 | private ended = false |
17 | ||
18 | async run () { | |
19 | this.ffmpegCommand = CONFIG.LIVE.TRANSCODING.ENABLED | |
20 | ? await this.buildFFmpegLive().getLiveTranscodingCommand({ | |
28705705 | 21 | inputUrl: this.inputLocalUrl, |
0c9668f7 C |
22 | |
23 | outPath: this.outDirectory, | |
24 | masterPlaylistName: this.streamingPlaylist.playlistFilename, | |
25 | ||
26 | segmentListSize: this.segmentListSize, | |
27 | segmentDuration: this.segmentDuration, | |
28 | ||
29 | toTranscode: this.toTranscode, | |
30 | ||
31 | bitrate: this.bitrate, | |
32 | ratio: this.ratio, | |
33 | ||
34 | hasAudio: this.hasAudio | |
35 | }) | |
36 | : this.buildFFmpegLive().getLiveMuxingCommand({ | |
28705705 | 37 | inputUrl: this.inputLocalUrl, |
0c9668f7 C |
38 | outPath: this.outDirectory, |
39 | ||
40 | masterPlaylistName: this.streamingPlaylist.playlistFilename, | |
41 | ||
42 | segmentListSize: VIDEO_LIVE.SEGMENTS_LIST_SIZE, | |
43 | segmentDuration: getLiveSegmentTime(this.videoLive.latencyMode) | |
44 | }) | |
45 | ||
46 | logger.info('Running local live muxing/transcoding for %s.', this.videoUUID, this.lTags()) | |
47 | ||
0c9668f7 C |
48 | let ffmpegShellCommand: string |
49 | this.ffmpegCommand.on('start', cmdline => { | |
50 | ffmpegShellCommand = cmdline | |
51 | ||
52 | logger.debug('Running ffmpeg command for live', { ffmpegShellCommand, ...this.lTags() }) | |
53 | }) | |
54 | ||
55 | this.ffmpegCommand.on('error', (err, stdout, stderr) => { | |
56 | this.onFFmpegError({ err, stdout, stderr, ffmpegShellCommand }) | |
57 | }) | |
58 | ||
59 | this.ffmpegCommand.on('end', () => { | |
60 | this.onFFmpegEnded() | |
61 | }) | |
62 | ||
63 | this.ffmpegCommand.run() | |
64 | } | |
65 | ||
66 | abort () { | |
ef2e6aab C |
67 | if (this.ended || this.errored || this.aborted) return |
68 | ||
fe7019b2 C |
69 | logger.debug('Killing ffmpeg after live abort of ' + this.videoUUID, this.lTags()) |
70 | ||
ef2e6aab C |
71 | this.ffmpegCommand.kill('SIGINT') |
72 | ||
73 | this.aborted = true | |
74 | this.emit('end') | |
0c9668f7 C |
75 | } |
76 | ||
77 | private onFFmpegError (options: { | |
78 | err: any | |
79 | stdout: string | |
80 | stderr: string | |
81 | ffmpegShellCommand: string | |
82 | }) { | |
83 | const { err, stdout, stderr, ffmpegShellCommand } = options | |
84 | ||
85 | // Don't care that we killed the ffmpeg process | |
86 | if (err?.message?.includes('Exiting normally')) return | |
ef2e6aab | 87 | if (this.ended || this.errored || this.aborted) return |
0c9668f7 C |
88 | |
89 | logger.error('FFmpeg transcoding error.', { err, stdout, stderr, ffmpegShellCommand, ...this.lTags() }) | |
90 | ||
ef2e6aab | 91 | this.errored = true |
0c9668f7 C |
92 | this.emit('error', { err }) |
93 | } | |
94 | ||
95 | private onFFmpegEnded () { | |
ef2e6aab | 96 | if (this.ended || this.errored || this.aborted) return |
0c9668f7 | 97 | |
fe7019b2 C |
98 | logger.debug('Live ffmpeg transcoding ended for ' + this.videoUUID, this.lTags()) |
99 | ||
0c9668f7 C |
100 | this.ended = true |
101 | this.emit('end') | |
102 | } | |
103 | ||
104 | private buildFFmpegLive () { | |
105 | return new FFmpegLive(getFFmpegCommandWrapperOptions('live', VideoTranscodingProfilesManager.Instance.getAvailableEncoders())) | |
106 | } | |
107 | } |