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