-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize-track.js
106 lines (92 loc) · 2.29 KB
/
normalize-track.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import * as Path from 'node:path';
import { parseArgs } from 'node:util';
import * as fs from 'node:fs';
import { analyzeTrack } from './src/analyze-track.js';
import {
normalizeAudioTrackToAAC,
normalizeVideoTrackToM4V,
} from './src/render-track.js';
import { runFfmpegCommandAsync } from './src/ffexec.js';
const args = parseArgs({
options: {
input: {
type: 'string',
short: 'i',
multiple: true,
},
output_dir: {
type: 'string',
short: 'o',
},
},
});
if (args.values.input.length < 1) {
console.error(
'input is required using -i (can provide multiple files in order to combine audio and video)'
);
process.exit(1);
}
const outputDir = args.values.output_dir || Path.dirname(args.values.input[0]);
if (args.values.output_dir) {
fs.mkdirSync(outputDir, { recursive: true });
}
let videoPath;
let audioPath;
let combinedOutputPath;
for (const inputPath of args.values.input) {
if (!fs.existsSync(inputPath)) {
console.error("input path doesn't exist: ", inputPath);
process.exit(1);
}
const basename = Path.basename(inputPath, Path.extname(inputPath));
const analysis = await analyzeTrack(`analyze_${basename}`, inputPath);
if (analysis.isVideo) {
const videoOutputPath = Path.resolve(
outputDir,
basename + '_normalized.m4v'
);
await normalizeVideoTrackToM4V(
basename,
analysis,
inputPath,
videoOutputPath
);
videoPath = videoOutputPath;
combinedOutputPath = Path.resolve(outputDir, basename + '_combined.mp4');
} else {
const audioOutputPath = Path.resolve(
outputDir,
basename + '_normalized.aac'
);
await normalizeAudioTrackToAAC(
basename,
analysis,
inputPath,
audioOutputPath
);
audioPath = audioOutputPath;
}
}
if (videoPath && audioPath && combinedOutputPath) {
const basename = Path.basename(
combinedOutputPath,
Path.extname(combinedOutputPath)
);
const args = [
'-i',
videoPath,
'-i',
audioPath,
'-c',
'copy',
'-map',
'0:0',
'-map',
'1:0',
combinedOutputPath,
];
await runFfmpegCommandAsync(`combine_${basename}`, args);
fs.rmSync(videoPath);
fs.rmSync(audioPath);
console.log('combined video and audio written to: %s', combinedOutputPath);
}