Skip to content

Instantly share code, notes, and snippets.

@danieldsf
Last active August 17, 2021 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldsf/b97311ed5a123b99b3d85bac981ef9da to your computer and use it in GitHub Desktop.
Save danieldsf/b97311ed5a123b99b3d85bac981ef9da to your computer and use it in GitHub Desktop.
Audio Looper
const mp3Duration = require('mp3-duration')
const humanInterval = require('human-interval')
const path = require('path')
const fs = require('fs')
const { exec } = require("child_process")
const MYLIST = path.basename("mylist.txt")
var milliseconds, duration, source, destination
if(!process.argv[2] || !process.argv[3] || !process.argv[4]){
throw new Error("Insuficient arguments error")
}
// Get Interval:
duration = process.argv[2]
milliseconds = humanInterval(duration)
console.log(milliseconds)
// Get Source Filename:
source = process.argv[3]
source = path.basename(source)
// Get Destination Filename:
destination = process.argv[4]
destination = path.basename(destination)
//file exists
if (fs.existsSync(MYLIST)) {
fs.unlinkSync(MYLIST)
}
const handler = fs.createWriteStream(MYLIST, {
flags: 'a'
})
mp3Duration(source, function (err, duration) {
if (err) return console.log(err.message)
let durationInMilliseconds = duration * 1000;
let times = Math.ceil(milliseconds / durationInMilliseconds)
//
handler.write(`# this is a comment\n`)
for (let index = 0; index < times; index++) {
handler.write(`file '${source}'\n`)
}
handler.end()
//
const command = `ffmpeg -f concat -safe 0 -i ${MYLIST} -c copy ${destination}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
console.log(`stdout: ${stdout}`)
console.log(`File generated: ${destination}`)
})
})
//SOURCE: https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url/49849482
// Check Valid URL for new implementations:
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return !!pattern.test(str);
}
// -> Relies on Youtube-dl and FFMpeg
//youtube-dl --extract-audio --audio-format mp3 https://www.youtube.com/watch?v=DVob1D8WlBM
{
"name": "audio-looper",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Daniel Farias",
"license": "ISC",
"dependencies": {
"human-interval": "^1.0.0",
"mp3-duration": "^1.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment