mirror of https://github.com/Chocobozzz/PeerTube
Make backups of files in optimize-old-videos.ts (#1304)
parent
eff7cdd7b7
commit
5e10e8d73a
|
@ -1,10 +1,11 @@
|
|||
import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
|
||||
import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils'
|
||||
import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution, getDurationFromVideoFile } from '../server/helpers/ffmpeg-utils'
|
||||
import { getMaxBitrate } from '../shared/models/videos'
|
||||
import { VideoModel } from '../server/models/video/video'
|
||||
import { optimizeVideofile } from '../server/lib/video-transcoding'
|
||||
import { initDatabaseModels } from '../server/initializers'
|
||||
import { join } from 'path'
|
||||
import { join, basename, dirname } from 'path'
|
||||
import { copy, remove, move } from 'fs-extra'
|
||||
|
||||
run()
|
||||
.then(() => process.exit(0))
|
||||
|
@ -13,24 +14,51 @@ run()
|
|||
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 () {
|
||||
await initDatabaseModels(true)
|
||||
|
||||
const localVideos = await VideoModel.listLocal()
|
||||
|
||||
for (const video of localVideos) {
|
||||
currentVideoId = video.id
|
||||
for (const file of video.VideoFiles) {
|
||||
const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
|
||||
currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
|
||||
|
||||
const [ videoBitrate, fps, resolution ] = await Promise.all([
|
||||
getVideoFileBitrate(inputPath),
|
||||
getVideoFileFPS(inputPath),
|
||||
getVideoFileResolution(inputPath)
|
||||
getVideoFileBitrate(currentFile),
|
||||
getVideoFileFPS(currentFile),
|
||||
getVideoFileResolution(currentFile)
|
||||
])
|
||||
|
||||
const isMaxBitrateExceeded = videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
|
||||
const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
|
||||
const isMaxBitrateExceeded = videoBitrate > maxBitrate
|
||||
if (isMaxBitrateExceeded) {
|
||||
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)
|
||||
await optimizeVideofile(video, file)
|
||||
const originalDuration = await getDurationFromVideoFile(backupFile)
|
||||
const newDuration = await getDurationFromVideoFile(currentFile)
|
||||
if (originalDuration === newDuration) {
|
||||
console.log('Finished optimizing %s', basename(currentFile))
|
||||
await remove(backupFile)
|
||||
} else {
|
||||
console.log('Failed to optimize %s, restoring original', basename(currentFile))
|
||||
move(backupFile, currentFile, { overwrite: true })
|
||||
await video.createTorrentAndSetInfoHash(file)
|
||||
await file.save()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue