2019-10-21 10:13:07 -05:00
|
|
|
import { registerTSPaths } from '../server/helpers/register-ts-paths'
|
2019-11-21 05:16:27 -06:00
|
|
|
registerTSPaths()
|
|
|
|
|
2019-04-11 07:26:41 -05:00
|
|
|
import { VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
|
2020-11-20 10:16:55 -06:00
|
|
|
import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffprobe-utils'
|
2018-10-08 09:26:04 -05:00
|
|
|
import { getMaxBitrate } from '../shared/models/videos'
|
|
|
|
import { VideoModel } from '../server/models/video/video'
|
2019-11-15 08:06:03 -06:00
|
|
|
import { optimizeOriginalVideofile } from '../server/lib/video-transcoding'
|
2020-05-07 07:58:24 -05:00
|
|
|
import { initDatabaseModels } from '../server/initializers/database'
|
2019-11-15 08:06:03 -06:00
|
|
|
import { basename, dirname } from 'path'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { copy, move, remove } from 'fs-extra'
|
2019-11-15 08:06:03 -06:00
|
|
|
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
|
|
|
|
import { getVideoFilePath } from '@server/lib/video-paths'
|
|
|
|
|
2018-10-08 09:26:04 -05:00
|
|
|
run()
|
|
|
|
.then(() => process.exit(0))
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|
|
|
|
|
2018-10-23 02:25:09 -05: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 09:26:04 -05:00
|
|
|
async function run () {
|
2018-10-18 09:53:52 -05:00
|
|
|
await initDatabaseModels(true)
|
|
|
|
|
2018-10-08 09:50:56 -05:00
|
|
|
const localVideos = await VideoModel.listLocal()
|
2018-10-08 09:26:04 -05:00
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
for (const localVideo of localVideos) {
|
|
|
|
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(localVideo.id)
|
|
|
|
|
2018-10-23 02:25:09 -05:00
|
|
|
currentVideoId = video.id
|
2019-07-23 05:04:15 -05:00
|
|
|
|
2018-10-08 09:50:56 -05:00
|
|
|
for (const file of video.VideoFiles) {
|
2019-11-15 08:06:03 -06:00
|
|
|
currentFile = getVideoFilePath(video, file)
|
2018-10-08 09:50:56 -05:00
|
|
|
|
|
|
|
const [ videoBitrate, fps, resolution ] = await Promise.all([
|
2018-10-23 02:25:09 -05:00
|
|
|
getVideoFileBitrate(currentFile),
|
|
|
|
getVideoFileFPS(currentFile),
|
|
|
|
getVideoFileResolution(currentFile)
|
2018-10-08 09:50:56 -05:00
|
|
|
])
|
|
|
|
|
2018-10-23 02:25:09 -05:00
|
|
|
const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
|
|
|
|
const isMaxBitrateExceeded = videoBitrate > maxBitrate
|
2018-10-08 09:50:56 -05:00
|
|
|
if (isMaxBitrateExceeded) {
|
2019-07-23 05:04:15 -05:00
|
|
|
console.log(
|
|
|
|
'Optimizing video file %s with bitrate %s kbps (max: %s kbps)',
|
|
|
|
basename(currentFile), videoBitrate / 1000, maxBitrate / 1000
|
|
|
|
)
|
|
|
|
|
2018-10-23 02:25:09 -05:00
|
|
|
const backupFile = `${currentFile}_backup`
|
|
|
|
await copy(currentFile, backupFile)
|
2019-07-23 05:04:15 -05:00
|
|
|
|
2019-11-15 08:06:03 -06:00
|
|
|
await optimizeOriginalVideofile(video, file)
|
2019-07-23 05:04:15 -05:00
|
|
|
|
2018-10-23 02:25:09 -05:00
|
|
|
const originalDuration = await getDurationFromVideoFile(backupFile)
|
|
|
|
const newDuration = await getDurationFromVideoFile(currentFile)
|
2019-07-23 05:04:15 -05:00
|
|
|
|
2018-10-23 02:25:09 -05:00
|
|
|
if (originalDuration === newDuration) {
|
|
|
|
console.log('Finished optimizing %s', basename(currentFile))
|
|
|
|
await remove(backupFile)
|
2019-07-24 02:34:06 -05:00
|
|
|
continue
|
2018-10-23 02:25:09 -05:00
|
|
|
}
|
2019-07-23 05:04:15 -05:00
|
|
|
|
|
|
|
console.log('Failed to optimize %s, restoring original', basename(currentFile))
|
|
|
|
await move(backupFile, currentFile, { overwrite: true })
|
2021-02-18 04:28:00 -06:00
|
|
|
await createTorrentAndSetInfoHash(video, file)
|
2019-07-23 05:04:15 -05:00
|
|
|
await file.save()
|
2018-10-08 09:50:56 -05:00
|
|
|
}
|
2018-10-08 09:26:04 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 09:50:56 -05:00
|
|
|
|
2018-10-08 09:26:04 -05:00
|
|
|
console.log('Finished optimizing videos')
|
|
|
|
}
|