aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/YoutubeUtils.hx
blob: 0bb8016b11cabe48a87a68098e06009f0ef0392e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package utils;

typedef YoutubeVideoDetails = {
	viewCount:String,
	videoId:String,
	title:String,
	thumbnail:{
		thumbnails:Array<{
			url:String,
			width:Int,
			height:Int,
		}>
	},
	shortDescription:String,
	lengthSeconds:String,
	keywords:Array<String>,
	isUnpluggedCorpus:Bool,
	isPrivate:Bool,
	isOwnerViewing:Bool,
	isLiveContent:Bool,
	isCrawlable:Bool,
	channelId:String,
	author:String,
	allowRatings:Bool
}

typedef YoutubeVideoFormat = {
	?signatureCipher:String,
	itag:Int,
	width:Int,
	height:Int,
	url:String,
	qualityLabel:String, // 240p, 1080p, etc
	quality:String,
	projectionType:String,
	mimeType:String,
	lastModified:String,
	bitrate:Int,
	approxDurationMs:String,
	?initRange:{start:Int, end:Int},
	?indexRange:{start:Int, end:Int},
	?audioQuality:String, // AUDIO_QUALITY_LOW
	?audioSampleRate:Int,
	?audioChannels:Int,

	?container:String,
	?videoCodec:String,
	?audioCodec:String,
}

typedef YouTubeVideoInfo = {
	public var videoDetails:YoutubeVideoDetails;
	public var ?formats:Array<YoutubeVideoFormat>;
	public var ?adaptiveFormats:Array<YoutubeVideoFormat>;
	public var ?liveData:{
		manifestUrl:String,
	};
}

class YoutubeUtils {
	static final matchId = ~/youtube\.com.*v=([A-z0-9_-]+)/;
	static final matchShort = ~/youtu\.be\/([A-z0-9_-]+)/;
	static final matchShorts = ~/youtube\.com\/shorts\/([A-z0-9_-]+)/;
	static final matchEmbed = ~/youtube\.com\/embed\/([A-z0-9_-]+)/;
	static final matchPlaylist = ~/youtube\.com.*list=([A-z0-9_-]+)/;

	public static function extractVideoId(url:String):String {
		if (matchId.match(url)) {
			return matchId.matched(1);
		}
		if (matchShort.match(url)) {
			return matchShort.matched(1);
		}
		if (matchShorts.match(url)) {
			return matchShorts.matched(1);
		}
		if (matchEmbed.match(url)) {
			return matchEmbed.matched(1);
		}
		return "";
	}

	public static function extractPlaylistId(url:String):String {
		if (!matchPlaylist.match(url)) return "";
		return matchPlaylist.matched(1);
	}
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage