aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/optimize-old-videos.ts
diff options
context:
space:
mode:
authorFelix Ableitner <me@nutomic.com>2018-10-23 02:25:09 -0500
committerRigel Kent <par@rigelk.eu>2018-10-23 09:25:09 +0200
commit5e10e8d73a19ed38aeb26d7e57b96d5843e7136d (patch)
tree009604b854d4be7a411ceacbad3fc27e0c4c997b /scripts/optimize-old-videos.ts
parenteff7cdd7b7c3667ac17312d49104488aaf698ba2 (diff)
downloadPeerTube-5e10e8d73a19ed38aeb26d7e57b96d5843e7136d.tar.gz
PeerTube-5e10e8d73a19ed38aeb26d7e57b96d5843e7136d.tar.zst
PeerTube-5e10e8d73a19ed38aeb26d7e57b96d5843e7136d.zip
Make backups of files in optimize-old-videos.ts (#1304)
Diffstat (limited to 'scripts/optimize-old-videos.ts')
-rw-r--r--scripts/optimize-old-videos.ts42
1 files changed, 35 insertions, 7 deletions
diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts
index c93f82316..1bee1b0f3 100644
--- a/scripts/optimize-old-videos.ts
+++ b/scripts/optimize-old-videos.ts
@@ -1,10 +1,11 @@
1import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants' 1import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
2import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils' 2import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution, getDurationFromVideoFile } from '../server/helpers/ffmpeg-utils'
3import { getMaxBitrate } from '../shared/models/videos' 3import { getMaxBitrate } from '../shared/models/videos'
4import { VideoModel } from '../server/models/video/video' 4import { VideoModel } from '../server/models/video/video'
5import { optimizeVideofile } from '../server/lib/video-transcoding' 5import { optimizeVideofile } from '../server/lib/video-transcoding'
6import { initDatabaseModels } from '../server/initializers' 6import { initDatabaseModels } from '../server/initializers'
7import { join } from 'path' 7import { join, basename, dirname } from 'path'
8import { copy, remove, move } from 'fs-extra'
8 9
9run() 10run()
10 .then(() => process.exit(0)) 11 .then(() => process.exit(0))
@@ -13,24 +14,51 @@ run()
13 process.exit(-1) 14 process.exit(-1)
14 }) 15 })
15 16
17let currentVideoId = null
18let currentFile = null
19
20process.on('SIGINT', async function () {
21 console.log('Cleaning up temp files')
22 await remove(`${currentFile}_backup`)
23 await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`)
24 process.exit(0)
25})
26
16async function run () { 27async function run () {
17 await initDatabaseModels(true) 28 await initDatabaseModels(true)
18 29
19 const localVideos = await VideoModel.listLocal() 30 const localVideos = await VideoModel.listLocal()
20 31
21 for (const video of localVideos) { 32 for (const video of localVideos) {
33 currentVideoId = video.id
22 for (const file of video.VideoFiles) { 34 for (const file of video.VideoFiles) {
23 const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) 35 currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
24 36
25 const [ videoBitrate, fps, resolution ] = await Promise.all([ 37 const [ videoBitrate, fps, resolution ] = await Promise.all([
26 getVideoFileBitrate(inputPath), 38 getVideoFileBitrate(currentFile),
27 getVideoFileFPS(inputPath), 39 getVideoFileFPS(currentFile),
28 getVideoFileResolution(inputPath) 40 getVideoFileResolution(currentFile)
29 ]) 41 ])
30 42
31 const isMaxBitrateExceeded = videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) 43 const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
44 const isMaxBitrateExceeded = videoBitrate > maxBitrate
32 if (isMaxBitrateExceeded) { 45 if (isMaxBitrateExceeded) {
46 console.log('Optimizing video file %s with bitrate %s kbps (max: %s kbps)',
47 basename(currentFile), videoBitrate / 1000, maxBitrate / 1000)
48 const backupFile = `${currentFile}_backup`
49 await copy(currentFile, backupFile)
33 await optimizeVideofile(video, file) 50 await optimizeVideofile(video, file)
51 const originalDuration = await getDurationFromVideoFile(backupFile)
52 const newDuration = await getDurationFromVideoFile(currentFile)
53 if (originalDuration === newDuration) {
54 console.log('Finished optimizing %s', basename(currentFile))
55 await remove(backupFile)
56 } else {
57 console.log('Failed to optimize %s, restoring original', basename(currentFile))
58 move(backupFile, currentFile, { overwrite: true })
59 await video.createTorrentAndSetInfoHash(file)
60 await file.save()
61 }
34 } 62 }
35 } 63 }
36 } 64 }