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.ts63
1 files changed, 52 insertions, 11 deletions
diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.ts
index bf3ff78c2..086b860a2 100644
--- a/server/lib/video-transcoding.ts
+++ b/server/lib/video-transcoding.ts
@@ -1,22 +1,27 @@
1import { CONFIG } from '../initializers' 1import { CONFIG, HLS_PLAYLIST_DIRECTORY } from '../initializers'
2import { join, extname } from 'path' 2import { extname, join } from 'path'
3import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils' 3import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils'
4import { copy, remove, rename, stat } from 'fs-extra' 4import { copy, 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'
7import { VideoFileModel } from '../models/video/video-file' 7import { VideoFileModel } from '../models/video/video-file'
8import { VideoModel } from '../models/video/video' 8import { VideoModel } from '../models/video/video'
9import { updateMasterHLSPlaylist, updateSha256Segments } from './hls'
10import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
11import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
9 12
10async function optimizeOriginalVideofile (video: VideoModel) { 13async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
11 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR 14 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
12 const newExtname = '.mp4' 15 const newExtname = '.mp4'
13 const inputVideoFile = video.getOriginalFile() 16
17 const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
14 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile)) 18 const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
15 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname) 19 const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
16 20
17 const transcodeOptions = { 21 const transcodeOptions = {
18 inputPath: videoInputPath, 22 inputPath: videoInputPath,
19 outputPath: videoTranscodedPath 23 outputPath: videoTranscodedPath,
24 resolution: inputVideoFile.resolution
20 } 25 }
21 26
22 // Could be very long! 27 // Could be very long!
@@ -29,7 +34,7 @@ async function optimizeOriginalVideofile (video: VideoModel) {
29 inputVideoFile.set('extname', newExtname) 34 inputVideoFile.set('extname', newExtname)
30 35
31 const videoOutputPath = video.getVideoFilePath(inputVideoFile) 36 const videoOutputPath = video.getVideoFilePath(inputVideoFile)
32 await rename(videoTranscodedPath, videoOutputPath) 37 await move(videoTranscodedPath, videoOutputPath)
33 const stats = await stat(videoOutputPath) 38 const stats = await stat(videoOutputPath)
34 const fps = await getVideoFileFPS(videoOutputPath) 39 const fps = await getVideoFileFPS(videoOutputPath)
35 40
@@ -46,7 +51,7 @@ async function optimizeOriginalVideofile (video: VideoModel) {
46 } 51 }
47} 52}
48 53
49async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) { 54async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) {
50 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR 55 const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
51 const extname = '.mp4' 56 const extname = '.mp4'
52 57
@@ -59,13 +64,13 @@ async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoR
59 size: 0, 64 size: 0,
60 videoId: video.id 65 videoId: video.id
61 }) 66 })
62 const videoOutputPath = join(videosDirectory, video.getVideoFilename(newVideoFile)) 67 const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile))
63 68
64 const transcodeOptions = { 69 const transcodeOptions = {
65 inputPath: videoInputPath, 70 inputPath: videoInputPath,
66 outputPath: videoOutputPath, 71 outputPath: videoOutputPath,
67 resolution, 72 resolution,
68 isPortraitMode 73 isPortraitMode: isPortrait
69 } 74 }
70 75
71 await transcode(transcodeOptions) 76 await transcode(transcodeOptions)
@@ -83,6 +88,41 @@ async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoR
83 video.VideoFiles.push(newVideoFile) 88 video.VideoFiles.push(newVideoFile)
84} 89}
85 90
91async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) {
92 const baseHlsDirectory = join(HLS_PLAYLIST_DIRECTORY, video.uuid)
93 await ensureDir(join(HLS_PLAYLIST_DIRECTORY, video.uuid))
94
95 const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile()))
96 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
97
98 const transcodeOptions = {
99 inputPath: videoInputPath,
100 outputPath,
101 resolution,
102 isPortraitMode,
103
104 hlsPlaylist: {
105 videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution)
106 }
107 }
108
109 await transcode(transcodeOptions)
110
111 await updateMasterHLSPlaylist(video)
112 await updateSha256Segments(video)
113
114 const playlistUrl = CONFIG.WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
115
116 await VideoStreamingPlaylistModel.upsert({
117 videoId: video.id,
118 playlistUrl,
119 segmentsSha256Url: CONFIG.WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid),
120 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles),
121
122 type: VideoStreamingPlaylistType.HLS
123 })
124}
125
86async function importVideoFile (video: VideoModel, inputFilePath: string) { 126async function importVideoFile (video: VideoModel, inputFilePath: string) {
87 const { videoFileResolution } = await getVideoFileResolution(inputFilePath) 127 const { videoFileResolution } = await getVideoFileResolution(inputFilePath)
88 const { size } = await stat(inputFilePath) 128 const { size } = await stat(inputFilePath)
@@ -124,7 +164,8 @@ async function importVideoFile (video: VideoModel, inputFilePath: string) {
124} 164}
125 165
126export { 166export {
127 optimizeOriginalVideofile, 167 generateHlsPlaylist,
168 optimizeVideofile,
128 transcodeOriginalVideofile, 169 transcodeOriginalVideofile,
129 importVideoFile 170 importVideoFile
130} 171}