2019-10-21 17:13:07 +02:00
|
|
|
import { registerTSPaths } from '../server/helpers/register-ts-paths'
|
|
|
|
registerTSPaths()
|
|
|
|
|
2019-04-11 14:26:41 +02:00
|
|
|
import { VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
|
|
|
|
import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils'
|
2018-10-08 16:26:04 +02:00
|
|
|
import { getMaxBitrate } from '../shared/models/videos'
|
|
|
|
import { VideoModel } from '../server/models/video/video'
|
|
|
|
import { optimizeVideofile } from '../server/lib/video-transcoding'
|
2018-10-18 16:53:52 +02:00
|
|
|
import { initDatabaseModels } from '../server/initializers'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { basename, dirname, join } from 'path'
|
|
|
|
import { copy, move, remove } from 'fs-extra'
|
|
|
|
import { CONFIG } from '../server/initializers/config'
|
2018-10-08 16:26:04 +02:00
|
|
|
|
|
|
|
run()
|
|
|
|
.then(() => process.exit(0))
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|
|
|
|
|
2018-10-23 09:25:09 +02:00
|
|
|
let currentVideoId = null
|
|
|
|
let currentFile = null
|
|
|
|
|
|
|
|
process.on('SIGINT', async function () {
|
|
|
|
console.log('Cleaning up temp files')
|
|
|
|
await remove(`${currentFile}_backup`)
|
|
|
|
await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`)
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
|
2018-10-08 16:26:04 +02:00
|
|
|
async function run () {
|
2018-10-18 16:53:52 +02:00
|
|
|
await initDatabaseModels(true)
|
|
|
|
|
2018-10-08 16:50:56 +02:00
|
|
|
const localVideos = await VideoModel.listLocal()
|
2018-10-08 16:26:04 +02:00
|
|
|
|
2018-10-08 16:50:56 +02:00
|
|
|
for (const video of localVideos) {
|
2018-10-23 09:25:09 +02:00
|
|
|
currentVideoId = video.id
|
2019-07-23 12:04:15 +02:00
|
|
|
|
2018-10-08 16:50:56 +02:00
|
|
|
for (const file of video.VideoFiles) {
|
2018-10-23 09:25:09 +02:00
|
|
|
currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
|
2018-10-08 16:50:56 +02:00
|
|
|
|
|
|
|
const [ videoBitrate, fps, resolution ] = await Promise.all([
|
2018-10-23 09:25:09 +02:00
|
|
|
getVideoFileBitrate(currentFile),
|
|
|
|
getVideoFileFPS(currentFile),
|
|
|
|
getVideoFileResolution(currentFile)
|
2018-10-08 16:50:56 +02:00
|
|
|
])
|
|
|
|
|
2018-10-23 09:25:09 +02:00
|
|
|
const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
|
|
|
|
const isMaxBitrateExceeded = videoBitrate > maxBitrate
|
2018-10-08 16:50:56 +02:00
|
|
|
if (isMaxBitrateExceeded) {
|
2019-07-23 12:04:15 +02:00
|
|
|
console.log(
|
|
|
|
'Optimizing video file %s with bitrate %s kbps (max: %s kbps)',
|
|
|
|
basename(currentFile), videoBitrate / 1000, maxBitrate / 1000
|
|
|
|
)
|
|
|
|
|
2018-10-23 09:25:09 +02:00
|
|
|
const backupFile = `${currentFile}_backup`
|
|
|
|
await copy(currentFile, backupFile)
|
2019-07-23 12:04:15 +02:00
|
|
|
|
2018-10-08 16:50:56 +02:00
|
|
|
await optimizeVideofile(video, file)
|
2019-07-23 12:04:15 +02:00
|
|
|
|
2018-10-23 09:25:09 +02:00
|
|
|
const originalDuration = await getDurationFromVideoFile(backupFile)
|
|
|
|
const newDuration = await getDurationFromVideoFile(currentFile)
|
2019-07-23 12:04:15 +02:00
|
|
|
|
2018-10-23 09:25:09 +02:00
|
|
|
if (originalDuration === newDuration) {
|
|
|
|
console.log('Finished optimizing %s', basename(currentFile))
|
|
|
|
await remove(backupFile)
|
2019-07-24 09:34:06 +02:00
|
|
|
continue
|
2018-10-23 09:25:09 +02:00
|
|
|
}
|
2019-07-23 12:04:15 +02:00
|
|
|
|
|
|
|
console.log('Failed to optimize %s, restoring original', basename(currentFile))
|
|
|
|
await move(backupFile, currentFile, { overwrite: true })
|
|
|
|
await video.createTorrentAndSetInfoHash(file)
|
|
|
|
await file.save()
|
2018-10-08 16:50:56 +02:00
|
|
|
}
|
2018-10-08 16:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 16:50:56 +02:00
|
|
|
|
2018-10-08 16:26:04 +02:00
|
|
|
console.log('Finished optimizing videos')
|
|
|
|
}
|