Skip to content

Instantly share code, notes, and snippets.

@cruizba
Last active January 3, 2021 18:35
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 cruizba/5092cc4bd3027614c392a4221ff97654 to your computer and use it in GitHub Desktop.
Save cruizba/5092cc4bd3027614c392a4221ff97654 to your computer and use it in GitHub Desktop.
Split youtube audio/video file into sections using definition file
// tested on https://www.youtube.com/watch?v=5nhp6ULk7mE
// see http://www.regexr.com/3bp42
exec = function (cmd) { return require("child_process").execSync(cmd, {stdio:[null, null, null]}).toString() }
console.log((function() {
fs = require("fs")
youtubeVideoFilename = process.argv[2]
specsFilename = process.argv[3]
length = exec(`ffprobe -show_format "${youtubeVideoFilename}"`).split("duration=")[1].split("\n")[0]
end = new Date(length * 1000).toISOString().substr(11, 8);
songSpecifications = fs.readFileSync(specsFilename)
.toString()
.split("\n")
.map(function(line) {
descPart = line.match(/((?:\d:)?\d\d?:\d\d) (.*)/)
return [descPart[1], descPart[2]]
})
for(var i = 0; i < songSpecifications.length; i++) {
fromTime = songSpecifications[i][0]
filename = songSpecifications[i][1]
if ((i + 1) === songSpecifications.length) {
toTime = end;
} else {
toTime = songSpecifications[i + 1][0]
}
shellLine = `ffmpeg -y -i "${youtubeVideoFilename}" -ss ${fromTime} -to ${toTime} -acodec libmp3lame -ab 192k "${filename}.mp3"`
console.log(`Executing: ${shellLine}`);
exec(shellLine);
}
})())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment