diff options
Diffstat (limited to 'scripts/optimize-old-videos.ts')
-rw-r--r-- | scripts/optimize-old-videos.ts | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts deleted file mode 100644 index 245e4cf28..000000000 --- a/scripts/optimize-old-videos.ts +++ /dev/null | |||
@@ -1,92 +0,0 @@ | |||
1 | import { registerTSPaths } from '../server/helpers/register-ts-paths' | ||
2 | registerTSPaths() | ||
3 | |||
4 | import { copy, move, remove } from 'fs-extra' | ||
5 | import { basename, dirname } from 'path' | ||
6 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | ||
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' | ||
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' | ||
16 | |||
17 | run() | ||
18 | .then(() => process.exit(0)) | ||
19 | .catch(err => { | ||
20 | console.error(err) | ||
21 | process.exit(-1) | ||
22 | }) | ||
23 | |||
24 | let currentVideoId: string | ||
25 | let currentFilePath: string | ||
26 | |||
27 | process.on('SIGINT', async function () { | ||
28 | console.log('Cleaning up temp files') | ||
29 | await remove(`${currentFilePath}_backup`) | ||
30 | await remove(`${dirname(currentFilePath)}/${currentVideoId}-transcoded.mp4`) | ||
31 | process.exit(0) | ||
32 | }) | ||
33 | |||
34 | async function run () { | ||
35 | await initDatabaseModels(true) | ||
36 | |||
37 | const localVideos = await VideoModel.listLocal() | ||
38 | |||
39 | for (const localVideo of localVideos) { | ||
40 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(localVideo.id) | ||
41 | |||
42 | currentVideoId = video.id | ||
43 | |||
44 | for (const file of video.VideoFiles) { | ||
45 | await VideoPathManager.Instance.makeAvailableVideoFile(video, file, async path => { | ||
46 | currentFilePath = path | ||
47 | |||
48 | const [ videoBitrate, fps, dataResolution ] = await Promise.all([ | ||
49 | getVideoFileBitrate(currentFilePath), | ||
50 | getVideoFileFPS(currentFilePath), | ||
51 | getVideoFileResolution(currentFilePath) | ||
52 | ]) | ||
53 | |||
54 | const maxBitrate = getMaxBitrate({ ...dataResolution, fps }) | ||
55 | const isMaxBitrateExceeded = videoBitrate > maxBitrate | ||
56 | if (isMaxBitrateExceeded) { | ||
57 | console.log( | ||
58 | 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)', | ||
59 | basename(currentFilePath), videoBitrate / 1000, maxBitrate / 1000 | ||
60 | ) | ||
61 | |||
62 | const backupFile = `${currentFilePath}_backup` | ||
63 | await copy(currentFilePath, backupFile) | ||
64 | |||
65 | await optimizeOriginalVideofile(video, file) | ||
66 | // Update file path, the video filename changed | ||
67 | currentFilePath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, file) | ||
68 | |||
69 | const originalDuration = await getDurationFromVideoFile(backupFile) | ||
70 | const newDuration = await getDurationFromVideoFile(currentFilePath) | ||
71 | |||
72 | if (originalDuration === newDuration) { | ||
73 | console.log('Finished optimizing %s', basename(currentFilePath)) | ||
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() | ||
82 | } | ||
83 | }) | ||
84 | } | ||
85 | |||
86 | if (CONFIG.OBJECT_STORAGE.ENABLED === true) { | ||
87 | await processMoveToObjectStorage({ data: { videoUUID: video.uuid } as MoveObjectStoragePayload } as any) | ||
88 | } | ||
89 | } | ||
90 | |||
91 | console.log('Finished optimizing videos') | ||
92 | } | ||