Skip to content

Instantly share code, notes, and snippets.

@tayiorbeii
Created April 11, 2023 18:46
Show Gist options
  • Save tayiorbeii/c5507d99c8cef65f9faca243416d8985 to your computer and use it in GitHub Desktop.
Save tayiorbeii/c5507d99c8cef65f9faca243416d8985 to your computer and use it in GitHub Desktop.
import "@johnlindquist/kit"
let files = await drop()
cd(path.dirname(files[0].path))
let lengthStrings = []
let totalSeconds = 0
// function that formats seconds into hours, minutes and seconds
function formatTime(totalSeconds) {
let hours = Math.floor(totalSeconds / 3600)
let minutes = Math.floor((totalSeconds - (hours * 3600)) / 60)
let seconds = totalSeconds - (hours * 3600) - (minutes * 60)
// round seconds
seconds = Math.round(seconds * 100) / 100
let result = (hours < 10 ? "0" + hours : hours)
result += ":" + (minutes < 10 ? "0" + minutes : minutes)
result += ":" + (seconds < 10 ? "0" + seconds : seconds)
// if hours is 0, don't show it
if (hours === 0) {
return result.substring(3)
}
return result
}
for (let file of files) {
let {stdout} = await exec(`ffprobe -i "${file.path}" -show_entries format=duration -v quiet -of csv="p=0"`)
let duration = parseFloat(stdout)
let durationInSeconds = Math.round(duration)
totalSeconds += durationInSeconds
lengthStrings.push(`${file.name} - ${formatTime(durationInSeconds)}`)
}
let totalLength = formatTime(totalSeconds)
await div(`<div>${lengthStrings.join("<br/><br/>")}<br/><br/>Total length: ${totalLength}</div>s`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment