-/**
- * A toolbox to play with audio
- */
-namespace audio {
- export const get = (_ffmpeg, pos: number | string = 0) => {
- // without position, ffprobe considers the last input only
- // we make it consider the first input only
- // if you pass a file path to pos, then ffprobe acts on that file directly
- return new Promise<{ absolutePath: string, audioStream?: any }>((res, rej) => {
- _ffmpeg.ffprobe(pos, (err,data) => {
- if (err) return rej(err)
-
- if ('streams' in data) {
- const audioStream = data['streams'].find(stream => stream['codec_type'] === 'audio')
- if (audioStream) {
- return res({
- absolutePath: data.format.filename,
- audioStream
- })
- }
- }
- return res({ absolutePath: data.format.filename })
- })
- })
+async function buildAudioMergeCommand (command: ffmpeg.FfmpegCommand, options: MergeAudioTranscodeOptions) {
+ command = command.loop(undefined)
+
+ command = await presetVideo(command, options.audioPath, options)
+
+ command.outputOption('-preset:v veryfast')
+
+ 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
+}
+
+function addCommonHLSVODCommandOptions (command: ffmpeg.FfmpegCommand, outputPath: string) {
+ return command.outputOption('-hls_time 4')
+ .outputOption('-hls_list_size 0')
+ .outputOption('-hls_playlist_type vod')
+ .outputOption('-hls_segment_filename ' + outputPath)
+ .outputOption('-hls_segment_type fmp4')
+ .outputOption('-f hls')
+ .outputOption('-hls_flags single_file')
+}
+
+async function buildHLSVODCommand (command: ffmpeg.FfmpegCommand, options: HLSTranscodeOptions) {
+ const videoPath = getHLSVideoPath(options)
+
+ if (options.copyCodecs) command = presetCopy(command)
+ else if (options.resolution === VideoResolution.H_NOVIDEO) command = presetOnlyAudio(command)
+ else command = await buildx264VODCommand(command, options)
+
+ addCommonHLSVODCommandOptions(command, videoPath)
+
+ return command
+}
+
+async function buildHLSVODFromTSCommand (command: ffmpeg.FfmpegCommand, options: HLSFromTSTranscodeOptions) {
+ const videoPath = getHLSVideoPath(options)
+
+ command.outputOption('-c copy')
+
+ if (options.isAAC) {
+ // Required for example when copying an AAC stream from an MPEG-TS
+ // Since it's a bitstream filter, we don't need to reencode the audio
+ command.outputOption('-bsf:a aac_adtstoasc')