X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fffmpeg%2Fffmpeg-live.ts;h=379d7b1ad5b532b72162bd6eb4801aa2b4351cfb;hb=b1dbb9fefc870a90b25f5c0153589f45c9e75e3e;hp=ff571626c98bb11557c944d3df3820e488c30671;hpb=ab4b8974997777373a6032073f9c1aaf33ba9931;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/ffmpeg/ffmpeg-live.ts b/server/helpers/ffmpeg/ffmpeg-live.ts index ff571626c..379d7b1ad 100644 --- a/server/helpers/ffmpeg/ffmpeg-live.ts +++ b/server/helpers/ffmpeg/ffmpeg-live.ts @@ -1,7 +1,7 @@ import { FfmpegCommand, FilterSpecification } from 'fluent-ffmpeg' import { join } from 'path' import { VIDEO_LIVE } from '@server/initializers/constants' -import { AvailableEncoders } from '@shared/models' +import { AvailableEncoders, LiveVideoLatencyMode } from '@shared/models' import { logger, loggerTagsFactory } from '../logger' import { buildStreamSuffix, getFFmpeg, getScaleFilter, StreamType } from './ffmpeg-commons' import { getEncoderBuilderResult } from './ffmpeg-encoders' @@ -15,6 +15,7 @@ async function getLiveTranscodingCommand (options: { outPath: string masterPlaylistName: string + latencyMode: LiveVideoLatencyMode resolutions: number[] @@ -22,11 +23,24 @@ async function getLiveTranscodingCommand (options: { fps: number bitrate: number ratio: number + hasAudio: boolean availableEncoders: AvailableEncoders profile: string }) { - const { inputUrl, outPath, resolutions, fps, bitrate, availableEncoders, profile, masterPlaylistName, ratio } = options + const { + inputUrl, + outPath, + resolutions, + fps, + bitrate, + availableEncoders, + profile, + masterPlaylistName, + ratio, + latencyMode, + hasAudio + } = options const command = getFFmpeg(inputUrl, 'live') @@ -46,6 +60,7 @@ async function getLiveTranscodingCommand (options: { addDefaultEncoderGlobalParams(command) for (let i = 0; i < resolutions.length; i++) { + const streamMap: string[] = [] const resolution = resolutions[i] const resolutionFPS = computeFPS(fps, resolution) @@ -93,9 +108,11 @@ async function getLiveTranscodingCommand (options: { options: `w=-2:h=${resolution}`, outputs: `vout${resolution}` }) + + streamMap.push(`v:${i}`) } - { + if (hasAudio) { const streamType: StreamType = 'audio' const builderResult = await getEncoderBuilderResult({ ...baseEncoderBuilderParams, streamType }) if (!builderResult) { @@ -113,21 +130,30 @@ async function getLiveTranscodingCommand (options: { command.outputOption(`${buildStreamSuffix('-c:a', i)} ${builderResult.encoder}`) applyEncoderOptions(command, builderResult.result) + + streamMap.push(`a:${i}`) } - varStreamMap.push(`v:${i},a:${i}`) + varStreamMap.push(streamMap.join(',')) } command.complexFilter(complexFilter) - addDefaultLiveHLSParams(command, outPath, masterPlaylistName) + addDefaultLiveHLSParams({ command, outPath, masterPlaylistName, latencyMode }) command.outputOption('-var_stream_map', varStreamMap.join(' ')) return command } -function getLiveMuxingCommand (inputUrl: string, outPath: string, masterPlaylistName: string) { +function getLiveMuxingCommand (options: { + inputUrl: string + outPath: string + masterPlaylistName: string + latencyMode: LiveVideoLatencyMode +}) { + const { inputUrl, outPath, masterPlaylistName, latencyMode } = options + const command = getFFmpeg(inputUrl, 'live') command.outputOption('-c:v copy') @@ -135,24 +161,41 @@ function getLiveMuxingCommand (inputUrl: string, outPath: string, masterPlaylist command.outputOption('-map 0:a?') command.outputOption('-map 0:v?') - addDefaultLiveHLSParams(command, outPath, masterPlaylistName) + addDefaultLiveHLSParams({ command, outPath, masterPlaylistName, latencyMode }) return command } +function getLiveSegmentTime (latencyMode: LiveVideoLatencyMode) { + if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) { + return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY + } + + return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY +} + // --------------------------------------------------------------------------- export { + getLiveSegmentTime, + getLiveTranscodingCommand, getLiveMuxingCommand } // --------------------------------------------------------------------------- -function addDefaultLiveHLSParams (command: FfmpegCommand, outPath: string, masterPlaylistName: string) { - command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS) +function addDefaultLiveHLSParams (options: { + command: FfmpegCommand + outPath: string + masterPlaylistName: string + latencyMode: LiveVideoLatencyMode +}) { + const { command, outPath, masterPlaylistName, latencyMode } = options + + command.outputOption('-hls_time ' + getLiveSegmentTime(latencyMode)) command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE) - command.outputOption('-hls_flags delete_segments+independent_segments') + command.outputOption('-hls_flags delete_segments+independent_segments+program_date_time') command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`) command.outputOption('-master_pl_name ' + masterPlaylistName) command.outputOption(`-f hls`)