@@ -3,7 +3,8 @@ import fetch from 'node-fetch';
33import { getVideoDurationInSeconds } from 'get-video-duration' ;
44import { fileTypeFromBuffer } from 'file-type' ;
55import mime from 'mime-types' ;
6- import ytdl from '@distube/ytdl-core' ;
6+ import { Innertube } from 'youtubei.js' ;
7+ import { env } from './env' ;
78
89function 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