]>
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 |
9f1ddd24 | 35 | for (const file of video.VideoFiles) { |
5e10e8d7 | 36 | currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) |
9f1ddd24 C |
37 | |
38 | const [ videoBitrate, fps, resolution ] = await Promise.all([ | |
5e10e8d7 FA |
39 | getVideoFileBitrate(currentFile), |
40 | getVideoFileFPS(currentFile), | |
41 | getVideoFileResolution(currentFile) | |
9f1ddd24 C |
42 | ]) |
43 | ||
5e10e8d7 FA |
44 | const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) |
45 | const isMaxBitrateExceeded = videoBitrate > maxBitrate | |
9f1ddd24 | 46 | if (isMaxBitrateExceeded) { |
5e10e8d7 FA |
47 | console.log('Optimizing video file %s with bitrate %s kbps (max: %s kbps)', |
48 | basename(currentFile), videoBitrate / 1000, maxBitrate / 1000) | |
49 | const backupFile = `${currentFile}_backup` | |
50 | await copy(currentFile, backupFile) | |
9f1ddd24 | 51 | await optimizeVideofile(video, file) |
5e10e8d7 FA |
52 | const originalDuration = await getDurationFromVideoFile(backupFile) |
53 | const newDuration = await getDurationFromVideoFile(currentFile) | |
54 | if (originalDuration === newDuration) { | |
55 | console.log('Finished optimizing %s', basename(currentFile)) | |
56 | await remove(backupFile) | |
57 | } else { | |
58 | console.log('Failed to optimize %s, restoring original', basename(currentFile)) | |
59 | move(backupFile, currentFile, { overwrite: true }) | |
60 | await video.createTorrentAndSetInfoHash(file) | |
61 | await file.save() | |
62 | } | |
9f1ddd24 | 63 | } |
edb4ffc7 FA |
64 | } |
65 | } | |
9f1ddd24 | 66 | |
edb4ffc7 FA |
67 | console.log('Finished optimizing videos') |
68 | } |