diff options
author | Jelle Besseling <jelle@pingiun.com> | 2021-08-17 08:26:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-17 08:26:20 +0200 |
commit | 0305db28c98fd6cf43a3c50ba92c76215e99d512 (patch) | |
tree | 33b753a19728d9f453c1aa4f19b36ac797e5fe80 /scripts | |
parent | f88ae8f5bc223579313b28582de9101944a4a814 (diff) | |
download | PeerTube-0305db28c98fd6cf43a3c50ba92c76215e99d512.tar.gz PeerTube-0305db28c98fd6cf43a3c50ba92c76215e99d512.tar.zst PeerTube-0305db28c98fd6cf43a3c50ba92c76215e99d512.zip |
Add support for saving video files to object storage (#4290)
* Add support for saving video files to object storage
* Add support for custom url generation on s3 stored files
Uses two config keys to support url generation that doesn't directly go
to (compatible s3). Can be used to generate urls to any cache server or
CDN.
* Upload files to s3 concurrently and delete originals afterwards
* Only publish after move to object storage is complete
* Use base url instead of url template
* Fix mistyped config field
* Add rudenmentary way to download before transcode
* Implement Chocobozzz suggestions
https://github.com/Chocobozzz/PeerTube/pull/4290#issuecomment-891670478
The remarks in question:
Try to use objectStorage prefix instead of s3 prefix for your function/variables/config names
Prefer to use a tree for the config: s3.streaming_playlists_bucket -> object_storage.streaming_playlists.bucket
Use uppercase for config: S3.STREAMING_PLAYLISTS_BUCKETINFO.bucket -> OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET (maybe BUCKET_NAME instead of BUCKET)
I suggest to rename moveJobsRunning to pendingMovingJobs (or better, create a dedicated videoJobInfo table with a pendingMove & videoId columns so we could also use this table to track pending transcoding jobs)
https://github.com/Chocobozzz/PeerTube/pull/4290/files#diff-3e26d41ca4bda1de8e1747af70ca2af642abcc1e9e0bfb94239ff2165acfbde5R19 uses a string instead of an integer
I think we should store the origin object storage URL in fileUrl, without base_url injection. Instead, inject the base_url at "runtime" so admins can easily change this configuration without running a script to update DB URLs
* Import correct function
* Support multipart upload
* Remove import of node 15.0 module stream/promises
* Extend maximum upload job length
Using the same value as for redundancy downloading seems logical
* Use dynamic part size for really large uploads
Also adds very small part size for local testing
* Fix decreasePendingMove query
* Resolve various PR comments
* Move to object storage after optimize
* Make upload size configurable and increase default
* Prune webtorrent files that are stored in object storage
* Move files after transcoding jobs
* Fix federation
* Add video path manager
* Support move to external storage job in client
* Fix live object storage tests
Co-authored-by: Chocobozzz <me@florianbigard.com>
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/ci.sh | 3 | ||||
-rwxr-xr-x | scripts/create-transcoding-job.ts | 13 | ||||
-rw-r--r-- | scripts/optimize-old-videos.ts | 91 |
3 files changed, 61 insertions, 46 deletions
diff --git a/scripts/ci.sh b/scripts/ci.sh index 71b1be53b..f49dbe6ad 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh | |||
@@ -89,9 +89,10 @@ elif [ "$1" = "api-4" ]; then | |||
89 | 89 | ||
90 | moderationFiles=$(findTestFiles ./dist/server/tests/api/moderation) | 90 | moderationFiles=$(findTestFiles ./dist/server/tests/api/moderation) |
91 | redundancyFiles=$(findTestFiles ./dist/server/tests/api/redundancy) | 91 | redundancyFiles=$(findTestFiles ./dist/server/tests/api/redundancy) |
92 | objectStorageFiles=$(findTestFiles ./dist/server/tests/api/object-storage) | ||
92 | activitypubFiles=$(findTestFiles ./dist/server/tests/api/activitypub) | 93 | activitypubFiles=$(findTestFiles ./dist/server/tests/api/activitypub) |
93 | 94 | ||
94 | MOCHA_PARALLEL=true TS_NODE_FILES=true runTest "$1" 2 $moderationFiles $redundancyFiles $activitypubFiles | 95 | MOCHA_PARALLEL=true TS_NODE_FILES=true runTest "$1" 2 $moderationFiles $redundancyFiles $activitypubFiles $objectStorageFiles |
95 | elif [ "$1" = "external-plugins" ]; then | 96 | elif [ "$1" = "external-plugins" ]; then |
96 | npm run build:server | 97 | npm run build:server |
97 | 98 | ||
diff --git a/scripts/create-transcoding-job.ts b/scripts/create-transcoding-job.ts index 3a552c19a..0bb9bfeab 100755 --- a/scripts/create-transcoding-job.ts +++ b/scripts/create-transcoding-job.ts | |||
@@ -6,9 +6,10 @@ import { VideoModel } from '../server/models/video/video' | |||
6 | import { initDatabaseModels } from '../server/initializers/database' | 6 | import { initDatabaseModels } from '../server/initializers/database' |
7 | import { JobQueue } from '../server/lib/job-queue' | 7 | import { JobQueue } from '../server/lib/job-queue' |
8 | import { computeResolutionsToTranscode } from '@server/helpers/ffprobe-utils' | 8 | import { computeResolutionsToTranscode } from '@server/helpers/ffprobe-utils' |
9 | import { VideoTranscodingPayload } from '@shared/models' | 9 | import { VideoState, VideoTranscodingPayload } from '@shared/models' |
10 | import { CONFIG } from '@server/initializers/config' | 10 | import { CONFIG } from '@server/initializers/config' |
11 | import { isUUIDValid } from '@server/helpers/custom-validators/misc' | 11 | import { isUUIDValid } from '@server/helpers/custom-validators/misc' |
12 | import { addTranscodingJob } from '@server/lib/video' | ||
12 | 13 | ||
13 | program | 14 | program |
14 | .option('-v, --video [videoUUID]', 'Video UUID') | 15 | .option('-v, --video [videoUUID]', 'Video UUID') |
@@ -47,7 +48,7 @@ async function run () { | |||
47 | if (!video) throw new Error('Video not found.') | 48 | if (!video) throw new Error('Video not found.') |
48 | 49 | ||
49 | const dataInput: VideoTranscodingPayload[] = [] | 50 | const dataInput: VideoTranscodingPayload[] = [] |
50 | const { resolution } = await video.getMaxQualityResolution() | 51 | const resolution = video.getMaxQualityFile().resolution |
51 | 52 | ||
52 | // Generate HLS files | 53 | // Generate HLS files |
53 | if (options.generateHls || CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) { | 54 | if (options.generateHls || CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) { |
@@ -62,6 +63,7 @@ async function run () { | |||
62 | resolution, | 63 | resolution, |
63 | isPortraitMode: false, | 64 | isPortraitMode: false, |
64 | copyCodecs: false, | 65 | copyCodecs: false, |
66 | isNewVideo: false, | ||
65 | isMaxQuality: false | 67 | isMaxQuality: false |
66 | }) | 68 | }) |
67 | } | 69 | } |
@@ -87,10 +89,13 @@ async function run () { | |||
87 | } | 89 | } |
88 | } | 90 | } |
89 | 91 | ||
90 | await JobQueue.Instance.init() | 92 | JobQueue.Instance.init() |
93 | |||
94 | video.state = VideoState.TO_TRANSCODE | ||
95 | await video.save() | ||
91 | 96 | ||
92 | for (const d of dataInput) { | 97 | for (const d of dataInput) { |
93 | await JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: d }) | 98 | await addTranscodingJob(d, {}) |
94 | console.log('Transcoding job for video %s created.', video.uuid) | 99 | console.log('Transcoding job for video %s created.', video.uuid) |
95 | } | 100 | } |
96 | } | 101 | } |
diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts index 9e66105dd..245e4cf28 100644 --- a/scripts/optimize-old-videos.ts +++ b/scripts/optimize-old-videos.ts | |||
@@ -1,15 +1,18 @@ | |||
1 | import { registerTSPaths } from '../server/helpers/register-ts-paths' | 1 | import { registerTSPaths } from '../server/helpers/register-ts-paths' |
2 | registerTSPaths() | 2 | registerTSPaths() |
3 | 3 | ||
4 | import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffprobe-utils' | ||
5 | import { VideoModel } from '../server/models/video/video' | ||
6 | import { optimizeOriginalVideofile } from '../server/lib/transcoding/video-transcoding' | ||
7 | import { initDatabaseModels } from '../server/initializers/database' | ||
8 | import { basename, dirname } from 'path' | ||
9 | import { copy, move, remove } from 'fs-extra' | 4 | import { copy, move, remove } from 'fs-extra' |
5 | import { basename, dirname } from 'path' | ||
10 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | 6 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' |
11 | import { getVideoFilePath } from '@server/lib/video-paths' | 7 | import { CONFIG } from '@server/initializers/config' |
8 | import { processMoveToObjectStorage } from '@server/lib/job-queue/handlers/move-to-object-storage' | ||
9 | import { VideoPathManager } from '@server/lib/video-path-manager' | ||
12 | import { getMaxBitrate } from '@shared/core-utils' | 10 | import { getMaxBitrate } from '@shared/core-utils' |
11 | import { MoveObjectStoragePayload } from '@shared/models' | ||
12 | import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffprobe-utils' | ||
13 | import { initDatabaseModels } from '../server/initializers/database' | ||
14 | import { optimizeOriginalVideofile } from '../server/lib/transcoding/video-transcoding' | ||
15 | import { VideoModel } from '../server/models/video/video' | ||
13 | 16 | ||
14 | run() | 17 | run() |
15 | .then(() => process.exit(0)) | 18 | .then(() => process.exit(0)) |
@@ -39,43 +42,49 @@ async function run () { | |||
39 | currentVideoId = video.id | 42 | currentVideoId = video.id |
40 | 43 | ||
41 | for (const file of video.VideoFiles) { | 44 | for (const file of video.VideoFiles) { |
42 | currentFilePath = getVideoFilePath(video, file) | 45 | await VideoPathManager.Instance.makeAvailableVideoFile(video, file, async path => { |
43 | 46 | currentFilePath = path | |
44 | const [ videoBitrate, fps, dataResolution ] = await Promise.all([ | 47 | |
45 | getVideoFileBitrate(currentFilePath), | 48 | const [ videoBitrate, fps, dataResolution ] = await Promise.all([ |
46 | getVideoFileFPS(currentFilePath), | 49 | getVideoFileBitrate(currentFilePath), |
47 | getVideoFileResolution(currentFilePath) | 50 | getVideoFileFPS(currentFilePath), |
48 | ]) | 51 | getVideoFileResolution(currentFilePath) |
49 | 52 | ]) | |
50 | const maxBitrate = getMaxBitrate({ ...dataResolution, fps }) | 53 | |
51 | const isMaxBitrateExceeded = videoBitrate > maxBitrate | 54 | const maxBitrate = getMaxBitrate({ ...dataResolution, fps }) |
52 | if (isMaxBitrateExceeded) { | 55 | const isMaxBitrateExceeded = videoBitrate > maxBitrate |
53 | console.log( | 56 | if (isMaxBitrateExceeded) { |
54 | 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)', | 57 | console.log( |
55 | basename(currentFilePath), videoBitrate / 1000, maxBitrate / 1000 | 58 | 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)', |
56 | ) | 59 | basename(currentFilePath), videoBitrate / 1000, maxBitrate / 1000 |
57 | 60 | ) | |
58 | const backupFile = `${currentFilePath}_backup` | 61 | |
59 | await copy(currentFilePath, backupFile) | 62 | const backupFile = `${currentFilePath}_backup` |
60 | 63 | await copy(currentFilePath, backupFile) | |
61 | await optimizeOriginalVideofile(video, file) | 64 | |
62 | // Update file path, the video filename changed | 65 | await optimizeOriginalVideofile(video, file) |
63 | currentFilePath = getVideoFilePath(video, file) | 66 | // Update file path, the video filename changed |
64 | 67 | currentFilePath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, file) | |
65 | const originalDuration = await getDurationFromVideoFile(backupFile) | 68 | |
66 | const newDuration = await getDurationFromVideoFile(currentFilePath) | 69 | const originalDuration = await getDurationFromVideoFile(backupFile) |
67 | 70 | const newDuration = await getDurationFromVideoFile(currentFilePath) | |
68 | if (originalDuration === newDuration) { | 71 | |
69 | console.log('Finished optimizing %s', basename(currentFilePath)) | 72 | if (originalDuration === newDuration) { |
70 | await remove(backupFile) | 73 | console.log('Finished optimizing %s', basename(currentFilePath)) |
71 | continue | 74 | await remove(backupFile) |
75 | return | ||
76 | } | ||
77 | |||
78 | console.log('Failed to optimize %s, restoring original', basename(currentFilePath)) | ||
79 | await move(backupFile, currentFilePath, { overwrite: true }) | ||
80 | await createTorrentAndSetInfoHash(video, file) | ||
81 | await file.save() | ||
72 | } | 82 | } |
83 | }) | ||
84 | } | ||
73 | 85 | ||
74 | console.log('Failed to optimize %s, restoring original', basename(currentFilePath)) | 86 | if (CONFIG.OBJECT_STORAGE.ENABLED === true) { |
75 | await move(backupFile, currentFilePath, { overwrite: true }) | 87 | await processMoveToObjectStorage({ data: { videoUUID: video.uuid } as MoveObjectStoragePayload } as any) |
76 | await createTorrentAndSetInfoHash(video, file) | ||
77 | await file.save() | ||
78 | } | ||
79 | } | 88 | } |
80 | } | 89 | } |
81 | 90 | ||