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