2
1

video-file-import.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import * as Bull from 'bull'
  2. import { logger } from '../../../helpers/logger'
  3. import { VideoModel } from '../../../models/video/video'
  4. import { publishNewResolutionIfNeeded } from './video-transcoding'
  5. import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
  6. import { copy, stat } from 'fs-extra'
  7. import { VideoFileModel } from '../../../models/video/video-file'
  8. import { extname } from 'path'
  9. export type VideoFileImportPayload = {
  10. videoUUID: string,
  11. filePath: string
  12. }
  13. async function processVideoFileImport (job: Bull.Job) {
  14. const payload = job.data as VideoFileImportPayload
  15. logger.info('Processing video file import in job %d.', job.id)
  16. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
  17. // No video, maybe deleted?
  18. if (!video) {
  19. logger.info('Do not process job %d, video does not exist.', job.id)
  20. return undefined
  21. }
  22. await updateVideoFile(video, payload.filePath)
  23. await publishNewResolutionIfNeeded(video)
  24. return video
  25. }
  26. // ---------------------------------------------------------------------------
  27. export {
  28. processVideoFileImport
  29. }
  30. // ---------------------------------------------------------------------------
  31. async function updateVideoFile (video: VideoModel, inputFilePath: string) {
  32. const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
  33. const { size } = await stat(inputFilePath)
  34. const fps = await getVideoFileFPS(inputFilePath)
  35. let updatedVideoFile = new VideoFileModel({
  36. resolution: videoFileResolution,
  37. extname: extname(inputFilePath),
  38. size,
  39. fps,
  40. videoId: video.id
  41. })
  42. const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
  43. if (currentVideoFile) {
  44. // Remove old file and old torrent
  45. await video.removeFile(currentVideoFile)
  46. await video.removeTorrent(currentVideoFile)
  47. // Remove the old video file from the array
  48. video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
  49. // Update the database
  50. currentVideoFile.set('extname', updatedVideoFile.extname)
  51. currentVideoFile.set('size', updatedVideoFile.size)
  52. currentVideoFile.set('fps', updatedVideoFile.fps)
  53. updatedVideoFile = currentVideoFile
  54. }
  55. const outputPath = video.getVideoFilePath(updatedVideoFile)
  56. await copy(inputFilePath, outputPath)
  57. await video.createTorrentAndSetInfoHash(updatedVideoFile)
  58. await updatedVideoFile.save()
  59. video.VideoFiles.push(updatedVideoFile)
  60. }