PeerTube/scripts/optimize-old-videos.ts

84 lines
2.9 KiB
TypeScript
Raw Normal View History

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