image-video-optimizer 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/videoProcessor.js +5 -68
package/package.json
CHANGED
package/src/videoProcessor.js
CHANGED
|
@@ -18,6 +18,11 @@ class VideoProcessor {
|
|
|
18
18
|
console.log(`Processing video: ${path.basename(inputPath)}`);
|
|
19
19
|
|
|
20
20
|
const outputPath = this.generateOutputPath(inputPath);
|
|
21
|
+
const parsedPath = path.parse(inputPath);
|
|
22
|
+
|
|
23
|
+
// Always use temporary file for processing
|
|
24
|
+
const tempPath = path.join(parsedPath.dir, `${parsedPath.name}_temp.mp4`);
|
|
25
|
+
console.log(`Using temporary path for processing: ${path.basename(tempPath)}`);
|
|
21
26
|
|
|
22
27
|
ffmpeg.ffprobe(inputPath, (err, metadata) => {
|
|
23
28
|
if (err) {
|
|
@@ -44,13 +49,6 @@ class VideoProcessor {
|
|
|
44
49
|
return;
|
|
45
50
|
}
|
|
46
51
|
|
|
47
|
-
const outputPath = this.generateOutputPath(inputPath);
|
|
48
|
-
const parsedPath = path.parse(inputPath);
|
|
49
|
-
|
|
50
|
-
// Always use temporary file for processing
|
|
51
|
-
const tempPath = path.join(parsedPath.dir, `${parsedPath.name}_temp.mp4`);
|
|
52
|
-
console.log(`Using temporary path for processing: ${path.basename(tempPath)}`);
|
|
53
|
-
|
|
54
52
|
let tempFfmpegCommand = ffmpeg(inputPath);
|
|
55
53
|
|
|
56
54
|
if (needsResize) {
|
|
@@ -113,67 +111,6 @@ class VideoProcessor {
|
|
|
113
111
|
resolve({ processed: false, error: err.message });
|
|
114
112
|
})
|
|
115
113
|
.run();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
let normalFfmpegCommand = ffmpeg(inputPath);
|
|
119
|
-
|
|
120
|
-
if (needsResize) {
|
|
121
|
-
const newHeight = Math.round((maxWidth / videoStream.width) * videoStream.height);
|
|
122
|
-
normalFfmpegCommand = normalFfmpegCommand.videoFilters(`scale=${maxWidth}:${newHeight}`);
|
|
123
|
-
console.log(`Resizing to: ${maxWidth}x${newHeight}`);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
switch (encodeFormat.toLowerCase()) {
|
|
127
|
-
case 'h264':
|
|
128
|
-
normalFfmpegCommand = normalFfmpegCommand.videoCodec('libx264').audioCodec('aac');
|
|
129
|
-
break;
|
|
130
|
-
case 'h265':
|
|
131
|
-
normalFfmpegCommand = normalFfmpegCommand.videoCodec('libx265').audioCodec('aac');
|
|
132
|
-
break;
|
|
133
|
-
case 'vp9':
|
|
134
|
-
normalFfmpegCommand = normalFfmpegCommand.videoCodec('libvpx-vp9').audioCodec('libvorbis');
|
|
135
|
-
break;
|
|
136
|
-
default:
|
|
137
|
-
normalFfmpegCommand = normalFfmpegCommand.videoCodec('libx264').audioCodec('aac');
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Add input/output options with proper quoting
|
|
141
|
-
normalFfmpegCommand = normalFfmpegCommand.inputOptions(['-fflags', '+genpts']);
|
|
142
|
-
normalFfmpegCommand = normalFfmpegCommand.outputOptions(['-crf', '23', '-preset', 'medium', '-y']);
|
|
143
|
-
normalFfmpegCommand = normalFfmpegCommand.format('mp4');
|
|
144
|
-
normalFfmpegCommand = normalFfmpegCommand.output(outputPath);
|
|
145
|
-
|
|
146
|
-
normalFfmpegCommand
|
|
147
|
-
.on('start', (commandLine) => {
|
|
148
|
-
console.log(`FFmpeg command: ${commandLine}`);
|
|
149
|
-
})
|
|
150
|
-
.on('end', () => {
|
|
151
|
-
const compressionResult = this.checkCompression(inputPath, outputPath);
|
|
152
|
-
|
|
153
|
-
if (compressionResult.effective) {
|
|
154
|
-
console.log(`✓ Processed: ${path.basename(inputPath)} (${compressionResult.compressionPercent}% reduction)`);
|
|
155
|
-
resolve({
|
|
156
|
-
processed: true,
|
|
157
|
-
outputPath,
|
|
158
|
-
originalSize: compressionResult.originalSize,
|
|
159
|
-
newSize: compressionResult.newSize,
|
|
160
|
-
compressionPercent: compressionResult.compressionPercent
|
|
161
|
-
});
|
|
162
|
-
} else {
|
|
163
|
-
console.log(`✗ Ineffective compression, keeping original: ${path.basename(inputPath)}`);
|
|
164
|
-
fs.unlinkSync(outputPath);
|
|
165
|
-
resolve({ processed: false, reason: 'ineffective_compression' });
|
|
166
|
-
}
|
|
167
|
-
})
|
|
168
|
-
.on('error', (err) => {
|
|
169
|
-
console.error(`Error processing video ${inputPath}:`, err.message);
|
|
170
|
-
console.error(`FFmpeg stderr: ${err.stderr || 'No stderr available'}`);
|
|
171
|
-
if (fs.existsSync(outputPath)) {
|
|
172
|
-
fs.unlinkSync(outputPath);
|
|
173
|
-
}
|
|
174
|
-
resolve({ processed: false, error: err.message });
|
|
175
|
-
})
|
|
176
|
-
.run();
|
|
177
114
|
});
|
|
178
115
|
} catch (error) {
|
|
179
116
|
console.error(`Error processing video ${inputPath}:`, error.message);
|