PeerTube/scripts/optimize-old-videos.ts

83 lines
2.8 KiB
TypeScript
Raw Normal View History

import { registerTSPaths } from '../server/helpers/register-ts-paths'
2019-11-21 12:16:27 +01:00
registerTSPaths()
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'
import { getMaxBitrate } from '../shared/models/videos'
import { VideoModel } from '../server/models/video/video'
import { optimizeOriginalVideofile } from '../server/lib/transcoding/video-transcoding'
2020-05-07 14:58:24 +02: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'
run()
.then(() => process.exit(0))
.catch(err => {
console.error(err)
process.exit(-1)
})
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)
})
async function run () {
2018-10-18 16:53:52 +02: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 12:04:15 +02:00
for (const file of video.VideoFiles) {
currentFile = getVideoFilePath(video, file)
const [ videoBitrate, fps, resolution ] = await Promise.all([
getVideoFileBitrate(currentFile),
getVideoFileFPS(currentFile),
getVideoFileResolution(currentFile)
])
const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
const isMaxBitrateExceeded = videoBitrate > maxBitrate
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
)
const backupFile = `${currentFile}_backup`
await copy(currentFile, backupFile)
2019-07-23 12:04:15 +02:00
await optimizeOriginalVideofile(video, file)
2019-07-23 12:04:15 +02:00
const originalDuration = await getDurationFromVideoFile(backupFile)
const newDuration = await getDurationFromVideoFile(currentFile)
2019-07-23 12:04:15 +02:00
if (originalDuration === newDuration) {
console.log('Finished optimizing %s', basename(currentFile))
await remove(backupFile)
2019-07-24 09:34:06 +02:00
continue
}
2019-07-23 12:04:15 +02:00
console.log('Failed to optimize %s, restoring original', basename(currentFile))
await move(backupFile, currentFile, { 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()
}
}
}
console.log('Finished optimizing videos')
}