+async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) {
+ let fps = await getVideoFileFPS(options.inputPath)
+ if (
+ // On small/medium resolutions, limit FPS
+ options.resolution !== undefined &&
+ options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN &&
+ fps > VIDEO_TRANSCODING_FPS.AVERAGE
+ ) {
+ // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value
+ fps = getClosestFramerateStandard(fps, 'STANDARD')
+ }
+
+ command = await presetH264(command, options.inputPath, options.resolution, fps)
+
+ if (options.resolution !== undefined) {
+ // '?x720' or '720x?' for example
+ const size = options.isPortraitMode === true ? `${options.resolution}x?` : `?x${options.resolution}`
+ command = command.size(size)
+ }
+
+ if (fps) {
+ // Hard FPS limits
+ if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD')
+ else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN
+
+ command = command.withFPS(fps)
+ }
+
+ return command
+}
+
+async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) {
+ command = command.loop(undefined)
+
+ command = await presetH264VeryFast(command, options.audioPath, options.resolution)
+
+ command = command.input(options.audioPath)
+ .videoFilter('scale=trunc(iw/2)*2:trunc(ih/2)*2') // Avoid "height not divisible by 2" error
+ .outputOption('-tune stillimage')
+ .outputOption('-shortest')
+
+ return command
+}
+
+function buildOnlyAudioCommand (command: ffmpeg.FfmpegCommand, options: OnlyAudioTranscodeOptions) {
+ command = presetOnlyAudio(command)
+
+ return command
+}
+
+function buildQuickTranscodeCommand (command: ffmpeg.FfmpegCommand) {
+ command = presetCopy(command)
+
+ command = command.outputOption('-map_metadata -1') // strip all metadata
+ .outputOption('-movflags faststart')
+
+ return command
+}
+
+async function buildHLSCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) {
+ const videoPath = getHLSVideoPath(options)
+
+ if (options.copyCodecs) command = presetCopy(command)
+ else command = await buildx264Command(command, options)
+
+ command = command.outputOption('-hls_time 4')
+ .outputOption('-hls_list_size 0')
+ .outputOption('-hls_playlist_type vod')
+ .outputOption('-hls_segment_filename ' + videoPath)
+ .outputOption('-hls_segment_type fmp4')
+ .outputOption('-f hls')
+ .outputOption('-hls_flags single_file')
+
+ return command
+}
+
+function getHLSVideoPath (options: HLSTranscodeOptions) {
+ return `${dirname(options.outputPath)}/${options.hlsPlaylist.videoFilename}`
+}
+
+async function fixHLSPlaylistIfNeeded (options: TranscodeOptions) {
+ if (options.type !== 'hls') return
+
+ const fileContent = await readFile(options.outputPath)
+
+ const videoFileName = options.hlsPlaylist.videoFilename
+ const videoFilePath = getHLSVideoPath(options)
+
+ // Fix wrong mapping with some ffmpeg versions
+ const newContent = fileContent.toString()
+ .replace(`#EXT-X-MAP:URI="${videoFilePath}",`, `#EXT-X-MAP:URI="${videoFileName}",`)
+
+ await writeFile(options.outputPath, newContent)
+}
+
+function getVideoStreamFromFile (path: string) {