optimize-old-videos.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { registerTSPaths } from '../server/helpers/register-ts-paths'
  2. registerTSPaths()
  3. import { VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
  4. import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils'
  5. import { getMaxBitrate } from '../shared/models/videos'
  6. import { VideoModel } from '../server/models/video/video'
  7. import { optimizeOriginalVideofile } from '../server/lib/video-transcoding'
  8. import { initDatabaseModels } from '../server/initializers'
  9. import { basename, dirname } from 'path'
  10. import { copy, move, remove } from 'fs-extra'
  11. import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
  12. import { getVideoFilePath } from '@server/lib/video-paths'
  13. run()
  14. .then(() => process.exit(0))
  15. .catch(err => {
  16. console.error(err)
  17. process.exit(-1)
  18. })
  19. let currentVideoId = null
  20. let currentFile = null
  21. process.on('SIGINT', async function () {
  22. console.log('Cleaning up temp files')
  23. await remove(`${currentFile}_backup`)
  24. await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`)
  25. process.exit(0)
  26. })
  27. async function run () {
  28. await initDatabaseModels(true)
  29. const localVideos = await VideoModel.listLocal()
  30. for (const video of localVideos) {
  31. currentVideoId = video.id
  32. for (const file of video.VideoFiles) {
  33. currentFile = getVideoFilePath(video, file)
  34. const [ videoBitrate, fps, resolution ] = await Promise.all([
  35. getVideoFileBitrate(currentFile),
  36. getVideoFileFPS(currentFile),
  37. getVideoFileResolution(currentFile)
  38. ])
  39. const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
  40. const isMaxBitrateExceeded = videoBitrate > maxBitrate
  41. if (isMaxBitrateExceeded) {
  42. console.log(
  43. 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)',
  44. basename(currentFile), videoBitrate / 1000, maxBitrate / 1000
  45. )
  46. const backupFile = `${currentFile}_backup`
  47. await copy(currentFile, backupFile)
  48. await optimizeOriginalVideofile(video, file)
  49. const originalDuration = await getDurationFromVideoFile(backupFile)
  50. const newDuration = await getDurationFromVideoFile(currentFile)
  51. if (originalDuration === newDuration) {
  52. console.log('Finished optimizing %s', basename(currentFile))
  53. await remove(backupFile)
  54. continue
  55. }
  56. console.log('Failed to optimize %s, restoring original', basename(currentFile))
  57. await move(backupFile, currentFile, { overwrite: true })
  58. await createTorrentAndSetInfoHash(video, file)
  59. await file.save()
  60. }
  61. }
  62. }
  63. console.log('Finished optimizing videos')
  64. }