Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Создаёт скрипт для вычисления размера и длительности mp3-файла #16

Merged
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
npm run test && \
npm run file-size && \
git add .
27 changes: 25 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"cover": "cd src && rsync --archive --compress cover.png [email protected]:/var/www/web-standards.ru/podcast/",
"feed": "cd dist && rsync --archive --compress index.xml [email protected]:/var/www/web-standards.ru/podcast/feed/",
"deploy": "npm run mp3 && npm run cover && npm run feed",
"new": "node scripts/new.js"
"new": "node scripts/new.js",
"file-size": "node scripts/file-size.js",
"prepare": "husky || true"
},
"engines": {
"node": "20",
Expand All @@ -20,8 +22,10 @@
"@eslint/js": "^9.1.1",
"@stylistic/eslint-plugin-js": "^1.8.0",
"editorconfig-checker": "^5.1.1",
"get-mp3-duration": "^1.0.0",
"globals": "^15.1.0",
"html-minifier-terser": "^7.1.0",
"husky": "^9.0.11",
"js-yaml": "^4.1.0",
"minify-xml": "^4.4.1"
}
Expand Down
53 changes: 53 additions & 0 deletions scripts/file-size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import fs from 'node:fs';
import path from 'node:path';
import getMP3Duration from 'get-mp3-duration';

function isFileExist(filePath) {
try {
fs.statSync(filePath);
return true;
} catch {
return false;
}
}

const folderDataPath = path.join('src', 'episodes');
const folderMediaPath = path.join('src', 'mp3');

const dir = fs.opendirSync(folderDataPath, {
encoding: 'utf-8',
withFileTypes: true,
});

try {
let item = null;

while ((item = dir.readSync())) {
if (!item.isDirectory()) {
continue;
}

const episodeFolder = path.join(item.path, item.name);
const dataFilePath = path.join(episodeFolder, 'index.json');

if (isFileExist(dataFilePath)) {
continue;
}

const mediaFilePath = path.join(folderMediaPath, item.name + '.mp3');

if (!isFileExist(mediaFilePath)) {
console.log(`Файл '${mediaFilePath}' не найден`);
continue;
}

const fileBuffer = fs.readFileSync(mediaFilePath);
const fileSize = fileBuffer.byteLength;
const duration = getMP3Duration(fileBuffer);

const dataToSave = JSON.stringify({ fileSize, duration }, null, '\t');
fs.writeFileSync(dataFilePath, dataToSave, 'utf-8');
}
} finally {
dir.closeSync();
}