Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for chapters & video heatmap #263

Merged
merged 2 commits into from
Dec 27, 2022

Conversation

LuanRT
Copy link
Owner

@LuanRT LuanRT commented Dec 27, 2022

Description

This adds support for chapters & heatmap. Here are a few examples of how this can be used:

Chapters:

import { Innertube, UniversalCache } from 'youtubei.js';

(async () => {
  const yt = await Innertube.create({ cache: new UniversalCache() });

  const info = await yt.getInfo('5DrSU72hOUQ');

  const markers_map = info.player_overlays?.decorated_player_bar?.player_bar?.markers_map;

  const chapters = (
    markers_map?.get({ marker_key: 'AUTO_CHAPTERS' }) ||
    markers_map?.get({ marker_key: 'DESCRIPTION_CHAPTERS' })
  )?.value.chapters;

  if (chapters) {
    console.log('Chapters:\n');
    for (const chapter of chapters) {
      console.log(chapter.title.toString(), ' - Starts in ', Math.round(chapter.time_range_start_millis / 1000), 'seconds');
    }
  }
})();

Output:

Chapters:

Intro  - Starts in  0 seconds
Paul and Morgan  - Starts in  125 seconds
My Comment  - Starts in  1763 seconds
Outro  - Starts in  1851 seconds

Heatmap:

import { Innertube, UniversalCache } from 'youtubei.js';

(async () => {
  const yt = await Innertube.create({ cache: new UniversalCache() });

  const info = await yt.getInfo('5DrSU72hOUQ');

  const markers_map = info.player_overlays?.decorated_player_bar?.player_bar?.markers_map;
  const heatmap = markers_map?.get({ marker_key: 'HEATSEEKER' })?.value.heatmap;

  if (heatmap) {
    const heatmap_columns = heatmap.heat_markers.map((marker) => '█'.repeat(Math.round(marker.heat_marker_intensity_score_normalized * 10) + 1).split(''));

    let last_col_index = heatmap_columns.reduce((prev, curr) => {
      if (curr.length > prev.length) {
        return curr;
      }
      return prev;
    }).length;

    console.log('\nHeatmap:\n', '-'.repeat(heatmap_columns.length + 2));

    // Draw the heatmap horizontally
    while (last_col_index !== -1) {
      let row = '';

      for (const col of heatmap_columns) {
        row += col[last_col_index] || ' ';
      }

      console.log('|', row, '|', last_col_index + 1);

      row = '';

      last_col_index--;
    }
  }
})();

Output:

Heatmap:
 ------------------------------------------------------------------------------------------------------
|                                                                                                      | 12
|            █                                                                              █          | 11
|            █                                                                              █          | 10
| █          █                                                                              █          | 9
| █          ██                                                                             █          | 8
| █          ██                                              ███      █                     █          | 7
| █       █ ███                                              ███      █                     █          | 6
| █      ██ ███                                     █        ███      █                     █          | 5
| ██    ███ ███   █                                 █        ███      █                     █          | 4
| ██    ███████  ██                   █            ██ █     █████     █                    ██    █     | 3
| ████ █████████████           █████ ████████████████████ ███████    ██   █   █ █     ██   ██  █ ███   | 2
| ████████████████████████████████████████████████████████████████████████████████████████████████████ | 1

Related: yt-dlp/yt-dlp#3888

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have checked my code and corrected any misspellings

@github-actions github-actions bot added enhancement New feature or request medium-diff parser labels Dec 27, 2022
@github-actions github-actions bot added the tests label Dec 27, 2022
@LuanRT LuanRT marked this pull request as ready for review December 27, 2022 07:13
@LuanRT LuanRT merged commit 6a4b4f3 into main Dec 27, 2022
@LuanRT LuanRT deleted the feat/heatmap-and-chapters branch December 27, 2022 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant