]>
Commit | Line | Data |
---|---|---|
1 | import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants' | |
2 | import { join } from 'path' | |
3 | import { getVideoFileFPS, transcode, canDoQuickTranscode } from '../helpers/ffmpeg-utils' | |
4 | import { ensureDir, move, remove, 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 | import { updateMasterHLSPlaylist, updateSha256Segments } from './hls' | |
10 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' | |
11 | import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type' | |
12 | import { CONFIG } from '../initializers/config' | |
13 | ||
14 | /** | |
15 | * Optimize the original video file and replace it. The resolution is not changed. | |
16 | */ | |
17 | async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) { | |
18 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR | |
19 | const newExtname = '.mp4' | |
20 | ||
21 | const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile() | |
22 | const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile)) | |
23 | const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname) | |
24 | ||
25 | const doQuickTranscode = await(canDoQuickTranscode(videoInputPath)) | |
26 | ||
27 | const transcodeOptions = { | |
28 | inputPath: videoInputPath, | |
29 | outputPath: videoTranscodedPath, | |
30 | resolution: inputVideoFile.resolution, | |
31 | doQuickTranscode | |
32 | } | |
33 | ||
34 | // Could be very long! | |
35 | await transcode(transcodeOptions) | |
36 | ||
37 | try { | |
38 | await remove(videoInputPath) | |
39 | ||
40 | // Important to do this before getVideoFilename() to take in account the new file extension | |
41 | inputVideoFile.set('extname', newExtname) | |
42 | ||
43 | const videoOutputPath = video.getVideoFilePath(inputVideoFile) | |
44 | await move(videoTranscodedPath, videoOutputPath) | |
45 | const stats = await stat(videoOutputPath) | |
46 | const fps = await getVideoFileFPS(videoOutputPath) | |
47 | ||
48 | inputVideoFile.set('size', stats.size) | |
49 | inputVideoFile.set('fps', fps) | |
50 | ||
51 | await video.createTorrentAndSetInfoHash(inputVideoFile) | |
52 | await inputVideoFile.save() | |
53 | } catch (err) { | |
54 | // Auto destruction... | |
55 | video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err })) | |
56 | ||
57 | throw err | |
58 | } | |
59 | } | |
60 | ||
61 | /** | |
62 | * Transcode the original video file to a lower resolution. | |
63 | */ | |
64 | async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) { | |
65 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR | |
66 | const extname = '.mp4' | |
67 | ||
68 | // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed | |
69 | const videoInputPath = join(videosDirectory, video.getVideoFilename(video.getOriginalFile())) | |
70 | ||
71 | const newVideoFile = new VideoFileModel({ | |
72 | resolution, | |
73 | extname, | |
74 | size: 0, | |
75 | videoId: video.id | |
76 | }) | |
77 | const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile)) | |
78 | ||
79 | const transcodeOptions = { | |
80 | inputPath: videoInputPath, | |
81 | outputPath: videoOutputPath, | |
82 | resolution, | |
83 | isPortraitMode: isPortrait | |
84 | } | |
85 | ||
86 | await transcode(transcodeOptions) | |
87 | ||
88 | const stats = await stat(videoOutputPath) | |
89 | const fps = await getVideoFileFPS(videoOutputPath) | |
90 | ||
91 | newVideoFile.set('size', stats.size) | |
92 | newVideoFile.set('fps', fps) | |
93 | ||
94 | await video.createTorrentAndSetInfoHash(newVideoFile) | |
95 | ||
96 | await newVideoFile.save() | |
97 | ||
98 | video.VideoFiles.push(newVideoFile) | |
99 | } | |
100 | ||
101 | async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) { | |
102 | const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) | |
103 | await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)) | |
104 | ||
105 | const videoInputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(video.getOriginalFile())) | |
106 | const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)) | |
107 | ||
108 | const transcodeOptions = { | |
109 | inputPath: videoInputPath, | |
110 | outputPath, | |
111 | resolution, | |
112 | isPortraitMode, | |
113 | ||
114 | hlsPlaylist: { | |
115 | videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution) | |
116 | } | |
117 | } | |
118 | ||
119 | await transcode(transcodeOptions) | |
120 | ||
121 | await updateMasterHLSPlaylist(video) | |
122 | await updateSha256Segments(video) | |
123 | ||
124 | const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) | |
125 | ||
126 | await VideoStreamingPlaylistModel.upsert({ | |
127 | videoId: video.id, | |
128 | playlistUrl, | |
129 | segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid), | |
130 | p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, video.VideoFiles), | |
131 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, | |
132 | ||
133 | type: VideoStreamingPlaylistType.HLS | |
134 | }) | |
135 | } | |
136 | ||
137 | export { | |
138 | generateHlsPlaylist, | |
139 | optimizeVideofile, | |
140 | transcodeOriginalVideofile | |
141 | } |