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