Skip to content

Commit 0fab030

Browse files
committed
refactor(content-utils): replace @distube/ytdl-core with youtubei.js for improved YouTube video info retrieval
1 parent 11a9d8a commit 0fab030

File tree

4 files changed

+50
-105
lines changed

4 files changed

+50
-105
lines changed

.husky/pre-commit

Lines changed: 0 additions & 4 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
"migration:up": "prisma migrate deploy",
1313
"migration:make": "prisma migrate dev"
1414
},
15-
"lint-staged": {
16-
"*.ts": "eslint --fix"
17-
},
1815
"author": "qlaffont",
1916
"license": "ISC",
2017
"devDependencies": {
@@ -39,7 +36,6 @@
3936
"typescript": "5.3.3"
4037
},
4138
"dependencies": {
42-
"@distube/ytdl-core": "^4.16.12",
4339
"@gquittet/graceful-server": "^4.0.8",
4440
"@prisma/client": "^5.10.2",
4541
"@socket.io/postgres-emitter": "^0.1.0",
@@ -63,6 +59,7 @@
6359
"tslib": "^2.6.2",
6460
"unify-errors": "^1.2.202",
6561
"unify-fastify": "^1.4.24",
62+
"youtubei.js": "^15.1.1",
6663
"zlib-sync": "^0.1.9",
6764
"zod": "^3.22.4",
6865
"zod-validation-error": "^3.0.1"

pnpm-lock.yaml

Lines changed: 24 additions & 87 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/content-utils.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import fetch from 'node-fetch';
33
import { getVideoDurationInSeconds } from 'get-video-duration';
44
import { fileTypeFromBuffer } from 'file-type';
55
import mime from 'mime-types';
6-
import ytdl from '@distube/ytdl-core';
6+
import { Innertube } from 'youtubei.js';
7+
import { env } from './env';
78

89
function getFileTypeWithRegex(url) {
910
const regex = /(?:\.([^.]+))?$/; // Regular expression to capture file extension
@@ -50,16 +51,30 @@ export const getContentInformationsFromUrl = async (url: string) => {
5051

5152
//if it is a youtube video, get the duration from the url
5253
if (url.includes('youtube.com') || url.includes('youtu.be')) {
53-
let agent;
54-
55-
//if file exist on root of app, use it
56-
if (fs.existsSync(env.YTDL_COOKIE_PATH)) {
57-
agent = ytdl.createAgent(JSON.parse(fs.readFileSync(env.YTDL_COOKIE_PATH, 'utf8')));
54+
try {
55+
// Create Innertube instance with optional cookie data
56+
const options: any = {};
57+
58+
//if file exist on root of app, use it
59+
if (fs.existsSync(env.YTDL_COOKIE_PATH)) {
60+
const cookieData = JSON.parse(fs.readFileSync(env.YTDL_COOKIE_PATH, 'utf8'));
61+
options.cookie = cookieData;
62+
}
63+
64+
const innertube = await Innertube.create(options);
65+
const info = await innertube.getInfo(url);
66+
67+
// Get duration from basic_info
68+
if (info.basic_info?.duration) {
69+
mediaDuration = info.basic_info.duration;
70+
}
71+
72+
// Check if it's crawlable (similar to isCrawlable)
73+
mediaIsShort = info.basic_info?.is_crawlable ?? true;
74+
contentType = 'video/mp4';
75+
} catch (error) {
76+
console.error('Error getting YouTube video info:', error);
5877
}
59-
const info = await ytdl.getInfo(url, { agent });
60-
mediaDuration = info.videoDetails.lengthSeconds;
61-
mediaIsShort = info.videoDetails.isCrawlable;
62-
contentType = 'video/mp4';
6378
}
6479

6580
return { contentType, mediaDuration, mediaIsShort };

0 commit comments

Comments
 (0)