aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-transcoding.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/video-transcoding.ts')
-rw-r--r--server/lib/video-transcoding.ts31
1 files changed, 23 insertions, 8 deletions
diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.ts
index 0fe0ff12a..d6b6b251a 100644
--- a/server/lib/video-transcoding.ts
+++ b/server/lib/video-transcoding.ts
@@ -1,6 +1,6 @@
1import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants' 1import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
2import { join } from 'path' 2import { join } from 'path'
3import { getVideoFileFPS, transcode } from '../helpers/ffmpeg-utils' 3import { getVideoFileFPS, transcode, canDoQuickTranscode } from '../helpers/ffmpeg-utils'
4import { ensureDir, move, remove, stat } from 'fs-extra' 4import { ensureDir, move, remove, stat } from 'fs-extra'
5import { logger } from '../helpers/logger' 5import { logger } from '../helpers/logger'
6import { VideoResolution } from '../../shared/models/videos' 6import { VideoResolution } from '../../shared/models/videos'
@@ -11,18 +11,25 @@ import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-pla
11import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type' 11import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
12import { CONFIG } from '../initializers/config' 12import { CONFIG } from '../initializers/config'
13 13
14/**
15 * Optimize the original video file and replace it. The resolution is not changed.
16 */
14async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) { 17async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
15 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR 18 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
19 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
16 const newExtname = '.mp4' 20 const newExtname = '.mp4'
17 21
18 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile() 22 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
19 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile)) 23 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
20 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname) 24 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
25
26 const doQuickTranscode = await(canDoQuickTranscode(videoInputPath))
21 27
22 const transcodeOptions = { 28 const transcodeOptions = {
23 inputPath: videoInputPath, 29 inputPath: videoInputPath,
24 outputPath: videoTranscodedPath, 30 outputPath: videoTranscodedPath,
25 resolution: inputVideoFile.resolution 31 resolution: inputVideoFile.resolution,
32 doQuickTranscode
26 } 33 }
27 34
28 // Could be very long! 35 // Could be very long!
@@ -34,10 +41,11 @@ async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFi
34 // Important to do this before getVideoFilename() to take in account the new file extension 41 // Important to do this before getVideoFilename() to take in account the new file extension
35 inputVideoFile.set('extname', newExtname) 42 inputVideoFile.set('extname', newExtname)
36 43
44 const stats = await stat(videoTranscodedPath)
45 const fps = await getVideoFileFPS(videoTranscodedPath)
46
37 const videoOutputPath = video.getVideoFilePath(inputVideoFile) 47 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
38 await move(videoTranscodedPath, videoOutputPath) 48 await move(videoTranscodedPath, videoOutputPath)
39 const stats = await stat(videoOutputPath)
40 const fps = await getVideoFileFPS(videoOutputPath)
41 49
42 inputVideoFile.set('size', stats.size) 50 inputVideoFile.set('size', stats.size)
43 inputVideoFile.set('fps', fps) 51 inputVideoFile.set('fps', fps)
@@ -52,8 +60,12 @@ async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFi
52 } 60 }
53} 61}
54 62
63/**
64 * Transcode the original video file to a lower resolution.
65 */
55async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) { 66async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
56 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR 67 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
68 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
57 const extname = '.mp4' 69 const extname = '.mp4'
58 70
59 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed 71 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
@@ -66,18 +78,21 @@ async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoR
66 videoId: video.id 78 videoId: video.id
67 }) 79 })
68 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile)) 80 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
81 const videoTranscodedPath = join(transcodeDirectory, video.getVideoFilename(newVideoFile))
69 82
70 const transcodeOptions = { 83 const transcodeOptions = {
71 inputPath: videoInputPath, 84 inputPath: videoInputPath,
72 outputPath: videoOutputPath, 85 outputPath: videoTranscodedPath,
73 resolution, 86 resolution,
74 isPortraitMode: isPortrait 87 isPortraitMode: isPortrait
75 } 88 }
76 89
77 await transcode(transcodeOptions) 90 await transcode(transcodeOptions)
78 91
79 const stats = await stat(videoOutputPath) 92 const stats = await stat(videoTranscodedPath)
80 const fps = await getVideoFileFPS(videoOutputPath) 93 const fps = await getVideoFileFPS(videoTranscodedPath)
94
95 await move(videoTranscodedPath, videoOutputPath)
81 96
82 newVideoFile.set('size', stats.size) 97 newVideoFile.set('size', stats.size)
83 newVideoFile.set('fps', fps) 98 newVideoFile.set('fps', fps)