Skip to content

Commit

Permalink
feat: add videoDetails.chapters (#913)
Browse files Browse the repository at this point in the history
* feat: add `videoDetails.chapters`

closes #625

* docs(example): Update `info.json`

* doc(typings): Add `videoDetails.chapters`
  • Loading branch information
skick1234 authored May 4, 2021
1 parent 67eb6dd commit 838de41
Show file tree
Hide file tree
Showing 8 changed files with 21,060 additions and 2,677 deletions.
6,171 changes: 3,495 additions & 2,676 deletions example/info.json

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions lib/info-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,30 @@ exports.getStoryboards = info => {
};
});
};

/**
* Get chapters info.
*
* @param {Object} info
* @returns {Array.<Object>}
*/
exports.getChapters = info => {
const playerOverlayRenderer = info.response &&
info.response.playerOverlays &&
info.response.playerOverlays.playerOverlayRenderer;
const playerBar = playerOverlayRenderer &&
playerOverlayRenderer.decoratedPlayerBarRenderer &&
playerOverlayRenderer.decoratedPlayerBarRenderer.decoratedPlayerBarRenderer &&
playerOverlayRenderer.decoratedPlayerBarRenderer.decoratedPlayerBarRenderer.playerBar;
const markersMap = playerBar &&
playerBar.multiMarkersPlayerBarRenderer &&
playerBar.multiMarkersPlayerBarRenderer.markersMap;
const marker = Array.isArray(markersMap) && markersMap.find(m => m.value && Array.isArray(m.value.chapters));
if (!marker) return [];
const chapters = marker.value.chapters;

return chapters.map(chapter => ({
title: getText(chapter.chapterRenderer.title),
start_time: chapter.chapterRenderer.timeRangeStartMillis / 1000,
}));
};
3 changes: 2 additions & 1 deletion lib/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ exports.getBasicInfo = async(id, options) => {

// Add additional properties to info.
const media = extras.getMedia(info);
let additional = {
const additional = {
author: extras.getAuthor(info),
media,
likes: extras.getLikes(info),
Expand All @@ -82,6 +82,7 @@ exports.getBasicInfo = async(id, options) => {
// Give the standard link to the video.
video_url: BASE_URL + id,
storyboards: extras.getStoryboards(info),
chapters: extras.getChapters(info),
};

info.videoDetails = extras.cleanVideoDetails(Object.assign({},
Expand Down
6 changes: 6 additions & 0 deletions test/files/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ const videos = [
type: 'longest-upload',
basicInfo: true,
},
{
id: 'W6NZfCO5SIk',
type: 'chapters',
basicInfo: true,
saveInfo: true,
},
];


Expand Down
17,433 changes: 17,433 additions & 0 deletions test/files/videos/chapters/expected-info.json

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions test/files/videos/chapters/watch.html

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions test/info-extras-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,26 @@ describe('extras.getStoryboards()', () => {
});
});
});

describe('extras.getChapters()', () => {
it('Returns chapters', () => {
const info = require('./files/videos/chapters/expected-info.json');
const chapters = extras.getChapters(info);

assert.ok(Array.isArray(chapters) && chapters.length);

for (const chapter of chapters) {
assert.ok(chapter.title);
assert.number(chapter.start_time);
}
});

describe('With no chapters', () => {
it('Returns empty array', () => {
const info = infoFromWatchJSON('regular');
const chapters = extras.getChapters(info);

assert.ok(Array.isArray(chapters) && !chapters.length);
});
});
});
6 changes: 6 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ declare module 'ytdl-core' {
storyboardCount: number;
}

interface Chapter {
title: string;
start_time: number;
}

interface MoreVideoDetails extends Omit<VideoDetails, 'author' | 'thumbnail' | 'shortDescription'>, Omit<MicroformatRenderer, 'title' | 'description'> {
published: number;
video_url: string;
Expand All @@ -222,6 +227,7 @@ declare module 'ytdl-core' {
author: Author;
thumbnails: thumbnail[];
storyboards: storyboard[];
chapters: Chapter[];
description: string | null;
}

Expand Down

0 comments on commit 838de41

Please sign in to comment.