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