59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import { exec } from 'child_process';
|
||
import { readdir, stat } from 'fs';
|
||
import path from 'path';
|
||
|
||
const currentDir = './'; // Текущая директория
|
||
|
||
function isVideoFile(filePath: string): Promise<boolean> {
|
||
return new Promise((resolve) => {
|
||
stat(filePath, (err, stats) => {
|
||
if (err) {
|
||
console.error(`Ошибка при получении информации о файле ${filePath}:`, err);
|
||
resolve(false);
|
||
return;
|
||
}
|
||
const ext = path.extname(filePath).toLowerCase();
|
||
const isVideo = ['.mp4', '.mov', '.avi', '.mkv'].includes(ext);
|
||
resolve(isVideo && stats.size > 0); // Убеждаемся, что файл не пустой
|
||
});
|
||
});
|
||
}
|
||
|
||
function compressVideoWithFFmpeg(filePath: string): void {
|
||
const outputFilePath = `HEVC_${path.basename(filePath)}`;
|
||
exec(`ffmpeg -i ${filePath} -c:v hevc_nvenc ${outputFilePath}`, (error, stdout, stderr) => {
|
||
if (error) {
|
||
console.error('Ошибка при сжатии видео:', error);
|
||
return;
|
||
}
|
||
if (stderr) {
|
||
console.error('FFmpeg ошибка:', stderr);
|
||
return;
|
||
}
|
||
console.log(`Сжата файл: ${filePath} в ${outputFilePath}`);
|
||
});
|
||
}
|
||
|
||
function walkThroughDirectory(dir: string): void {
|
||
readdir(dir, { withFileTypes: true }, (err, files) => {
|
||
if (err) {
|
||
console.error('Ошибка при чтении директории:', err);
|
||
return;
|
||
}
|
||
files.forEach((file) => {
|
||
const fullPath = path.join(dir, file.name);
|
||
if (file.isDirectory()) {
|
||
walkThroughDirectory(fullPath); // Рекурсивно обходим поддиректории
|
||
} else {
|
||
isVideoFile(fullPath).then((isVideo) => {
|
||
if (isVideo) {
|
||
compressVideoWithFFmpeg(fullPath);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
walkThroughDirectory(currentDir);
|