Get YouTube Video ID

Dear Experts,

How can I extract the YouTube video ID from a YouTube URL, no matter what format the URL is in? Examples of URLs below:

www.youtube.com/watch?v=4S-N7xWWrbo&noredirect=1
www.youtube.com/watch?noredirect=1&v=4S-N7xWWrbo
www.youtube.com/watch?v=4S-N7xWWrbo

The ID would be “4S-N7xWWrbo”.

Thank you.

Hi,

check with text item delimiters for the occurrence of v=.
The ID is after v= up to the end or the next ampersand character

Used the below and it works…

set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "v="
set idCode to text item 2 of pageURL
set AppleScript's text item delimiters to oldDelims
if idCode contains "&" then
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "&"
	set idCode to text item 1 of idCode
	set AppleScript's text item delimiters to oldDelims
end if