diff options
Diffstat (limited to 'scripts/optimize-old-videos.ts')
-rw-r--r-- | scripts/optimize-old-videos.ts | 91 |
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 @@ | |||
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 | ||