Youtube Video Id from URL - Swift3

Susanta Sahu

Basically I have a Youtube URL as string, I want to extract the video Id from that URL. I found some code in objective c that is as below:

NSError *error = NULL;
NSRegularExpression *regex = 
[NSRegularExpression regularExpressionWithPattern:@"?.*v=([^&]+)"
                                          options:NSRegularExpressionCaseInsensitive
                                            error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:youtubeURL
                                                options:0
                                                  range:NSMakeRange(0, [youtubeURL length])];
if (match) {
    NSRange videoIDRange = [match rangeAtIndex:1];
    NSString *substringForFirstMatch = [youtubeURL substringWithRange:videoIDRange];
}

When I am converting this code to swift3 that is:

var error: Error? = nil
var regex = try! NSRegularExpression(pattern: "?.*v=([^&]+)", options: .caseInsensitive)
var match = regex!.firstMatch(in: youtubeURL, options: [], range: NSRange(location: 0, length: youtubeURL.length))!
if match {
    var videoIDRange = match.rangeAt(1)
    var substringForFirstMatch = (youtubeURL as NSString).substring(with: videoIDRange)
}

Gives error as:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=2048 "The value “?.*v=([^&]+)” is invalid."

Can anybody help me about this error or anybody explain how to get video id from url in Swift 3.

Thanks in advance

totiG

I have a different way of doing this using URLComponents. You then just select the 'v' parameter from the url, if it exists.

func getYoutubeId(youtubeUrl: String) -> String? {
    return URLComponents(string: youtubeUrl)?.queryItems?.first(where: { $0.name == "v" })?.value
}

And then pass in a Youtube url like this:

print (getYoutubeId(youtubeUrl: "https://www.youtube.com/watch?v=Y7ojcTR78qE&spfreload=9"))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

How to get youtube video id from URL with java?

Regex to extract both video id or playlist id from youtube url

Play a video from Youtube in a AVPlayerViewController in Swift

Extract the video ID from youtube url in .net

How can I get a Youtube video ID from the Youtube API given the video url?

Youtube video url regex

How to get direct url of video from youtube url?

Swift3 video file upload along with one more form parameter(can be skipped from form and sent as url path)

Youtube API v3 get every Video ID from given channel

Access Youtube Video URL or ID in MediaWiki API

Flutter Dart : How to extract youtube video ID from video URL?

Youtube video URL extractor

Getting video id "Live Stream" from Youtube

Get youtube video id from youtube url in smarty template?

Fetch YouTube Video Id from youtube URL in zend framework

Retrieve video ids from playlist id - youtube api v3

Access YouTube Video URL With YouTube Video ID

Youtube video id (or URL) to "user" and "channel"

Embed Youtube video from URL

Stream YouTube video from extracted url

Get youtube video Id from shared video Url

youtube-dl + ffmpeg: Import description and URL of an original video to ID3 tags

Retrieve video ID from YouTube link

How do I parse YouTube URLs to get the video ID from a URL using Dart?

I need some help in correcting my codes in php - How to get the video id from a YouTube URL

YouTube Video Downloader with URL

youtube channel ID or name from URL

Is there solution "get youtube video ID from url" contains mobile web url?