]>
Commit | Line | Data |
---|---|---|
9c6ca37f | 1 | import { CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY } from '../initializers' |
30842128 C |
2 | import { join } from 'path' |
3 | import { getVideoFileFPS, transcode } from '../helpers/ffmpeg-utils' | |
4 | import { ensureDir, move, remove, stat } from 'fs-extra' | |
098eb377 C |
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' | |
09209296 C |
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' | |
098eb377 | 12 | |
9f1ddd24 | 13 | async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) { |
098eb377 C |
14 | const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR |
15 | const newExtname = '.mp4' | |
9f1ddd24 C |
16 | |
17 | const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile() | |
18 | const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile)) | |
098eb377 C |
19 | const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname) |
20 | ||
21 | const transcodeOptions = { | |
22 | inputPath: videoInputPath, | |
09209296 C |
23 | outputPath: videoTranscodedPath, |
24 | resolution: inputVideoFile.resolution | |
098eb377 C |
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) | |
f481c4f9 | 37 | await move(videoTranscodedPath, videoOutputPath) |
098eb377 C |
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 | ||
09209296 | 54 | async function transcodeOriginalVideofile (video: VideoModel, resolution: VideoResolution, isPortrait: boolean) { |
098eb377 C |
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 | }) | |
09209296 | 67 | const videoOutputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(newVideoFile)) |
098eb377 C |
68 | |
69 | const transcodeOptions = { | |
70 | inputPath: videoInputPath, | |
71 | outputPath: videoOutputPath, | |
72 | resolution, | |
09209296 | 73 | isPortraitMode: isPortrait |
098eb377 C |
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 | ||
09209296 | 91 | async function generateHlsPlaylist (video: VideoModel, resolution: VideoResolution, isPortraitMode: boolean) { |
9c6ca37f C |
92 | const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid) |
93 | await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)) | |
09209296 C |
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, | |
4c280004 C |
103 | |
104 | hlsPlaylist: { | |
105 | videoFilename: VideoStreamingPlaylistModel.getHlsVideoName(video.uuid, resolution) | |
106 | } | |
09209296 C |
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 | ||
098eb377 | 126 | export { |
09209296 | 127 | generateHlsPlaylist, |
edb4ffc7 | 128 | optimizeVideofile, |
30842128 | 129 | transcodeOriginalVideofile |
098eb377 | 130 | } |