Force timestamps using --timestamps-only
Diff
index.js | 6 +++++-
src/parser.js | 28 ++++++++++++++++++++--------
test/parser_test.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
3 files changed, 80 insertions(+), 42 deletions(-)
@@ -22,6 +22,8 @@
where $VIDEOTITLE is the title of the YouTube video.
--timestamps-only Do not try to parse the timestamps as track durations
Examples
$ youtube-cue --audio-file audio.m4a "https://www.youtube.com/watch?v=THzUassmQwE"
"T A Y L O R S W I F T – Folklore [Full album].cue" saved
@@ -34,6 +36,8 @@
let audioFile = argv['audio-file']? argv['audio-file'] : `${info.videoDetails.title}.m4a`
let output_file = argv._[1]? argv._[1] : `${info.videoDetails.title}.cue`
let timestampsOnly = argv['timestamps-only']? argv['timestamps-only'] : false;
let res = getArtistTitle(info.videoDetails.title,{
defaultArtist: "Unknown Artist",
@@ -41,7 +45,7 @@
});
let [artist, album] = res
artist = (info.videoDetails.media ? info.videoDetails.media.artist : artist)
let tracks = parse(info.videoDetails.description, {artist})
let tracks = parse(info.videoDetails.description, {artist,timestampsOnly})
generate({tracks, artist, audioFile, album}, output_file)
console.log(`"${output_file}" saved`)
})
@@ -128,6 +128,9 @@
return obj;
};
var fixDurations = function(list) {
for (let i in list) {
if (i == 0) {
@@ -149,7 +152,7 @@
}
};
export function parse(text, options = { artist: "Unknown" }) {
export function parse(text, options = { artist: "Unknown", timestampsOnly : false }) {
_options = options;
let durations = false;
let result = text
@@ -158,18 +161,21 @@
.map(firstPass)
.map(calcTimestamp);
result.forEach((current, index, list) => {
if (index > 0) {
let previous = list[index - 1];
if (current.start.calc < previous.start.calc) {
durations = true;
if (options.timestampsOnly == false) {
result.forEach((current, index, list) => {
if (index > 0) {
let previous = list[index - 1];
if (current.start.calc < previous.start.calc) {
durations = true;
}
}
}
});
});
if (durations) {
fixDurations(result);
if (durations) {
fixDurations(result);
}
}
return result
@@ -65,46 +65,74 @@
it("should parse timestamps with square brackets", function() {
let result = parse(`[00:00:00] 1. Steve Kroeger x Skye Holland - Through The Dark
[00:02:53] 2. Gabri Ponte x Jerome - Lonely `)
[00:02:53] 2. Gabri Ponte x Jerome - Lonely `);
assert.deepEqual(result[0], {
artist: "Steve Kroeger x Skye Holland",
title: "Through The Dark",
track: 1,
start: { ts: "00:00:00", hh: 0, mm: 0, ss: 0, calc: 0 },
end: { ts: "00:02:53", hh: 0, mm: 2, ss: 53, calc: 173 },
_: { left_text: "", right_text: "Steve Kroeger x Skye Holland - Through The Dark" },
})
artist: "Steve Kroeger x Skye Holland",
title: "Through The Dark",
track: 1,
start: { ts: "00:00:00", hh: 0, mm: 0, ss: 0, calc: 0 },
end: { ts: "00:02:53", hh: 0, mm: 2, ss: 53, calc: 173 },
_: {
left_text: "",
right_text: "Steve Kroeger x Skye Holland - Through The Dark",
},
});
});
it("should parse durations when given", function() {
let result = parse(`1. Artist - Title 6:19
2. Another Artist - Another Title 6:59
3. Yet Another Artist - Yet another title 5:12`)
3. Yet Another Artist - Yet another title 5:12`);
assert.deepEqual(result[0], {
artist: "Artist",
title: "Title",
track: 1,
start: { ts: "00:00:00", hh: 0, mm: 0, ss: 0, calc: 0 },
end: { ts: "00:06:19", hh: 0, mm: 6, ss: 19, calc: 379 },
_: { left_text: "Artist - Title", right_text: "" },
})
artist: "Artist",
title: "Title",
track: 1,
start: { ts: "00:00:00", hh: 0, mm: 0, ss: 0, calc: 0 },
end: { ts: "00:06:19", hh: 0, mm: 6, ss: 19, calc: 379 },
_: { left_text: "Artist - Title", right_text: "" },
});
assert.deepEqual(result[1], {
artist: "Another Artist",
title: "Another Title",
track: 2,
start: { ts: "00:06:19", hh: 0, mm: 6, ss: 19, calc: 379 },
end: { ts: "00:13:18", hh: 0, mm: 13, ss: 18, calc: 798 },
_: { left_text: "Another Artist - Another Title", right_text: "" },
})
artist: "Another Artist",
title: "Another Title",
track: 2,
start: { ts: "00:06:19", hh: 0, mm: 6, ss: 19, calc: 379 },
end: { ts: "00:13:18", hh: 0, mm: 13, ss: 18, calc: 798 },
_: { left_text: "Another Artist - Another Title", right_text: "" },
});
assert.deepEqual(result[2], {
artist: "Yet Another Artist",
title: "Yet another title",
track: 3,
start: { ts: "00:13:18", hh: 0, mm: 13, ss: 18, calc: 798 },
end: { ts: "00:18:30", hh: 0, mm: 18, ss: 30, calc: 1110 },
_: { left_text: "Yet Another Artist - Yet another title", right_text: "" },
})
artist: "Yet Another Artist",
title: "Yet another title",
track: 3,
start: { ts: "00:13:18", hh: 0, mm: 13, ss: 18, calc: 798 },
end: { ts: "00:18:30", hh: 0, mm: 18, ss: 30, calc: 1110 },
_: {
left_text: "Yet Another Artist - Yet another title",
right_text: "",
},
});
});
it("should parse durations as timestamps when forced", function() {
let result = parse(
`1. Artist - Title 5:00
2. Another Artist - Another Title 4:20`,
{ timestampsOnly: true }
);
assert.deepEqual(result[0].end, {
ts: "00:4:20",
hh: 0,
mm: 4,
ss: 20,
calc: 260,
});
assert.deepEqual(result[1].start, {
ts: "00:4:20",
hh: 0,
mm: 4,
ss: 20,
calc: 260,
});
});
it("should parse taylor swift", function() {