Skip to content

Commit

Permalink
fix: fix getting correct videoDetails.lengthSeconds when video is t…
Browse files Browse the repository at this point in the history
…oo long

closes #856
  • Loading branch information
fent committed Jan 12, 2021
1 parent 6a9ca4f commit 15e7fac
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/info-extras.js
Expand Up @@ -268,9 +268,10 @@ exports.getDislikes = info => {
* Cleans up a few fields on `videoDetails`.
*
* @param {Object} videoDetails
* @param {Object} info
* @returns {Object}
*/
exports.cleanVideoDetails = videoDetails => {
exports.cleanVideoDetails = (videoDetails, info) => {
videoDetails.thumbnails = videoDetails.thumbnail.thumbnails;
delete videoDetails.thumbnail;
utils.deprecate(videoDetails, 'thumbnail', { thumbnails: videoDetails.thumbnails },
Expand All @@ -279,6 +280,11 @@ exports.cleanVideoDetails = videoDetails => {
delete videoDetails.shortDescription;
utils.deprecate(videoDetails, 'shortDescription', videoDetails.description,
'videoDetails.shortDescription', 'videoDetails.description');

// Use more reliable `lengthSeconds` from `playerMicroformatRenderer`.
videoDetails.lengthSeconds =
info.player_response.microformat &&
info.player_response.microformat.playerMicroformatRenderer.lengthSeconds;
return videoDetails;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/info.js
Expand Up @@ -87,7 +87,7 @@ exports.getBasicInfo = async(id, options) => {
info.videoDetails = extras.cleanVideoDetails(Object.assign({},
info.player_response && info.player_response.microformat &&
info.player_response.microformat.playerMicroformatRenderer,
info.player_response && info.player_response.videoDetails, additional));
info.player_response && info.player_response.videoDetails, additional), info);

return info;
};
Expand Down
12 changes: 12 additions & 0 deletions test/basic-info-test.js
Expand Up @@ -195,6 +195,18 @@ describe('ytdl.getBasicInfo()', () => {
});
});

describe('From the longest video uploaded', () => {
it('Gets correct `lengthSeconds`', async() => {
const id = 'TceijYjxdrQ';
const scope = nock(id, 'longest-upload');
let info = await ytdl.getBasicInfo(id);
scope.done();
assert.ok(info);
assert.ok(info.videoDetails);
assert.equal(info.videoDetails.lengthSeconds, '805000');
});
});

describe('With cookie headers', () => {
const id = '_HSylqgVYQI';
describe('`x-youtube-identity-token` given', () => {
Expand Down
5 changes: 5 additions & 0 deletions test/files/refresh.js
Expand Up @@ -107,6 +107,11 @@ const videos = [
basicInfo: true,
saveInfo: true,
},
{
id: 'TceijYjxdrQ',
type: 'longest-upload',
basicInfo: true,
},
];


Expand Down
67 changes: 67 additions & 0 deletions test/files/videos/longest-upload/watch.html

Large diffs are not rendered by default.

0 comments on commit 15e7fac

Please sign in to comment.