]>
Commit | Line | Data |
---|---|---|
2aaa1a3f | 1 | import { registerTSPaths } from '../server/helpers/register-ts-paths' |
66fb2aa3 C |
2 | registerTSPaths() |
3 | ||
daf6e480 | 4 | import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffprobe-utils' |
edb4ffc7 | 5 | import { VideoModel } from '../server/models/video/video' |
c07902b9 | 6 | import { optimizeOriginalVideofile } from '../server/lib/transcoding/video-transcoding' |
80fdaf06 | 7 | import { initDatabaseModels } from '../server/initializers/database' |
d7a25329 | 8 | import { basename, dirname } from 'path' |
74dc3bca | 9 | import { copy, move, remove } from 'fs-extra' |
d7a25329 C |
10 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' |
11 | import { getVideoFilePath } from '@server/lib/video-paths' | |
679c12e6 | 12 | import { getMaxBitrate } from '@shared/core-utils' |
d7a25329 | 13 | |
edb4ffc7 FA |
14 | run() |
15 | .then(() => process.exit(0)) | |
16 | .catch(err => { | |
17 | console.error(err) | |
18 | process.exit(-1) | |
19 | }) | |
20 | ||
764b1a14 C |
21 | let currentVideoId: string |
22 | let currentFilePath: string | |
5e10e8d7 FA |
23 | |
24 | process.on('SIGINT', async function () { | |
25 | console.log('Cleaning up temp files') | |
764b1a14 C |
26 | await remove(`${currentFilePath}_backup`) |
27 | await remove(`${dirname(currentFilePath)}/${currentVideoId}-transcoded.mp4`) | |
5e10e8d7 FA |
28 | process.exit(0) |
29 | }) | |
30 | ||
edb4ffc7 | 31 | async function run () { |
74cd011b C |
32 | await initDatabaseModels(true) |
33 | ||
9f1ddd24 | 34 | const localVideos = await VideoModel.listLocal() |
edb4ffc7 | 35 | |
90a8bd30 C |
36 | for (const localVideo of localVideos) { |
37 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(localVideo.id) | |
38 | ||
5e10e8d7 | 39 | currentVideoId = video.id |
5b77537c | 40 | |
9f1ddd24 | 41 | for (const file of video.VideoFiles) { |
764b1a14 | 42 | currentFilePath = getVideoFilePath(video, file) |
9f1ddd24 | 43 | |
679c12e6 | 44 | const [ videoBitrate, fps, dataResolution ] = await Promise.all([ |
764b1a14 C |
45 | getVideoFileBitrate(currentFilePath), |
46 | getVideoFileFPS(currentFilePath), | |
47 | getVideoFileResolution(currentFilePath) | |
9f1ddd24 C |
48 | ]) |
49 | ||
679c12e6 | 50 | const maxBitrate = getMaxBitrate({ ...dataResolution, fps }) |
5e10e8d7 | 51 | const isMaxBitrateExceeded = videoBitrate > maxBitrate |
9f1ddd24 | 52 | if (isMaxBitrateExceeded) { |
5b77537c C |
53 | console.log( |
54 | 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)', | |
764b1a14 | 55 | basename(currentFilePath), videoBitrate / 1000, maxBitrate / 1000 |
5b77537c C |
56 | ) |
57 | ||
764b1a14 C |
58 | const backupFile = `${currentFilePath}_backup` |
59 | await copy(currentFilePath, backupFile) | |
5b77537c | 60 | |
d7a25329 | 61 | await optimizeOriginalVideofile(video, file) |
764b1a14 C |
62 | // Update file path, the video filename changed |
63 | currentFilePath = getVideoFilePath(video, file) | |
5b77537c | 64 | |
5e10e8d7 | 65 | const originalDuration = await getDurationFromVideoFile(backupFile) |
764b1a14 | 66 | const newDuration = await getDurationFromVideoFile(currentFilePath) |
5b77537c | 67 | |
5e10e8d7 | 68 | if (originalDuration === newDuration) { |
764b1a14 | 69 | console.log('Finished optimizing %s', basename(currentFilePath)) |
5e10e8d7 | 70 | await remove(backupFile) |
51326912 | 71 | continue |
5e10e8d7 | 72 | } |
5b77537c | 73 | |
764b1a14 C |
74 | console.log('Failed to optimize %s, restoring original', basename(currentFilePath)) |
75 | await move(backupFile, currentFilePath, { overwrite: true }) | |
8efc27bf | 76 | await createTorrentAndSetInfoHash(video, file) |
5b77537c | 77 | await file.save() |
9f1ddd24 | 78 | } |
edb4ffc7 FA |
79 | } |
80 | } | |
9f1ddd24 | 81 | |
edb4ffc7 FA |
82 | console.log('Finished optimizing videos') |
83 | } |