refactor: убрать useNvenc и разделить выбор энкодера/декодера

This commit is contained in:
2026-01-20 10:38:25 +03:00
parent f550b7eb69
commit 224f14a8e0
8 changed files with 194 additions and 68 deletions

View File

@@ -10,7 +10,7 @@
* create-vod ./video.mp4 ./output -r 720,1080
*/
import { convertToDash, checkFFmpeg, checkMP4Box, getVideoMetadata, detectHardwareEncoders } from './index';
import { convertToDash, checkFFmpeg, checkMP4Box, getVideoMetadata, detectHardwareEncoders, detectHardwareDecoders } from './index';
import cliProgress from 'cli-progress';
import { statSync } from 'node:fs';
import { basename, extname } from 'node:path';
@@ -31,6 +31,7 @@ let h264CRF: number | undefined;
let av1CQ: number | undefined;
let av1CRF: number | undefined;
let accelerator: HardwareAccelerationOption | undefined;
let decoder: HardwareAccelerationOption | undefined;
// First pass: extract flags and their values
for (let i = 0; i < args.length; i++) {
@@ -101,15 +102,24 @@ for (let i = 0; i < args.length; i++) {
process.exit(1);
}
i++; // Skip next arg
} else if (args[i] === '--accel' || args[i] === '--hardware') {
} else if (args[i] === '--accel' || args[i] === '--hardware' || args[i] === '-e' || args[i] === '--encoder') {
const acc = args[i + 1];
const allowed: HardwareAccelerationOption[] = ['auto', 'nvenc', 'qsv', 'amf', 'cpu'];
const allowed: HardwareAccelerationOption[] = ['auto', 'nvenc', 'qsv', 'amf', 'cpu', 'vaapi', 'videotoolbox', 'v4l2'];
if (!allowed.includes(acc as HardwareAccelerationOption)) {
console.error(`❌ Invalid accelerator: ${acc}. Valid: auto, nvenc, qsv, amf, cpu`);
console.error(`❌ Invalid accelerator: ${acc}. Valid: auto, nvenc, qsv, amf, vaapi, videotoolbox, v4l2, cpu`);
process.exit(1);
}
accelerator = acc as HardwareAccelerationOption;
i++;
} else if (args[i] === '-d' || args[i] === '--decoder') {
const acc = args[i + 1];
const allowed: HardwareAccelerationOption[] = ['auto', 'nvenc', 'qsv', 'amf', 'vaapi', 'videotoolbox', 'v4l2', 'cpu'];
if (!allowed.includes(acc as HardwareAccelerationOption)) {
console.error(`❌ Invalid decoder: ${acc}. Valid: auto, nvenc, qsv, amf, vaapi, videotoolbox, v4l2, cpu`);
process.exit(1);
}
decoder = acc as HardwareAccelerationOption;
i++;
} else if (!args[i].startsWith('-')) {
// Positional argument
positionalArgs.push(args[i]);
@@ -127,7 +137,8 @@ if (!input) {
console.error(' -c, --codec Video codec: av1, h264, or dual (default: dual)');
console.error(' -f, --format Streaming format: dash, hls, or both (default: both)');
console.error(' -p, --poster Poster timecode (e.g., 00:00:05 or 10)');
console.error(' --accel <type> Hardware accelerator: auto|nvenc|qsv|amf|cpu (default: auto)');
console.error(' -e, --encoder <type> Hardware encoder: auto|nvenc|qsv|amf|vaapi|videotoolbox|v4l2|cpu (default: auto)');
console.error(' -d, --decoder <type> Hardware decoder: auto|nvenc|qsv|amf|vaapi|videotoolbox|v4l2|cpu (default: auto)');
console.error('\nQuality Options (override defaults):');
console.error(' --h264-cq <value> H.264 GPU CQ value (0-51, lower = better, default: auto)');
console.error(' --h264-crf <value> H.264 CPU CRF value (0-51, lower = better, default: auto)');
@@ -153,6 +164,7 @@ const hasFFmpeg = await checkFFmpeg();
const hasMP4Box = await checkMP4Box();
const hwEncoders = await detectHardwareEncoders();
const hasAv1Hardware = hwEncoders.some(item => item.av1Encoder);
const hwDecoders = await detectHardwareDecoders();
const accelPriority: Record<string, number> = {
nvenc: 100,
@@ -176,6 +188,10 @@ const accelLabel = bestAccelName
? `${bestAccelName}${accelRest.length > 0 ? ` (${accelRest.join(', ')})` : ''}`
: '❌';
console.log(`Hardware: ${accelLabel}`);
if (hwDecoders.length > 0) {
const decList = Array.from(new Set(hwDecoders.map((d) => d.accelerator.toUpperCase())));
console.log(`Decoders: ${decList.join(', ')}`);
}
console.log('');
if (!hasFFmpeg) {
@@ -267,7 +283,13 @@ const manifestDesc =
const thumbnailsPlanned = true;
const posterPlanned = posterTimecode || '00:00:00';
const codecDisplay = codecType === 'dual' ? 'dual (AV1 + H.264)' : codecType;
const codecNote = codecType === 'h264' && accelRest && accelRest.length >= 0 && !hasAv1Hardware ? ' (AV1 disabled: no HW)' : '';
const codecNote = codecType === 'h264' && !hasAv1Hardware ? ' (AV1 disabled: no HW)' : '';
const plannedAccel = accelerator ? accelerator.toUpperCase() : (bestAccelName || 'CPU');
const plannedDecoder = decoder ? decoder.toUpperCase() : (hwDecoders[0]?.accelerator.toUpperCase() || 'CPU');
const acceleratorDisplay = plannedAccel === 'AUTO' ? (bestAccelName || 'CPU') : plannedAccel;
const decoderDisplay = plannedDecoder === 'AUTO'
? (hwDecoders[0]?.accelerator.toUpperCase() || 'CPU')
: plannedDecoder;
console.log('\n📦 Parameters:');
console.log(` Input: ${input}`);
@@ -277,7 +299,8 @@ console.log(` Profiles: ${displayProfiles.join(', ')}`);
console.log(` Manifests: ${manifestDesc}`);
console.log(` Poster: ${posterPlanned} (will be generated)`);
console.log(` Thumbnails: ${thumbnailsPlanned ? 'yes (with VTT)' : 'no'}`);
console.log(` Accelerator: ${bestAccel ? bestAccel.accelerator.toUpperCase() : 'CPU'}`);
console.log(` Accelerator: ${acceleratorDisplay}`);
console.log(` Decoder: ${decoderDisplay}`);
// Build quality settings if any are specified
let quality: QualitySettings | undefined;
@@ -316,6 +339,7 @@ const bars: Record<string, any> = {};
let overallBar: any = null;
try {
const startedAt = Date.now();
const result = await convertToDash({
input,
outputDir,
@@ -325,6 +349,7 @@ try {
format: formatType,
segmentDuration: 2,
hardwareAccelerator: accelerator,
hardwareDecoder: decoder,
quality,
generateThumbnails: true,
generatePoster: true,
@@ -368,7 +393,9 @@ try {
multibar.stop();
console.log('\n✅ Conversion completed successfully!\n');
const elapsedMs = Date.now() - startedAt;
const elapsedSec = (elapsedMs / 1000).toFixed(2);
console.log(`\n✅ Conversion completed successfully! (${elapsedSec}s)\n`);
} catch (error) {
multibar.stop();