Skip to content

Commit 838de41

Browse files
authored
feat: add videoDetails.chapters (#913)
* feat: add `videoDetails.chapters` closes #625 * docs(example): Update `info.json` * doc(typings): Add `videoDetails.chapters`
1 parent 67eb6dd commit 838de41

File tree

8 files changed

+21060
-2677
lines changed

8 files changed

+21060
-2677
lines changed

example/info.json

+3,495-2,676
Large diffs are not rendered by default.

lib/info-extras.js

+27
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,30 @@ exports.getStoryboards = info => {
336336
};
337337
});
338338
};
339+
340+
/**
341+
* Get chapters info.
342+
*
343+
* @param {Object} info
344+
* @returns {Array.<Object>}
345+
*/
346+
exports.getChapters = info => {
347+
const playerOverlayRenderer = info.response &&
348+
info.response.playerOverlays &&
349+
info.response.playerOverlays.playerOverlayRenderer;
350+
const playerBar = playerOverlayRenderer &&
351+
playerOverlayRenderer.decoratedPlayerBarRenderer &&
352+
playerOverlayRenderer.decoratedPlayerBarRenderer.decoratedPlayerBarRenderer &&
353+
playerOverlayRenderer.decoratedPlayerBarRenderer.decoratedPlayerBarRenderer.playerBar;
354+
const markersMap = playerBar &&
355+
playerBar.multiMarkersPlayerBarRenderer &&
356+
playerBar.multiMarkersPlayerBarRenderer.markersMap;
357+
const marker = Array.isArray(markersMap) && markersMap.find(m => m.value && Array.isArray(m.value.chapters));
358+
if (!marker) return [];
359+
const chapters = marker.value.chapters;
360+
361+
return chapters.map(chapter => ({
362+
title: getText(chapter.chapterRenderer.title),
363+
start_time: chapter.chapterRenderer.timeRangeStartMillis / 1000,
364+
}));
365+
};

lib/info.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ exports.getBasicInfo = async(id, options) => {
7272

7373
// Add additional properties to info.
7474
const media = extras.getMedia(info);
75-
let additional = {
75+
const additional = {
7676
author: extras.getAuthor(info),
7777
media,
7878
likes: extras.getLikes(info),
@@ -82,6 +82,7 @@ exports.getBasicInfo = async(id, options) => {
8282
// Give the standard link to the video.
8383
video_url: BASE_URL + id,
8484
storyboards: extras.getStoryboards(info),
85+
chapters: extras.getChapters(info),
8586
};
8687

8788
info.videoDetails = extras.cleanVideoDetails(Object.assign({},

test/files/refresh.js

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ const videos = [
112112
type: 'longest-upload',
113113
basicInfo: true,
114114
},
115+
{
116+
id: 'W6NZfCO5SIk',
117+
type: 'chapters',
118+
basicInfo: true,
119+
saveInfo: true,
120+
},
115121
];
116122

117123

test/files/videos/chapters/expected-info.json

+17,433
Large diffs are not rendered by default.

test/files/videos/chapters/watch.html

+68
Large diffs are not rendered by default.

test/info-extras-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,26 @@ describe('extras.getStoryboards()', () => {
281281
});
282282
});
283283
});
284+
285+
describe('extras.getChapters()', () => {
286+
it('Returns chapters', () => {
287+
const info = require('./files/videos/chapters/expected-info.json');
288+
const chapters = extras.getChapters(info);
289+
290+
assert.ok(Array.isArray(chapters) && chapters.length);
291+
292+
for (const chapter of chapters) {
293+
assert.ok(chapter.title);
294+
assert.number(chapter.start_time);
295+
}
296+
});
297+
298+
describe('With no chapters', () => {
299+
it('Returns empty array', () => {
300+
const info = infoFromWatchJSON('regular');
301+
const chapters = extras.getChapters(info);
302+
303+
assert.ok(Array.isArray(chapters) && !chapters.length);
304+
});
305+
});
306+
});

typings/index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ declare module 'ytdl-core' {
212212
storyboardCount: number;
213213
}
214214

215+
interface Chapter {
216+
title: string;
217+
start_time: number;
218+
}
219+
215220
interface MoreVideoDetails extends Omit<VideoDetails, 'author' | 'thumbnail' | 'shortDescription'>, Omit<MicroformatRenderer, 'title' | 'description'> {
216221
published: number;
217222
video_url: string;
@@ -222,6 +227,7 @@ declare module 'ytdl-core' {
222227
author: Author;
223228
thumbnails: thumbnail[];
224229
storyboards: storyboard[];
230+
chapters: Chapter[];
225231
description: string | null;
226232
}
227233

0 commit comments

Comments
 (0)