Files
play-dl-test/play-dl/YouTube/classes/Video.ts

224 lines
5.6 KiB
TypeScript
Raw Normal View History

2021-09-27 22:20:50 +05:30
import { YouTubeChannel } from './Channel';
2021-11-18 15:38:25 +05:30
import { YouTubeThumbnail } from './Thumbnail';
2021-08-12 15:58:55 +05:30
interface VideoMusic {
song?: string;
artist?: string;
album?: string;
writers?: string;
license?: string;
}
2021-08-12 13:28:17 +05:30
interface VideoOptions {
2021-11-18 15:38:25 +05:30
/**
* YouTube Video ID
*/
2021-11-23 09:56:08 +05:30
id?: string;
/**
* YouTube video url
*/
url: string;
/**
* YouTube Video title
*/
title?: string;
/**
* YouTube Video description.
*/
description?: string;
/**
* YouTube Video Duration Formatted
*/
durationRaw: string;
/**
* YouTube Video Duration in seconds
*/
durationInSec: number;
/**
* YouTube Video Uploaded Date
*/
uploadedAt?: string;
/**
* If the video is upcoming or a premiere that isn't currently live, this will contain the premiere date, for watch page playlists this will be true, it defaults to undefined
*/
upcoming?: Date | true;
2021-11-23 09:56:08 +05:30
/**
* YouTube Views
*/
views: number;
/**
* YouTube Thumbnail Data
*/
thumbnail?: {
2021-09-17 14:36:32 +05:30
width: number | undefined;
2021-08-13 13:16:34 +05:30
height: number | undefined;
2021-08-12 15:58:55 +05:30
url: string | undefined;
};
2021-11-23 09:56:08 +05:30
/**
* YouTube Video's uploader Channel Data
*/
channel?: YouTubeChannel;
/**
* YouTube Video's likes
*/
likes: number;
/**
* YouTube Video live status
*/
live: boolean;
/**
* YouTube Video private status
*/
private: boolean;
/**
* YouTube Video tags
*/
tags: string[];
/**
* `true` if the video has been identified by the YouTube community as inappropriate or offensive to some audiences and viewer discretion is advised
*/
discretionAdvised?: boolean;
/**
* Gives info about music content in that video.
*/
music?: VideoMusic[];
2021-08-12 13:28:17 +05:30
}
2021-09-29 20:23:16 +05:30
/**
* Class for YouTube Video url
*/
2021-09-27 22:20:50 +05:30
export class YouTubeVideo {
2021-11-18 15:38:25 +05:30
/**
* YouTube Video ID
*/
2021-08-12 13:28:17 +05:30
id?: string;
2021-11-18 15:38:25 +05:30
/**
* YouTube video url
*/
2021-09-27 22:20:50 +05:30
url: string;
2021-11-18 15:38:25 +05:30
/**
* YouTube Class type. == "video"
*/
2021-09-27 22:20:50 +05:30
type: 'video' | 'playlist' | 'channel';
2021-11-18 15:38:25 +05:30
/**
* YouTube Video title
*/
2021-08-12 13:28:17 +05:30
title?: string;
2021-11-18 15:38:25 +05:30
/**
* YouTube Video description.
*/
2021-08-12 13:28:17 +05:30
description?: string;
2021-11-18 15:38:25 +05:30
/**
* YouTube Video Duration Formatted
*/
2021-08-23 15:22:18 +05:30
durationRaw: string;
2021-11-18 15:38:25 +05:30
/**
* YouTube Video Duration in seconds
*/
2021-08-23 15:22:18 +05:30
durationInSec: number;
2021-11-18 15:38:25 +05:30
/**
* YouTube Video Uploaded Date
*/
2021-08-12 13:28:17 +05:30
uploadedAt?: string;
/**
* If the video is upcoming or a premiere that isn't currently live, this will contain the premiere date, for watch page playlists this will be true, it defaults to undefined
*/
upcoming?: Date | true;
2021-11-18 15:38:25 +05:30
/**
* YouTube Views
*/
2021-08-12 13:28:17 +05:30
views: number;
2021-11-18 15:38:25 +05:30
/**
* YouTube Thumbnail Data
*/
thumbnails: YouTubeThumbnail[];
2021-11-18 15:38:25 +05:30
/**
* YouTube Video's uploader Channel Data
*/
2021-09-27 22:20:50 +05:30
channel?: YouTubeChannel;
2021-11-18 15:38:25 +05:30
/**
* YouTube Video's likes
*/
2021-08-12 13:28:17 +05:30
likes: number;
2021-11-18 15:38:25 +05:30
/**
* YouTube Video live status
*/
2021-08-12 13:28:17 +05:30
live: boolean;
2021-11-18 15:38:25 +05:30
/**
* YouTube Video private status
*/
2021-08-12 13:28:17 +05:30
private: boolean;
2021-11-18 15:38:25 +05:30
/**
* YouTube Video tags
*/
2021-08-12 13:28:17 +05:30
tags: string[];
/**
* `true` if the video has been identified by the YouTube community as inappropriate or offensive to some audiences and viewer discretion is advised
*/
discretionAdvised?: boolean;
/**
* Gives info about music content in that video.
*/
music?: VideoMusic[];
2021-11-18 15:38:25 +05:30
/**
2021-11-18 15:48:33 +05:30
* Constructor for YouTube Video Class
2021-11-18 15:38:25 +05:30
* @param data JSON parsed data.
*/
2021-09-17 14:36:32 +05:30
constructor(data: any) {
if (!data) throw new Error(`Can not initiate ${this.constructor.name} without data`);
2021-08-12 13:28:17 +05:30
this.id = data.id || undefined;
2021-09-17 14:36:32 +05:30
this.url = `https://www.youtube.com/watch?v=${this.id}`;
2021-09-27 22:20:50 +05:30
this.type = 'video';
2021-08-12 13:28:17 +05:30
this.title = data.title || undefined;
this.description = data.description || undefined;
2021-09-17 14:36:32 +05:30
this.durationRaw = data.duration_raw || '0:00';
2021-08-23 15:22:18 +05:30
this.durationInSec = (data.duration < 0 ? 0 : data.duration) || 0;
2021-08-12 13:28:17 +05:30
this.uploadedAt = data.uploadedAt || undefined;
this.upcoming = data.upcoming;
2021-08-12 13:28:17 +05:30
this.views = parseInt(data.views) || 0;
const thumbnails = [];
for (const thumb of data.thumbnails) {
thumbnails.push(new YouTubeThumbnail(thumb));
}
this.thumbnails = thumbnails || [];
2021-11-04 18:50:19 +01:00
this.channel = new YouTubeChannel(data.channel) || {};
2021-11-27 14:32:43 +01:00
this.likes = data.likes || 0;
2021-08-12 13:28:17 +05:30
this.live = !!data.live;
this.private = !!data.private;
this.tags = data.tags || [];
this.discretionAdvised = data.discretionAdvised ?? undefined;
this.music = data.music || [];
2021-08-12 13:28:17 +05:30
}
2021-11-18 15:38:25 +05:30
/**
* Converts class to title name of video.
* @returns Title name
*/
toString(): string {
2021-09-17 14:36:32 +05:30
return this.url || '';
2021-08-12 13:28:17 +05:30
}
2021-11-18 15:38:25 +05:30
/**
* Converts class to JSON data
* @returns JSON data.
*/
toJSON(): VideoOptions {
2021-08-12 13:28:17 +05:30
return {
id: this.id,
url: this.url,
title: this.title,
description: this.description,
2021-08-23 15:22:18 +05:30
durationInSec: this.durationInSec,
durationRaw: this.durationRaw,
2021-08-12 13:28:17 +05:30
uploadedAt: this.uploadedAt,
thumbnail: this.thumbnails[this.thumbnails.length - 1].toJSON() || this.thumbnails,
2021-11-04 18:50:19 +01:00
channel: this.channel,
2021-08-12 13:28:17 +05:30
views: this.views,
tags: this.tags,
2021-11-18 15:38:25 +05:30
likes: this.likes,
2021-08-12 13:28:17 +05:30
live: this.live,
private: this.private,
discretionAdvised: this.discretionAdvised,
music: this.music
2021-08-12 13:28:17 +05:30
};
}
2021-09-17 14:36:32 +05:30
}