diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/lib/live/shared/transcoding-wrapper/ffmpeg-transcoding-wrapper.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/lib/live/shared/transcoding-wrapper/ffmpeg-transcoding-wrapper.ts')
-rw-r--r-- | server/lib/live/shared/transcoding-wrapper/ffmpeg-transcoding-wrapper.ts | 107 |
1 files changed, 0 insertions, 107 deletions
diff --git a/server/lib/live/shared/transcoding-wrapper/ffmpeg-transcoding-wrapper.ts b/server/lib/live/shared/transcoding-wrapper/ffmpeg-transcoding-wrapper.ts deleted file mode 100644 index c6ee8ebf1..000000000 --- a/server/lib/live/shared/transcoding-wrapper/ffmpeg-transcoding-wrapper.ts +++ /dev/null | |||
@@ -1,107 +0,0 @@ | |||
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 | ||
13 | |||
14 | private aborted = false | ||
15 | private errored = false | ||
16 | private ended = false | ||
17 | |||
18 | async run () { | ||
19 | this.ffmpegCommand = CONFIG.LIVE.TRANSCODING.ENABLED | ||
20 | ? await this.buildFFmpegLive().getLiveTranscodingCommand({ | ||
21 | inputUrl: this.inputLocalUrl, | ||
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({ | ||
37 | inputUrl: this.inputLocalUrl, | ||
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 | |||
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 () { | ||
67 | if (this.ended || this.errored || this.aborted) return | ||
68 | |||
69 | logger.debug('Killing ffmpeg after live abort of ' + this.videoUUID, this.lTags()) | ||
70 | |||
71 | this.ffmpegCommand.kill('SIGINT') | ||
72 | |||
73 | this.aborted = true | ||
74 | this.emit('end') | ||
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 | ||
87 | if (this.ended || this.errored || this.aborted) return | ||
88 | |||
89 | logger.error('FFmpeg transcoding error.', { err, stdout, stderr, ffmpegShellCommand, ...this.lTags() }) | ||
90 | |||
91 | this.errored = true | ||
92 | this.emit('error', { err }) | ||
93 | } | ||
94 | |||
95 | private onFFmpegEnded () { | ||
96 | if (this.ended || this.errored || this.aborted) return | ||
97 | |||
98 | logger.debug('Live ffmpeg transcoding ended for ' + this.videoUUID, this.lTags()) | ||
99 | |||
100 | this.ended = true | ||
101 | this.emit('end') | ||
102 | } | ||
103 | |||
104 | private buildFFmpegLive () { | ||
105 | return new FFmpegLive(getFFmpegCommandWrapperOptions('live', VideoTranscodingProfilesManager.Instance.getAvailableEncoders())) | ||
106 | } | ||
107 | } | ||