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