]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-transcoding.ts
04cadf74bd26b54090132e75901790f4ec4f96c6
[github/Chocobozzz/PeerTube.git] / server / lib / video-transcoding.ts
1 import { CONFIG } from '../initializers'
2 import { join, extname, basename } from 'path'
3 import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils'
4 import { copy, remove, rename, stat } from 'fs-extra'
5 import { logger } from '../helpers/logger'
6 import { VideoResolution } from '../../shared/models/videos'
7 import { VideoFileModel } from '../models/video/video-file'
8 import { VideoModel } from '../models/video/video'
9
10 async function optimizeVideofile (video: VideoModel, videoInputPath?: string) {
11 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
12 const newExtname = '.mp4'
13 let inputVideoFile = null
14 if (videoInputPath == null) {
15 inputVideoFile = video.getOriginalFile()
16 videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
17 } else {
18 inputVideoFile = basename(videoInputPath)
19 }
20 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
21
22 const transcodeOptions = {
23 inputPath: videoInputPath,
24 outputPath: videoTranscodedPath
25 }
26
27 // Could be very long!
28 await transcode(transcodeOptions)
29
30 try {
31 await remove(videoInputPath)
32
33 // Important to do this before getVideoFilename() to take in account the new file extension
34 inputVideoFile.set('extname', newExtname)
35
36 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
37 await rename(videoTranscodedPath, videoOutputPath)
38 const stats = await stat(videoOutputPath)
39 const fps = await getVideoFileFPS(videoOutputPath)
40
41 inputVideoFile.set('size', stats.size)
42 inputVideoFile.set('fps', fps)
43
44 await video.createTorrentAndSetInfoHash(inputVideoFile)
45 await inputVideoFile.save()
46 } catch (err) {
47 // Auto destruction...
48 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
49
50 throw err
51 }
52 }
53
54 async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
55 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
56 const extname = '.mp4'
57
58 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
59 const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile()))
60
61 const newVideoFile = new VideoFileModel({
62 resolution,
63 extname,
64 size: 0,
65 videoId: video.id
66 })
67 const videoOutputPath = join(videosDirectory, video.getVideoFilename(newVideoFile))
68
69 const transcodeOptions = {
70 inputPath: videoInputPath,
71 outputPath: videoOutputPath,
72 resolution,
73 isPortraitMode
74 }
75
76 await transcode(transcodeOptions)
77
78 const stats = await stat(videoOutputPath)
79 const fps = await getVideoFileFPS(videoOutputPath)
80
81 newVideoFile.set('size', stats.size)
82 newVideoFile.set('fps', fps)
83
84 await video.createTorrentAndSetInfoHash(newVideoFile)
85
86 await newVideoFile.save()
87
88 video.VideoFiles.push(newVideoFile)
89 }
90
91 async function importVideoFile (video: VideoModel, inputFilePath: string) {
92 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
93 const { size } = await stat(inputFilePath)
94 const fps = await getVideoFileFPS(inputFilePath)
95
96 let updatedVideoFile = new VideoFileModel({
97 resolution: videoFileResolution,
98 extname: extname(inputFilePath),
99 size,
100 fps,
101 videoId: video.id
102 })
103
104 const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution)
105
106 if (currentVideoFile) {
107 // Remove old file and old torrent
108 await video.removeFile(currentVideoFile)
109 await video.removeTorrent(currentVideoFile)
110 // Remove the old video file from the array
111 video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile)
112
113 // Update the database
114 currentVideoFile.set('extname', updatedVideoFile.extname)
115 currentVideoFile.set('size', updatedVideoFile.size)
116 currentVideoFile.set('fps', updatedVideoFile.fps)
117
118 updatedVideoFile = currentVideoFile
119 }
120
121 const outputPath = video.getVideoFilePath(updatedVideoFile)
122 await copy(inputFilePath, outputPath)
123
124 await video.createTorrentAndSetInfoHash(updatedVideoFile)
125
126 await updatedVideoFile.save()
127
128 video.VideoFiles.push(updatedVideoFile)
129 }
130
131 export {
132 optimizeVideofile,
133 transcodeOriginalVideofile,
134 importVideoFile
135 }