Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube Video ID From URL - Swift or Objective-C

Tags:

I have a Youtube url as an NSString or Swift String, but I need to extract the video id that is displayed in the url. I found many tutorials on how to do this in php or and other web-based programming languages, but none in Objective-C or Swift for Apple platforms...

I'm looking for a method that asks for an NSString url as the parameter and returns the video id as another NSString...

like image 232
The Man Avatar asked Jul 16 '12 17:07

The Man


People also ask

How do I get the YouTube video ID from a URL?

The video ID will be located in the URL of the video page, right after the v= URL parameter. In this case, the URL of the video is: https://www.youtube.com/watch?v=aqz-KE-bpKQ. Therefore, the ID of the video is aqz-KE-bpKQ .

Are YouTube video ids case sensitive?

The logic is change the last letter with the consecutive letter, the letters are case sensitive. So if the last letter is 'a' change it to 'b', if 'A' change it to 'B', if '1' change it to '2'.

How does YouTube video ID work?

As Scott explains, YouTube's video ID system, which can be seen in any URL that leads to one of the site's uploads, uses a 64-character language. That means, for each of the 11 spaces in one Video ID, there are 64 different characters that can be used.

What is the video ID on YouTube?

A YouTube Video ID is a unique ID to identify a video which is uploaded to YouTube. A YouTube video ID is used to create a unique URL to show the video and can be used to embed a YouTube video on any website. It's not possible to change a video ID of a video.


2 Answers

Here is RegExp it cover these cases

Objective C

- (NSString *)extractYoutubeIdFromLink:(NSString *)link {     NSString *regexString = @"((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)";     NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:regexString                                                                             options:NSRegularExpressionCaseInsensitive                                                                               error:nil];      NSArray *array = [regExp matchesInString:link options:0 range:NSMakeRange(0,link.length)];     if (array.count > 0) {         NSTextCheckingResult *result = array.firstObject;         return [link substringWithRange:result.range];     }     return nil; } 

Swift

func extractYoutubeIdFromLink(link: String) -> String? {     let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"     guard let regExp = try? NSRegularExpression(pattern: pattern, options: .CaseInsensitive) else {         return nil     }     let nsLink = link as NSString     let options = NSMatchingOptions(rawValue: 0)     let range = NSRange(location: 0,length: nsLink.length)     let matches = regExp.matchesInString(link as String, options:options, range:range)     if let firstMatch = matches.first {         return nsLink.substringWithRange(firstMatch.range)     }     return nil } 

Swift 3

func extractYoutubeIdFromLink(link: String) -> String? {     let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"     guard let regExp = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {         return nil     }     let nsLink = link as NSString     let options = NSRegularExpression.MatchingOptions(rawValue: 0)     let range = NSRange(location: 0, length: nsLink.length)     let matches = regExp.matches(in: link as String, options:options, range:range)     if let firstMatch = matches.first {         return nsLink.substring(with: firstMatch.range)     }     return nil } 
like image 125
Alex Avatar answered Nov 15 '22 12:11

Alex


After spending ages trying to find the correct syntax for the regex, I've come across this which has helped me.

NSString *regexString = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"; 

Taken from here. This works for the following URL formats:

 - www.youtube.com/v/VIDEOID    - www.youtube.com?v=VIDEOID  - http://www.youtube.com/watch?v=KFPtWedl7wg&feature=youtu.be  - http://www.youtube.com/watch?v=MkTD2Y4LXcM   - youtu.be/KFPtWedl7wg_U923  - http://www.youtube.com/watch?feature=player_detailpage&v=biVLGTAMC_U#t=31s 
like image 45
jt_uk Avatar answered Nov 15 '22 11:11

jt_uk