video-file-import.ts 2.7 KB

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