]>
Commit | Line | Data |
---|---|---|
74dc3bca C |
1 | import { VIDEO_TRANSCODING_FPS } from '../server/initializers/constants' |
2 | import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } 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' |
74dc3bca C |
7 | import { basename, dirname, join } from 'path' |
8 | import { copy, move, remove } from 'fs-extra' | |
9 | import { CONFIG } from '../server/initializers/config' | |
edb4ffc7 FA |
10 | |
11 | run() | |
12 | .then(() => process.exit(0)) | |
13 | .catch(err => { | |
14 | console.error(err) | |
15 | process.exit(-1) | |
16 | }) | |
17 | ||
5e10e8d7 FA |
18 | let currentVideoId = null |
19 | let currentFile = null | |
20 | ||
21 | process.on('SIGINT', async function () { | |
22 | console.log('Cleaning up temp files') | |
23 | await remove(`${currentFile}_backup`) | |
24 | await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`) | |
25 | process.exit(0) | |
26 | }) | |
27 | ||
edb4ffc7 | 28 | async function run () { |
74cd011b C |
29 | await initDatabaseModels(true) |
30 | ||
9f1ddd24 | 31 | const localVideos = await VideoModel.listLocal() |
edb4ffc7 | 32 | |
9f1ddd24 | 33 | for (const video of localVideos) { |
5e10e8d7 | 34 | currentVideoId = video.id |
5b77537c | 35 | |
9f1ddd24 | 36 | for (const file of video.VideoFiles) { |
5e10e8d7 | 37 | currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) |
9f1ddd24 C |
38 | |
39 | const [ videoBitrate, fps, resolution ] = await Promise.all([ | |
5e10e8d7 FA |
40 | getVideoFileBitrate(currentFile), |
41 | getVideoFileFPS(currentFile), | |
42 | getVideoFileResolution(currentFile) | |
9f1ddd24 C |
43 | ]) |
44 | ||
5e10e8d7 FA |
45 | const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) |
46 | const isMaxBitrateExceeded = videoBitrate > maxBitrate | |
9f1ddd24 | 47 | if (isMaxBitrateExceeded) { |
5b77537c C |
48 | console.log( |
49 | 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)', | |
50 | basename(currentFile), videoBitrate / 1000, maxBitrate / 1000 | |
51 | ) | |
52 | ||
5e10e8d7 FA |
53 | const backupFile = `${currentFile}_backup` |
54 | await copy(currentFile, backupFile) | |
5b77537c | 55 | |
9f1ddd24 | 56 | await optimizeVideofile(video, file) |
5b77537c | 57 | |
5e10e8d7 FA |
58 | const originalDuration = await getDurationFromVideoFile(backupFile) |
59 | const newDuration = await getDurationFromVideoFile(currentFile) | |
5b77537c | 60 | |
5e10e8d7 FA |
61 | if (originalDuration === newDuration) { |
62 | console.log('Finished optimizing %s', basename(currentFile)) | |
63 | await remove(backupFile) | |
51326912 | 64 | continue |
5e10e8d7 | 65 | } |
5b77537c C |
66 | |
67 | console.log('Failed to optimize %s, restoring original', basename(currentFile)) | |
68 | await move(backupFile, currentFile, { overwrite: true }) | |
69 | await video.createTorrentAndSetInfoHash(file) | |
70 | await file.save() | |
9f1ddd24 | 71 | } |
edb4ffc7 FA |
72 | } |
73 | } | |
9f1ddd24 | 74 | |
edb4ffc7 FA |
75 | console.log('Finished optimizing videos') |
76 | } |