Skip to content

Instantly share code, notes, and snippets.

@kiebekierror
Created July 14, 2024 02:07
Show Gist options
  • Save kiebekierror/2af98c1fef61e051a602a129b193dafe to your computer and use it in GitHub Desktop.
Save kiebekierror/2af98c1fef61e051a602a129b193dafe to your computer and use it in GitHub Desktop.
get metadata for "Create a custom static MP3 Stream URL with admin to update URL"
const fs = require("fs");
const path = require("path");
function readMetadata(audioFilePath) {
fs.open(audioFilePath, "r", function (err, fd) {
if (err) {
console.error("Error opening file:", err);
return;
}
// Read the first 128 bytes (where ID3 tags are usually located)
const buffer = Buffer.alloc(128);
fs.read(fd, buffer, 0, 128, 0, function (err, bytesRead, buffer) {
if (err) {
console.error("Error reading file:", err);
fs.close(fd, function (err) {
if (err) console.error("Error closing file:", err);
});
return;
}
// Check if the file has an ID3v2 tag (common format for MP3 metadata)
const tagHeader = buffer.toString("utf-8", 0, 3);
if (tagHeader === "ID3") {
const title = buffer.toString("utf-8", 3, 33).trim();
const artist = buffer.toString("utf-8", 33, 63).trim();
const album = buffer.toString("utf-8", 63, 93).trim();
const year = buffer.toString("utf-8", 93, 97).trim();
console.log("Title:", title);
console.log("Artist:", artist);
console.log("Album:", album);
console.log("Year:", year);
} else {
console.log("No ID3v2 tag found or unsupported format");
}
fs.close(fd, function (err) {
if (err) console.error("Error closing file:", err);
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment