+async function generateHlsPlaylistCommon (options: {
+ type: 'hls' | 'hls-from-ts'
+ video: MVideoWithFile
+ inputPath: string
+ resolution: VideoResolution
+ copyCodecs?: boolean
+ isAAC?: boolean
+ isPortraitMode: boolean
+
+ job?: Job
+}) {
+ const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
+
+ const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
+ await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
+
+ const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
+ const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
+
+ const transcodeOptions = {
+ type,
+
+ inputPath,
+ outputPath,
+
+ availableEncoders,
+ profile: 'default',
+
+ resolution,
+ copyCodecs,
+ isPortraitMode,
+
+ isAAC,
+
+ hlsPlaylist: {
+ videoFilename
+ },
+
+ job
+ }
+
+ await transcode(transcodeOptions)
+
+ const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
+
+ const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
+ videoId: video.id,
+ playlistUrl,
+ segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
+ p2pMediaLoaderInfohashes: [],
+ p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
+
+ type: VideoStreamingPlaylistType.HLS
+ }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
+ videoStreamingPlaylist.Video = video
+
+ const newVideoFile = new VideoFileModel({
+ resolution,
+ extname: extnameUtil(videoFilename),
+ size: 0,
+ fps: -1,
+ videoStreamingPlaylistId: videoStreamingPlaylist.id
+ })
+
+ const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
+ const stats = await stat(videoFilePath)
+
+ newVideoFile.size = stats.size
+ newVideoFile.fps = await getVideoFileFPS(videoFilePath)
+ newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
+
+ await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
+
+ await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
+ videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
+
+ videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
+ playlistUrl, videoStreamingPlaylist.VideoFiles
+ )
+ await videoStreamingPlaylist.save()
+
+ video.setHLSPlaylist(videoStreamingPlaylist)
+
+ await updateMasterHLSPlaylist(video)
+ await updateSha256VODSegments(video)
+
+ return outputPath