create-transcoding-job.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { registerTSPaths } from '../server/helpers/register-ts-paths'
  2. registerTSPaths()
  3. import { program } from 'commander'
  4. import { VideoModel } from '../server/models/video/video'
  5. import { initDatabaseModels } from '../server/initializers/database'
  6. import { JobQueue } from '../server/lib/job-queue'
  7. import { computeResolutionsToTranscode } from '@server/helpers/ffprobe-utils'
  8. import { VideoState, VideoTranscodingPayload } from '@shared/models'
  9. import { CONFIG } from '@server/initializers/config'
  10. import { isUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc'
  11. import { addTranscodingJob } from '@server/lib/video'
  12. program
  13. .option('-v, --video [videoUUID]', 'Video UUID')
  14. .option('-r, --resolution [resolution]', 'Video resolution (integer)')
  15. .option('--generate-hls', 'Generate HLS playlist')
  16. .parse(process.argv)
  17. const options = program.opts()
  18. if (options.video === undefined) {
  19. console.error('All parameters are mandatory.')
  20. process.exit(-1)
  21. }
  22. if (options.resolution !== undefined && Number.isNaN(+options.resolution)) {
  23. console.error('The resolution must be an integer (example: 1080).')
  24. process.exit(-1)
  25. }
  26. run()
  27. .then(() => process.exit(0))
  28. .catch(err => {
  29. console.error(err)
  30. process.exit(-1)
  31. })
  32. async function run () {
  33. await initDatabaseModels(true)
  34. const uuid = toCompleteUUID(options.video)
  35. if (isUUIDValid(uuid) === false) {
  36. console.error('%s is not a valid video UUID.', options.video)
  37. return
  38. }
  39. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(uuid)
  40. if (!video) throw new Error('Video not found.')
  41. const dataInput: VideoTranscodingPayload[] = []
  42. const resolution = video.getMaxQualityFile().resolution
  43. // Generate HLS files
  44. if (options.generateHls || CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) {
  45. const resolutionsEnabled = options.resolution
  46. ? [ options.resolution ]
  47. : computeResolutionsToTranscode(resolution, 'vod').concat([ resolution ])
  48. for (const resolution of resolutionsEnabled) {
  49. dataInput.push({
  50. type: 'new-resolution-to-hls',
  51. videoUUID: video.uuid,
  52. resolution,
  53. isPortraitMode: false,
  54. copyCodecs: false,
  55. isNewVideo: false,
  56. isMaxQuality: false
  57. })
  58. }
  59. } else {
  60. if (options.resolution !== undefined) {
  61. dataInput.push({
  62. type: 'new-resolution-to-webtorrent',
  63. videoUUID: video.uuid,
  64. isNewVideo: false,
  65. resolution: options.resolution
  66. })
  67. } else {
  68. if (video.VideoFiles.length === 0) {
  69. console.error('Cannot regenerate webtorrent files with a HLS only video.')
  70. return
  71. }
  72. dataInput.push({
  73. type: 'optimize-to-webtorrent',
  74. videoUUID: video.uuid,
  75. isNewVideo: false
  76. })
  77. }
  78. }
  79. JobQueue.Instance.init()
  80. video.state = VideoState.TO_TRANSCODE
  81. await video.save()
  82. for (const d of dataInput) {
  83. await addTranscodingJob(d, {})
  84. console.log('Transcoding job for video %s created.', video.uuid)
  85. }
  86. }