mebox-extractor
Version:
🎬 A powerful and type-safe video metadata extractor for YouTube and Bilibili platforms with full TypeScript support
202 lines (195 loc) • 5.62 kB
TypeScript
/**
* List of supported video platforms
*/
declare const SUPPORTED_WEBSITES: readonly ["youtube", "bilibili"];
/**
* Supported website type
*/
type Website = (typeof SUPPORTED_WEBSITES)[number];
/**
* Individual subtitle item with timing and text
*/
interface SubTitleItem {
start: number;
end: number;
text: string;
}
/**
* Array of subtitle items
*/
type SubTitles = SubTitleItem[];
/**
* Download format with MIME type and URL
*/
interface DownloadFormat {
mimeType: string;
url: string;
}
/**
* Complete video metadata structure
*/
interface VideoMetadata {
website: Website;
videoId: string;
url: string;
downloadURL: string | [DownloadFormat, DownloadFormat];
previewURL: string;
fps: number;
title: string;
description: string;
thumbnail: string;
duration: number;
subtitles?: SubTitles;
}
/**
* Video metadata schema (alias for VideoMetadata)
*/
interface VideoMetadataSchema {
website: Website;
videoId: string;
url: string;
downloadURL: string | [DownloadFormat, DownloadFormat];
previewURL: string;
fps: number;
title: string;
description: string;
thumbnail: string;
duration: number;
subtitles?: SubTitles;
}
/**
* Supported video resolutions
*/
type Resolution = '4k' | '1080p' | '720p' | '360p';
/**
* Options for video extraction
*/
interface ExtractorOptions {
resolution?: Resolution;
cookies?: Record<string, string>;
}
/**
* Browser-related utilities for URL parsing and website detection
*/
/**
* Converts a URL to a website key by matching hostname patterns
* @param url - The URL to analyze
* @returns The website key if supported, undefined otherwise
*/
declare const convertURLToWebsiteKey: (url?: string) => Website | undefined;
/**
* Extracts video ID from a video URL
* @param url - The video URL to extract ID from
* @returns The video ID
* @throws Error if website is not supported or video ID cannot be found
*/
declare const getVideoIdByURL: (url: string) => string;
/**
* Bilibili video view information structure
*/
interface VideoViewInfo {
aid: number;
cid: number;
desc: string;
title: string;
duration: number;
pic: string;
}
/**
* Bilibili subtitle information structure
*/
interface Subtitle {
lan_doc: string;
subtitle_url: string;
}
/**
* Bilibili API client for video operations
*/
declare class BilibiliClient {
private cookies;
private apiClient;
/**
* Creates a new Bilibili client
* @param cookies - Authentication cookies
*/
constructor(cookies?: Record<string, string>);
/**
* Checks if user is logged in based on SESSDATA cookie
*/
get isLogin(): boolean;
/**
* Fetches video URL based on login status
* @param bvid - Bilibili video ID
* @param cid - Video CID
* @param resolution - Desired resolution
* @param isPreview - Whether this is for preview
* @returns Video URL and quality info
*/
private fetchVideo;
/**
* Gets WBI signing keys from Bilibili API
* @returns Object containing img_key and sub_key for WBI signing
*/
getWbiKeys(): Promise<{
img_key: any;
sub_key: any;
}>;
/**
* Signs API parameters using WBI algorithm
* @param params - Parameters to sign
* @returns Signed query string
*/
signParams(params: Record<string, string | number>): Promise<string>;
/**
* Gets basic video information
* @param bvid - Bilibili video ID
* @returns Video view information
*/
getInfo(bvid: string): Promise<VideoViewInfo>;
/**
* Gets available subtitles for a video
* @param aid - Video AID
* @param cid - Video CID
* @returns Array of available subtitles
*/
getSubtitles(aid: number, cid: number): Promise<Subtitle[]>;
/**
* Gets preview URL for a video
* @param bvid - Bilibili video ID
* @param cid - Video CID
* @returns Preview URL and quality info
*/
getPreviewURL(bvid: string, cid: number): Promise<{
url: any;
quality: any;
}>;
getLowResolutionDownloadURL(bvid: string, cid: number): Promise<{
url: any;
quality: any;
}>;
getHighResolutionDownloadURL(bvid: string, cid: number, resolution: Resolution, isPreview?: boolean): Promise<{
url: any;
quality: any;
}>;
/**
* Gets download URL for a video with quality validation
* @param bvid - Bilibili video ID
* @param cid - Video CID
* @param resolution - Desired resolution
* @returns Download URL
*/
getDownloadURL(bvid: string, cid: number, resolution: Resolution): Promise<any>;
}
/**
* Extracts video metadata from supported platforms (YouTube and Bilibili)
* @param url - The video URL to extract metadata from
* @param options - Optional extraction options including resolution and cookies
* @returns Promise resolving to video metadata
* @throws Error if the website is not supported or video ID cannot be found
*/
declare const extract: (url: string, options?: ExtractorOptions) => Promise<VideoMetadataSchema>;
/**
* Alias for the main extract function
*/
declare const extractVideoMetadata: (url: string, options?: ExtractorOptions) => Promise<VideoMetadataSchema>;
export { BilibiliClient, type DownloadFormat, type ExtractorOptions, type Resolution, type SubTitleItem, type SubTitles, type VideoMetadata, type VideoMetadataSchema, type Website, convertURLToWebsiteKey, extract as default, extract, extractVideoMetadata, getVideoIdByURL };