nextav/src/lib/transcode/ffmpeg-args.ts

108 lines
2.4 KiB
TypeScript

import path from 'path';
import { getTranscodeConfig } from './config';
import { probeHardwareEncoder } from './hw-probe';
import { PROFILES, TranscodeProfileName } from './profiles';
function bitrateToNumber(bitrate: string): number {
return Number.parseInt(bitrate.replace(/k$/i, ''), 10);
}
export function buildRemuxArgs(inputPath: string, outDir: string, startTime: number): string[] {
const config = getTranscodeConfig();
return [
'-hide_banner',
'-loglevel',
'error',
'-nostats',
...(startTime > 0 ? ['-ss', startTime.toString()] : []),
'-i',
inputPath,
'-map',
'0:v:0',
'-map',
'0:a:0?',
'-c:v',
'copy',
'-bsf:v',
'h264_mp4toannexb',
'-c:a',
'copy',
'-f',
'hls',
'-hls_time',
config.segmentDuration.toString(),
'-hls_list_size',
'0',
'-hls_segment_type',
'mpegts',
'-hls_flags',
'independent_segments+temp_file',
'-hls_segment_filename',
path.join(outDir, 'seg_%05d.ts'),
'-start_number',
'0',
path.join(outDir, 'index.m3u8'),
];
}
export function buildTranscodeArgs(inputPath: string, outDir: string, profileName: Exclude<TranscodeProfileName, 'remux-copy'>, startTime: number): string[] {
const config = getTranscodeConfig();
const profile = PROFILES[profileName];
const encoder = probeHardwareEncoder();
const bitrate = bitrateToNumber(profile.vBitrate);
return [
'-hide_banner',
'-loglevel',
'error',
'-nostats',
...(startTime > 0 ? ['-ss', startTime.toString()] : []),
'-i',
inputPath,
'-map',
'0:v:0',
'-map',
'0:a:0?',
'-c:v',
encoder.codec,
...encoder.presetArgs,
'-profile:v',
'main',
'-level',
'4.1',
'-pix_fmt',
'yuv420p',
'-vf',
`scale=-2:min(${profile.maxHeight}\\,ih):flags=lanczos`,
'-b:v',
profile.vBitrate,
'-maxrate',
profile.vBitrate,
'-bufsize',
`${bitrate * 2}k`,
'-force_key_frames',
`expr:gte(t,n_forced*${config.segmentDuration})`,
'-c:a',
profile.aCodec,
'-b:a',
profile.aBitrate,
'-ac',
'2',
'-f',
'hls',
'-hls_time',
config.segmentDuration.toString(),
'-hls_list_size',
'0',
'-hls_segment_type',
'mpegts',
'-hls_flags',
'independent_segments+temp_file',
'-hls_segment_filename',
path.join(outDir, 'seg_%05d.ts'),
'-start_number',
'0',
path.join(outDir, 'index.m3u8'),
];
}