create-transcoding-job.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { registerTSPaths } from '../server/helpers/register-ts-paths'
  2. registerTSPaths()
  3. import * as program from 'commander'
  4. import { VideoModel } from '../server/models/video/video'
  5. import { initDatabaseModels } from '../server/initializers'
  6. import { JobQueue } from '../server/lib/job-queue'
  7. import { VideoTranscodingPayload } from '../server/lib/job-queue/handlers/video-transcoding'
  8. import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg-utils'
  9. program
  10. .option('-v, --video [videoUUID]', 'Video UUID')
  11. .option('-r, --resolution [resolution]', 'Video resolution (integer)')
  12. .option('--generate-hls', 'Generate HLS playlist')
  13. .parse(process.argv)
  14. if (program['video'] === undefined) {
  15. console.error('All parameters are mandatory.')
  16. process.exit(-1)
  17. }
  18. if (program.resolution !== undefined && Number.isNaN(+program.resolution)) {
  19. console.error('The resolution must be an integer (example: 1080).')
  20. process.exit(-1)
  21. }
  22. run()
  23. .then(() => process.exit(0))
  24. .catch(err => {
  25. console.error(err)
  26. process.exit(-1)
  27. })
  28. async function run () {
  29. await initDatabaseModels(true)
  30. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(program['video'])
  31. if (!video) throw new Error('Video not found.')
  32. const dataInput: VideoTranscodingPayload[] = []
  33. const { videoFileResolution } = await video.getMaxQualityResolution()
  34. if (program.generateHls) {
  35. const resolutionsEnabled = program.resolution
  36. ? [ program.resolution ]
  37. : computeResolutionsToTranscode(videoFileResolution).concat([ videoFileResolution ])
  38. for (const resolution of resolutionsEnabled) {
  39. dataInput.push({
  40. type: 'hls',
  41. videoUUID: video.uuid,
  42. resolution,
  43. isPortraitMode: false,
  44. copyCodecs: false
  45. })
  46. }
  47. } else if (program.resolution !== undefined) {
  48. dataInput.push({
  49. type: 'new-resolution' as 'new-resolution',
  50. videoUUID: video.uuid,
  51. isNewVideo: false,
  52. resolution: program.resolution
  53. })
  54. } else {
  55. dataInput.push({
  56. type: 'optimize' as 'optimize',
  57. videoUUID: video.uuid,
  58. isNewVideo: false
  59. })
  60. }
  61. await JobQueue.Instance.init()
  62. for (const d of dataInput) {
  63. await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: d })
  64. console.log('Transcoding job for video %s created.', video.uuid)
  65. }
  66. }