Kategoriler
Kodlama

Youtube video ID almak JS

JavaScript ile Youtube video ID’lerini almak için örnekler.

Sayfaya embed edilen veya tarayıcıda açık olan video veya shorts videosunun ID kodunu almak için JavaScript ile aşağıdaki örneği kullanabilirsiniz.

Regex basitçe şöyle:

/^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/

Yukarıdaki regex ile basit fonksiyon

function getVideoId(url){
	let regex = /(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))/gm;
	return regex.exec(url)[3];
}

Farklı bir fonksiyon ve kullanım:

const urls = ['http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel',
	'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub',
	'http://youtu.be/6dwqZw0j_jY',
	'http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be',
	'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub',
	'http://www.youtube.com/embed/nas1rJpm7wY?rel=0',
	'http://www.youtube.com/watch?v=peFZbP64dsU',
	'http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player',
	'http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
	'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
	'http://youtu.be/afa-5HQHiAs',
	'http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player',
	'//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0',
	'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo',
	'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I',
	'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY',
	'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0',
	'http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel',
	'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I',
	'http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player',
	'http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
	'http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player',
	'http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player'
]

const getVideoId = (url) =>{
	if (/^(?:http:\/\/)?youtu\.be/.test(url)){
		return url.match(/^(?:http:\/\/)?youtu\.be\/(.{11})/)[1]
	}else if (/^(?:(?:http:)?\/\/)?(?:www\.)?youtube(?:-nocookie)?.com/.test(url)){
		let match = url.match(/(?:http:\/\/)?(?:www\.)?youtube\.com.*?v=(.{11})/)
		if (!match) match = url.match(/(?:vi=|vi?\/|embed\/)(.{11})/)
		if (!match && /user/.test(url)){
			match = url.split('/')
			return match[match.length - 1].split('?')[0]
		}
		return (match || [, url])[1]
	}else return null
}

for (let url of urls){
	console.log(url)
	console.log(getVideoId(url))
}

Sayfa içerisindeki iframe’nin src URL’ini şöyle alabilirsiniz:

const iframe = document.getElementById('iframe_id');
iframe.addEventListener('load', evt =>{
	const loc = iframe.contentWindow.location;
	try{
		loc.href;
	}catch (e){
		if (e.name === 'SecurityError'){
			console.log(iframe.src);
		}
	}
});

veya:

let x = document.getElementById('iframe_id').src;

Kolay gelsin.