]>
Commit | Line | Data |
---|---|---|
74cd011b | 1 | import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants' |
5e10e8d7 | 2 | import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution, getDurationFromVideoFile } from '../server/helpers/ffmpeg-utils' |
edb4ffc7 | 3 | import { getMaxBitrate } from '../shared/models/videos' |
edb4ffc7 | 4 | import { VideoModel } from '../server/models/video/video' |
edb4ffc7 | 5 | import { optimizeVideofile } from '../server/lib/video-transcoding' |
74cd011b | 6 | import { initDatabaseModels } from '../server/initializers' |
5e10e8d7 FA |
7 | import { join, basename, dirname } from 'path' |
8 | import { copy, remove, move } from 'fs-extra' | |
edb4ffc7 FA |
9 | |
10 | run() | |
11 | .then(() => process.exit(0)) | |
12 | .catch(err => { | |
13 | console.error(err) | |
14 | process.exit(-1) | |
15 | }) | |
16 | ||
5e10e8d7 FA |
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 | ||
edb4ffc7 | 27 | async function run () { |
74cd011b C |
28 | await initDatabaseModels(true) |
29 | ||
9f1ddd24 | 30 | const localVideos = await VideoModel.listLocal() |
edb4ffc7 | 31 | |
9f1ddd24 | 32 | for (const video of localVideos) { |
5e10e8d7 | 33 | currentVideoId = video.id |
9f1ddd24 | 34 | for (const file of video.VideoFiles) { |
5e10e8d7 | 35 | currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) |
9f1ddd24 C |
36 | |
37 | const [ videoBitrate, fps, resolution ] = await Promise.all([ | |
5e10e8d7 FA |
38 | getVideoFileBitrate(currentFile), |
39 | getVideoFileFPS(currentFile), | |
40 | getVideoFileResolution(currentFile) | |
9f1ddd24 C |
41 | ]) |
42 | ||
5e10e8d7 FA |
43 | const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) |
44 | const isMaxBitrateExceeded = videoBitrate > maxBitrate | |
9f1ddd24 | 45 | if (isMaxBitrateExceeded) { |
5e10e8d7 FA |
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) | |
9f1ddd24 | 50 | await optimizeVideofile(video, file) |
5e10e8d7 FA |
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 | } | |
9f1ddd24 | 62 | } |
edb4ffc7 FA |
63 | } |
64 | } | |
9f1ddd24 | 65 | |
edb4ffc7 FA |
66 | console.log('Finished optimizing videos') |
67 | } |