Skip to content

Instantly share code, notes, and snippets.

@cednore
Created March 7, 2024 08:21
Show Gist options
  • Save cednore/7257d6b90dc3508385c520e7da120777 to your computer and use it in GitHub Desktop.
Save cednore/7257d6b90dc3508385c520e7da120777 to your computer and use it in GitHub Desktop.
Convert the timezone of timestamps in multiple mp3 recording files and organize them into folders based on the day number
#!/usr/bin/env zx
const tz = new Date().toLocaleString("en", { timeZoneName: "short" }).split(" ").pop();
const listOfMp3Files = (await $`ls *.mp3`).stdout.split("\n").filter(Boolean);
for (const filename of listOfMp3Files) {
let year = filename.slice(0, 4);
let month = filename.slice(4, 6);
let day = filename.slice(6, 8);
let hour = filename.slice(9, 11);
let minute = filename.slice(11, 13);
let second = filename.slice(13, 15);
let timestamp = new Date(`${year}-${month}-${day} ${hour}:${minute}:${second} ${tz}`);
const date = timestamp.toLocaleDateString("en-US", {
timeZone: "UTC",
hour12: false,
year: "numeric",
month: "2-digit",
day: "2-digit",
});
const time = timestamp.toLocaleTimeString("en-US", {
timeZone: "UTC",
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
day = date.split("/")[1];
hour = time.split(":")[0];
minute = time.split(":")[1];
// create folder with the name of the day, if found then ignore
await $`mkdir -p ${day}`;
// move the file to the folder
await $`mv ${filename} ${day}/${hour}.${minute}.mp3`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment