🏡 index : github.com/captn3m0/youtube-cue.git

author Nemo <commits@captnemo.in> 2021-07-21 13:30:42.0 +05:30:00
committer Nemo <commits@captnemo.in> 2021-07-21 13:30:42.0 +05:30:00
commit
10981c62ce7c8fbbf513f93ea448e6f6e217d661 [patch]
tree
75a84756c7dc71518e45531f0f4d9abecd91fae8
parent
b0fd29cbdb998b8f67d7d3f0807d627f5ff21f37
download
10981c62ce7c8fbbf513f93ea448e6f6e217d661.tar.gz

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(-)

diff --git a/index.js b/index.js
index 6f59533..a8e0da3 100755
--- a/index.js
+++ a/index.js
@@ -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`)
  })
diff --git a/src/parser.js b/src/parser.js
index 2ab6f0d..36609b3 100644
--- a/src/parser.js
+++ a/src/parser.js
@@ -128,6 +128,9 @@
  return obj;
};

// Instead of timestamps, some tracklists use durations
// If durations are provided, use them to re-calculate
// the starting and ending timestamps
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) {
    // If our timestamps are not in increasing order
    // Assume that we've been given a duration list instead
    result.forEach((current, index, list) => {
      if (index > 0) {
        let previous = list[index - 1];
        if (current.start.calc < previous.start.calc) {
          durations = true;
        }
      }
    }
  });
    });

  if (durations) {
    // console.error("Detected durations instead of timestamps. \nIf this is incorrect, pass the --timestamps-only flag and create an issue at \nhttps://github.com/captn3m0/youtube-cue/issues/new/choose")
    fixDurations(result);
    if (durations) {
      fixDurations(result);
    }
  }

  return result
diff --git a/test/parser_test.js b/test/parser_test.js
index eaa875f..27fd294 100644
--- a/test/parser_test.js
+++ a/test/parser_test.js
@@ -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() {