]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-file-import.ts
Improve target bitrate calculation
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file-import.ts
CommitLineData
30842128 1import * as Bull from 'bull'
30842128 2import { copy, stat } from 'fs-extra'
ea54cd04 3import { getLowercaseExtension } from '@server/helpers/core-utils'
d7a25329 4import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
83903cb6 5import { generateWebTorrentVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
7d9ba5c0 6import { UserModel } from '@server/models/user/user'
2451916e 7import { MVideoFullLight } from '@server/types/models'
8dc8a34e 8import { VideoFileImportPayload } from '@shared/models'
daf6e480
C
9import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils'
10import { logger } from '../../../helpers/logger'
11import { VideoModel } from '../../../models/video/video'
12import { VideoFileModel } from '../../../models/video/video-file'
24516aa2 13import { onNewWebTorrentFileResolution } from './video-transcoding'
30842128
C
14
15async function processVideoFileImport (job: Bull.Job) {
16 const payload = job.data as VideoFileImportPayload
17 logger.info('Processing video file import in job %d.', job.id)
18
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
69eddafb
C
26 const data = await getVideoFileResolution(payload.filePath)
27
30842128
C
28 await updateVideoFile(video, payload.filePath)
29
77d7e851 30 const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
69eddafb
C
31
32 const newResolutionPayload = {
33 type: 'new-resolution-to-webtorrent' as 'new-resolution-to-webtorrent',
34 videoUUID: video.uuid,
679c12e6 35 resolution: data.resolution,
69eddafb
C
36 isPortraitMode: data.isPortraitMode,
37 copyCodecs: false,
38 isNewVideo: false
39 }
40 await onNewWebTorrentFileResolution(video, user, newResolutionPayload)
77d7e851 41
30842128
C
42 return video
43}
44
45// ---------------------------------------------------------------------------
46
47export {
48 processVideoFileImport
49}
50
51// ---------------------------------------------------------------------------
52
90a8bd30 53async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
679c12e6 54 const { resolution } = await getVideoFileResolution(inputFilePath)
30842128
C
55 const { size } = await stat(inputFilePath)
56 const fps = await getVideoFileFPS(inputFilePath)
57
ea54cd04 58 const fileExt = getLowercaseExtension(inputFilePath)
30842128 59
679c12e6 60 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === resolution)
30842128
C
61
62 if (currentVideoFile) {
63 // Remove old file and old torrent
764b1a14 64 await video.removeFileAndTorrent(currentVideoFile)
30842128
C
65 // Remove the old video file from the array
66 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
67
2451916e 68 await currentVideoFile.destroy()
30842128
C
69 }
70
2451916e 71 const newVideoFile = new VideoFileModel({
679c12e6 72 resolution,
2451916e 73 extname: fileExt,
679c12e6 74 filename: generateWebTorrentVideoFilename(resolution, fileExt),
2451916e
C
75 size,
76 fps,
77 videoId: video.id
78 })
30842128 79
2451916e
C
80 const outputPath = getVideoFilePath(video, newVideoFile)
81 await copy(inputFilePath, outputPath)
30842128 82
2451916e 83 video.VideoFiles.push(newVideoFile)
8efc27bf 84 await createTorrentAndSetInfoHash(video, newVideoFile)
30842128 85
2451916e 86 await newVideoFile.save()
30842128 87}