aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/optimize-old-videos.ts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/optimize-old-videos.ts')
-rw-r--r--scripts/optimize-old-videos.ts32
1 files changed, 17 insertions, 15 deletions
diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts
index 9692d76ba..bde9d1e01 100644
--- a/scripts/optimize-old-videos.ts
+++ b/scripts/optimize-old-videos.ts
@@ -19,13 +19,13 @@ run()
19 process.exit(-1) 19 process.exit(-1)
20 }) 20 })
21 21
22let currentVideoId = null 22let currentVideoId: string
23let currentFile = null 23let currentFilePath: string
24 24
25process.on('SIGINT', async function () { 25process.on('SIGINT', async function () {
26 console.log('Cleaning up temp files') 26 console.log('Cleaning up temp files')
27 await remove(`${currentFile}_backup`) 27 await remove(`${currentFilePath}_backup`)
28 await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`) 28 await remove(`${dirname(currentFilePath)}/${currentVideoId}-transcoded.mp4`)
29 process.exit(0) 29 process.exit(0)
30}) 30})
31 31
@@ -40,12 +40,12 @@ async function run () {
40 currentVideoId = video.id 40 currentVideoId = video.id
41 41
42 for (const file of video.VideoFiles) { 42 for (const file of video.VideoFiles) {
43 currentFile = getVideoFilePath(video, file) 43 currentFilePath = getVideoFilePath(video, file)
44 44
45 const [ videoBitrate, fps, resolution ] = await Promise.all([ 45 const [ videoBitrate, fps, resolution ] = await Promise.all([
46 getVideoFileBitrate(currentFile), 46 getVideoFileBitrate(currentFilePath),
47 getVideoFileFPS(currentFile), 47 getVideoFileFPS(currentFilePath),
48 getVideoFileResolution(currentFile) 48 getVideoFileResolution(currentFilePath)
49 ]) 49 ])
50 50
51 const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) 51 const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
@@ -53,25 +53,27 @@ async function run () {
53 if (isMaxBitrateExceeded) { 53 if (isMaxBitrateExceeded) {
54 console.log( 54 console.log(
55 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)', 55 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)',
56 basename(currentFile), videoBitrate / 1000, maxBitrate / 1000 56 basename(currentFilePath), videoBitrate / 1000, maxBitrate / 1000
57 ) 57 )
58 58
59 const backupFile = `${currentFile}_backup` 59 const backupFile = `${currentFilePath}_backup`
60 await copy(currentFile, backupFile) 60 await copy(currentFilePath, backupFile)
61 61
62 await optimizeOriginalVideofile(video, file) 62 await optimizeOriginalVideofile(video, file)
63 // Update file path, the video filename changed
64 currentFilePath = getVideoFilePath(video, file)
63 65
64 const originalDuration = await getDurationFromVideoFile(backupFile) 66 const originalDuration = await getDurationFromVideoFile(backupFile)
65 const newDuration = await getDurationFromVideoFile(currentFile) 67 const newDuration = await getDurationFromVideoFile(currentFilePath)
66 68
67 if (originalDuration === newDuration) { 69 if (originalDuration === newDuration) {
68 console.log('Finished optimizing %s', basename(currentFile)) 70 console.log('Finished optimizing %s', basename(currentFilePath))
69 await remove(backupFile) 71 await remove(backupFile)
70 continue 72 continue
71 } 73 }
72 74
73 console.log('Failed to optimize %s, restoring original', basename(currentFile)) 75 console.log('Failed to optimize %s, restoring original', basename(currentFilePath))
74 await move(backupFile, currentFile, { overwrite: true }) 76 await move(backupFile, currentFilePath, { overwrite: true })
75 await createTorrentAndSetInfoHash(video, file) 77 await createTorrentAndSetInfoHash(video, file)
76 await file.save() 78 await file.save()
77 } 79 }