diff options
author | Chocobozzz <me@florianbigard.com> | 2020-10-26 16:44:23 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-11-09 15:33:04 +0100 |
commit | b5b687550d8ef8beafdf706e45d6556fb5f4c876 (patch) | |
tree | 232412d463c78af1f7ab5797db5aecf1096d08da /server/lib/job-queue/handlers | |
parent | ef680f68351ec10ab73a1131570a6d14ce14c195 (diff) | |
download | PeerTube-b5b687550d8ef8beafdf706e45d6556fb5f4c876.tar.gz PeerTube-b5b687550d8ef8beafdf706e45d6556fb5f4c876.tar.zst PeerTube-b5b687550d8ef8beafdf706e45d6556fb5f4c876.zip |
Add ability to save live replay
Diffstat (limited to 'server/lib/job-queue/handlers')
-rw-r--r-- | server/lib/job-queue/handlers/video-live-ending.ts | 84 | ||||
-rw-r--r-- | server/lib/job-queue/handlers/video-transcoding.ts | 30 |
2 files changed, 94 insertions, 20 deletions
diff --git a/server/lib/job-queue/handlers/video-live-ending.ts b/server/lib/job-queue/handlers/video-live-ending.ts index 1a58a9f7e..1a9a36129 100644 --- a/server/lib/job-queue/handlers/video-live-ending.ts +++ b/server/lib/job-queue/handlers/video-live-ending.ts | |||
@@ -1,24 +1,89 @@ | |||
1 | import * as Bull from 'bull' | 1 | import * as Bull from 'bull' |
2 | import { readdir, remove } from 'fs-extra' | 2 | import { readdir, remove } from 'fs-extra' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils' | ||
4 | import { getHLSDirectory } from '@server/lib/video-paths' | 5 | import { getHLSDirectory } from '@server/lib/video-paths' |
6 | import { generateHlsPlaylist } from '@server/lib/video-transcoding' | ||
5 | import { VideoModel } from '@server/models/video/video' | 7 | import { VideoModel } from '@server/models/video/video' |
8 | import { VideoLiveModel } from '@server/models/video/video-live' | ||
6 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' | 9 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
7 | import { VideoLiveEndingPayload } from '@shared/models' | 10 | import { MStreamingPlaylist, MVideo } from '@server/types/models' |
11 | import { VideoLiveEndingPayload, VideoState } from '@shared/models' | ||
8 | import { logger } from '../../../helpers/logger' | 12 | import { logger } from '../../../helpers/logger' |
9 | 13 | ||
10 | async function processVideoLiveEnding (job: Bull.Job) { | 14 | async function processVideoLiveEnding (job: Bull.Job) { |
11 | const payload = job.data as VideoLiveEndingPayload | 15 | const payload = job.data as VideoLiveEndingPayload |
12 | 16 | ||
13 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId) | 17 | const video = await VideoModel.load(payload.videoId) |
14 | if (!video) { | 18 | const live = await VideoLiveModel.loadByVideoId(payload.videoId) |
15 | logger.warn('Video live %d does not exist anymore. Cannot cleanup.', payload.videoId) | 19 | |
20 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) | ||
21 | if (!video || !streamingPlaylist || !live) { | ||
22 | logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId) | ||
16 | return | 23 | return |
17 | } | 24 | } |
18 | 25 | ||
19 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) | 26 | if (live.saveReplay !== true) { |
27 | return cleanupLive(video, streamingPlaylist) | ||
28 | } | ||
29 | |||
30 | return saveLive(video, streamingPlaylist) | ||
31 | } | ||
32 | |||
33 | // --------------------------------------------------------------------------- | ||
34 | |||
35 | export { | ||
36 | processVideoLiveEnding | ||
37 | } | ||
38 | |||
39 | // --------------------------------------------------------------------------- | ||
40 | |||
41 | async function saveLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { | ||
42 | const videoFiles = await streamingPlaylist.get('VideoFiles') | ||
43 | const hlsDirectory = getHLSDirectory(video, false) | ||
44 | |||
45 | for (const videoFile of videoFiles) { | ||
46 | const playlistPath = join(hlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(videoFile.resolution)) | ||
47 | |||
48 | const mp4TmpName = buildMP4TmpName(videoFile.resolution) | ||
49 | await hlsPlaylistToFragmentedMP4(playlistPath, mp4TmpName) | ||
50 | } | ||
51 | |||
52 | await cleanupLiveFiles(hlsDirectory) | ||
53 | |||
54 | video.isLive = false | ||
55 | video.state = VideoState.TO_TRANSCODE | ||
56 | await video.save() | ||
57 | |||
58 | const videoWithFiles = await VideoModel.loadWithFiles(video.id) | ||
59 | |||
60 | for (const videoFile of videoFiles) { | ||
61 | const videoInputPath = buildMP4TmpName(videoFile.resolution) | ||
62 | const { isPortraitMode } = await getVideoFileResolution(videoInputPath) | ||
63 | |||
64 | await generateHlsPlaylist({ | ||
65 | video: videoWithFiles, | ||
66 | videoInputPath, | ||
67 | resolution: videoFile.resolution, | ||
68 | copyCodecs: true, | ||
69 | isPortraitMode | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | video.state = VideoState.PUBLISHED | ||
74 | await video.save() | ||
75 | } | ||
76 | |||
77 | async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { | ||
20 | const hlsDirectory = getHLSDirectory(video, false) | 78 | const hlsDirectory = getHLSDirectory(video, false) |
21 | 79 | ||
80 | await cleanupLiveFiles(hlsDirectory) | ||
81 | |||
82 | streamingPlaylist.destroy() | ||
83 | .catch(err => logger.error('Cannot remove live streaming playlist.', { err })) | ||
84 | } | ||
85 | |||
86 | async function cleanupLiveFiles (hlsDirectory: string) { | ||
22 | const files = await readdir(hlsDirectory) | 87 | const files = await readdir(hlsDirectory) |
23 | 88 | ||
24 | for (const filename of files) { | 89 | for (const filename of files) { |
@@ -35,13 +100,8 @@ async function processVideoLiveEnding (job: Bull.Job) { | |||
35 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | 100 | .catch(err => logger.error('Cannot remove %s.', p, { err })) |
36 | } | 101 | } |
37 | } | 102 | } |
38 | |||
39 | streamingPlaylist.destroy() | ||
40 | .catch(err => logger.error('Cannot remove live streaming playlist.', { err })) | ||
41 | } | 103 | } |
42 | 104 | ||
43 | // --------------------------------------------------------------------------- | 105 | function buildMP4TmpName (resolution: number) { |
44 | 106 | return resolution + 'tmp.mp4' | |
45 | export { | ||
46 | processVideoLiveEnding | ||
47 | } | 107 | } |
diff --git a/server/lib/job-queue/handlers/video-transcoding.ts b/server/lib/job-queue/handlers/video-transcoding.ts index 6659ab716..2aebc29f7 100644 --- a/server/lib/job-queue/handlers/video-transcoding.ts +++ b/server/lib/job-queue/handlers/video-transcoding.ts | |||
@@ -1,21 +1,22 @@ | |||
1 | import * as Bull from 'bull' | 1 | import * as Bull from 'bull' |
2 | import { getVideoFilePath } from '@server/lib/video-paths' | ||
3 | import { MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/types/models' | ||
2 | import { | 4 | import { |
3 | MergeAudioTranscodingPayload, | 5 | MergeAudioTranscodingPayload, |
4 | NewResolutionTranscodingPayload, | 6 | NewResolutionTranscodingPayload, |
5 | OptimizeTranscodingPayload, | 7 | OptimizeTranscodingPayload, |
6 | VideoTranscodingPayload | 8 | VideoTranscodingPayload |
7 | } from '../../../../shared' | 9 | } from '../../../../shared' |
10 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | ||
11 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' | ||
8 | import { logger } from '../../../helpers/logger' | 12 | import { logger } from '../../../helpers/logger' |
13 | import { CONFIG } from '../../../initializers/config' | ||
14 | import { sequelizeTypescript } from '../../../initializers/database' | ||
9 | import { VideoModel } from '../../../models/video/video' | 15 | import { VideoModel } from '../../../models/video/video' |
10 | import { JobQueue } from '../job-queue' | ||
11 | import { federateVideoIfNeeded } from '../../activitypub/videos' | 16 | import { federateVideoIfNeeded } from '../../activitypub/videos' |
12 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | ||
13 | import { sequelizeTypescript } from '../../../initializers/database' | ||
14 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' | ||
15 | import { generateHlsPlaylist, mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewResolution } from '../../video-transcoding' | ||
16 | import { Notifier } from '../../notifier' | 17 | import { Notifier } from '../../notifier' |
17 | import { CONFIG } from '../../../initializers/config' | 18 | import { generateHlsPlaylist, mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewResolution } from '../../video-transcoding' |
18 | import { MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/types/models' | 19 | import { JobQueue } from '../job-queue' |
19 | 20 | ||
20 | async function processVideoTranscoding (job: Bull.Job) { | 21 | async function processVideoTranscoding (job: Bull.Job) { |
21 | const payload = job.data as VideoTranscodingPayload | 22 | const payload = job.data as VideoTranscodingPayload |
@@ -29,7 +30,20 @@ async function processVideoTranscoding (job: Bull.Job) { | |||
29 | } | 30 | } |
30 | 31 | ||
31 | if (payload.type === 'hls') { | 32 | if (payload.type === 'hls') { |
32 | await generateHlsPlaylist(video, payload.resolution, payload.copyCodecs, payload.isPortraitMode || false) | 33 | const videoFileInput = payload.copyCodecs |
34 | ? video.getWebTorrentFile(payload.resolution) | ||
35 | : video.getMaxQualityFile() | ||
36 | |||
37 | const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist() | ||
38 | const videoInputPath = getVideoFilePath(videoOrStreamingPlaylist, videoFileInput) | ||
39 | |||
40 | await generateHlsPlaylist({ | ||
41 | video, | ||
42 | videoInputPath, | ||
43 | resolution: payload.resolution, | ||
44 | copyCodecs: payload.copyCodecs, | ||
45 | isPortraitMode: payload.isPortraitMode || false | ||
46 | }) | ||
33 | 47 | ||
34 | await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video) | 48 | await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video) |
35 | } else if (payload.type === 'new-resolution') { | 49 | } else if (payload.type === 'new-resolution') { |