aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/optimize-old-videos.ts
diff options
context:
space:
mode:
authorJelle Besseling <jelle@pingiun.com>2021-08-17 08:26:20 +0200
committerGitHub <noreply@github.com>2021-08-17 08:26:20 +0200
commit0305db28c98fd6cf43a3c50ba92c76215e99d512 (patch)
tree33b753a19728d9f453c1aa4f19b36ac797e5fe80 /scripts/optimize-old-videos.ts
parentf88ae8f5bc223579313b28582de9101944a4a814 (diff)
downloadPeerTube-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/optimize-old-videos.ts')
-rw-r--r--scripts/optimize-old-videos.ts91
1 files changed, 50 insertions, 41 deletions
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 @@
1import { registerTSPaths } from '../server/helpers/register-ts-paths' 1import { registerTSPaths } from '../server/helpers/register-ts-paths'
2registerTSPaths() 2registerTSPaths()
3 3
4import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffprobe-utils'
5import { VideoModel } from '../server/models/video/video'
6import { optimizeOriginalVideofile } from '../server/lib/transcoding/video-transcoding'
7import { initDatabaseModels } from '../server/initializers/database'
8import { basename, dirname } from 'path'
9import { copy, move, remove } from 'fs-extra' 4import { copy, move, remove } from 'fs-extra'
5import { basename, dirname } from 'path'
10import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' 6import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
11import { getVideoFilePath } from '@server/lib/video-paths' 7import { CONFIG } from '@server/initializers/config'
8import { processMoveToObjectStorage } from '@server/lib/job-queue/handlers/move-to-object-storage'
9import { VideoPathManager } from '@server/lib/video-path-manager'
12import { getMaxBitrate } from '@shared/core-utils' 10import { getMaxBitrate } from '@shared/core-utils'
11import { MoveObjectStoragePayload } from '@shared/models'
12import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffprobe-utils'
13import { initDatabaseModels } from '../server/initializers/database'
14import { optimizeOriginalVideofile } from '../server/lib/transcoding/video-transcoding'
15import { VideoModel } from '../server/models/video/video'
13 16
14run() 17run()
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