diff options
Diffstat (limited to 'scripts/optimize-old-videos.ts')
-rw-r--r-- | scripts/optimize-old-videos.ts | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts index c93f82316..1bee1b0f3 100644 --- a/scripts/optimize-old-videos.ts +++ b/scripts/optimize-old-videos.ts | |||
@@ -1,10 +1,11 @@ | |||
1 | import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants' | 1 | import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants' |
2 | import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils' | 2 | import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution, getDurationFromVideoFile } from '../server/helpers/ffmpeg-utils' |
3 | import { getMaxBitrate } from '../shared/models/videos' | 3 | import { getMaxBitrate } from '../shared/models/videos' |
4 | import { VideoModel } from '../server/models/video/video' | 4 | import { VideoModel } from '../server/models/video/video' |
5 | import { optimizeVideofile } from '../server/lib/video-transcoding' | 5 | import { optimizeVideofile } from '../server/lib/video-transcoding' |
6 | import { initDatabaseModels } from '../server/initializers' | 6 | import { initDatabaseModels } from '../server/initializers' |
7 | import { join } from 'path' | 7 | import { join, basename, dirname } from 'path' |
8 | import { copy, remove, move } from 'fs-extra' | ||
8 | 9 | ||
9 | run() | 10 | run() |
10 | .then(() => process.exit(0)) | 11 | .then(() => process.exit(0)) |
@@ -13,24 +14,51 @@ run() | |||
13 | process.exit(-1) | 14 | process.exit(-1) |
14 | }) | 15 | }) |
15 | 16 | ||
17 | let currentVideoId = null | ||
18 | let currentFile = null | ||
19 | |||
20 | process.on('SIGINT', async function () { | ||
21 | console.log('Cleaning up temp files') | ||
22 | await remove(`${currentFile}_backup`) | ||
23 | await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`) | ||
24 | process.exit(0) | ||
25 | }) | ||
26 | |||
16 | async function run () { | 27 | async function run () { |
17 | await initDatabaseModels(true) | 28 | await initDatabaseModels(true) |
18 | 29 | ||
19 | const localVideos = await VideoModel.listLocal() | 30 | const localVideos = await VideoModel.listLocal() |
20 | 31 | ||
21 | for (const video of localVideos) { | 32 | for (const video of localVideos) { |
33 | currentVideoId = video.id | ||
22 | for (const file of video.VideoFiles) { | 34 | for (const file of video.VideoFiles) { |
23 | const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) | 35 | currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) |
24 | 36 | ||
25 | const [ videoBitrate, fps, resolution ] = await Promise.all([ | 37 | const [ videoBitrate, fps, resolution ] = await Promise.all([ |
26 | getVideoFileBitrate(inputPath), | 38 | getVideoFileBitrate(currentFile), |
27 | getVideoFileFPS(inputPath), | 39 | getVideoFileFPS(currentFile), |
28 | getVideoFileResolution(inputPath) | 40 | getVideoFileResolution(currentFile) |
29 | ]) | 41 | ]) |
30 | 42 | ||
31 | const isMaxBitrateExceeded = videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) | 43 | const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) |
44 | const isMaxBitrateExceeded = videoBitrate > maxBitrate | ||
32 | if (isMaxBitrateExceeded) { | 45 | if (isMaxBitrateExceeded) { |
46 | console.log('Optimizing video file %s with bitrate %s kbps (max: %s kbps)', | ||
47 | basename(currentFile), videoBitrate / 1000, maxBitrate / 1000) | ||
48 | const backupFile = `${currentFile}_backup` | ||
49 | await copy(currentFile, backupFile) | ||
33 | await optimizeVideofile(video, file) | 50 | await optimizeVideofile(video, file) |
51 | const originalDuration = await getDurationFromVideoFile(backupFile) | ||
52 | const newDuration = await getDurationFromVideoFile(currentFile) | ||
53 | if (originalDuration === newDuration) { | ||
54 | console.log('Finished optimizing %s', basename(currentFile)) | ||
55 | await remove(backupFile) | ||
56 | } else { | ||
57 | console.log('Failed to optimize %s, restoring original', basename(currentFile)) | ||
58 | move(backupFile, currentFile, { overwrite: true }) | ||
59 | await video.createTorrentAndSetInfoHash(file) | ||
60 | await file.save() | ||
61 | } | ||
34 | } | 62 | } |
35 | } | 63 | } |
36 | } | 64 | } |