Last active
September 12, 2015 11:27
-
-
Save wildeyes/4e3b70a1532aa90a570a to your computer and use it in GitHub Desktop.
Split a youtube playlist into playable files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// post about this: https://medium.com/@xwildeyes/download-and-split-a-youtube-playlist-into-playable-files-e208e2a2e51b | |
// A script to split a youtube "playlist" file | |
// into it's mp3 parts, with corresponding filenames and | |
// everything | |
// By wildeyes. | |
// give it the name produced by youtube-dl -x https://www.youtube.com/watch?v=5nhp6ULk7mE | |
youtubeAudioFilename = process.argv[2] | |
// basically, a list of pairs of (time, name) in the format 00:00[:00] artist - song | |
// see http://www.regexr.com/3bp42 for specs file example | |
specsFilename = process.argv[3] | |
exec = function (cmd) { return require("child_process").execSync(cmd, {stdio:[null, null, null]}).toString() } | |
fs = require("fs") | |
length = exec(`ffprobe -show_format "${youtubeAudioFilename}"`).split("duration=")[1].split("\n")[0] | |
songs = fs.readFileSync(specsFilename) | |
.toString() | |
.split("\n") | |
.map(function(line) { | |
descPart = line.match(/((?:\d:)?\d\d?:\d\d) (.*)/) | |
return [descPart[1], descPart[2]] | |
}) | |
var i; | |
try { | |
i = parseInt(fs.readFileSync('split.vid.js.error.log').toString().replace('split vid stopped at:', '')) | |
console.log('found log, resuming from video ' + i) | |
} catch(err) { | |
i = 0; | |
} | |
for(; i < songs.length; i++) { | |
fromTime = songs[i][0] | |
filename1 = songs[i][1] | |
// fix for "no such file or directory" error from ffmpeg | |
filename = filename1.replace('/','') | |
if(i === songs.length - 1) | |
toTime = length | |
else { | |
console.log (i + 1, songs[i + 1] ) | |
toTime = songs[i + 1][0] | |
} | |
shellLine = `ffmpeg -y -ss ${fromTime} -t ${toTime} -i "${youtubeAudioFilename}" -acodec libmp3lame -ab 128k "${filename}.mp3"` | |
console.log(`Executing: ${shellLine}`); | |
try { | |
exec(shellLine); | |
} catch(err) { | |
console.log("writing position to file before exiting...") | |
fs.writeFileSync("split.vid.js.error.log", "split vid stopped at:" + i); | |
throw err | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment