9

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

Alan Moore
  • 68,531
  • 11
  • 88
  • 149
Susanta Sahu
  • 105
  • 1
  • 4

8 Answers8

40

Safer version (without force unwrapping !):

extension String {
    var youtubeID: String? {
        let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"

        let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive)
        let range = NSRange(location: 0, length: count)

        guard let result = regex?.firstMatch(in: self, range: range) else {
            return nil
        }

        return (self as NSString).substring(with: result.range)
    }
}

Examples:

"https://www.youtube.com/watch?v=C0DPdy98e4c".youtubeID // "C0DPdy98e4c"
"https://youtube.com/watch?v=C0DPdy98e4c".youtubeID // "C0DPdy98e4c"
"www.youtube.com/watch?v=C0DPdy98e4c".youtubeID // "C0DPdy98e4c"
"youtube.com/watch?v=C0DPdy98e4c".youtubeID // "C0DPdy98e4c"

"https://youtu.be/C0DPdy98e4c".youtubeID // "C0DPdy98e4c"
"youtu.be/C0DPdy98e4c".youtubeID // "C0DPdy98e4c"

Credits: Usman Nisar's answer

Islam Q.
  • 3,343
  • 2
  • 26
  • 37
20

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"))
totiDev
  • 4,759
  • 3
  • 27
  • 28
  • single line `return URLComponents(string: youtubeUrl)?.queryItems?.first(where: { $0.name == "v" })?.value` – Leo Dabus Dec 15 '16 at 14:44
  • 1
    How would you handle short urls? (Example: "https://youtu.be/sWx8TtRBOfk") – Islam Q. Nov 17 '17 at 03:08
  • @IslamQ. This would work: URLComponents(string: "http://youtu.be/sWx8TtRBOfk")?.path.replacingOccurrences(of: "/", with: "") – totiDev Nov 17 '17 at 08:52
  • 1
    @IslamQ., check [this](https://stackoverflow.com/a/44986877/1603234) answer for your question. – Hemang Oct 04 '18 at 08:35
5

Here is code to extract youtube video id from any youtube url: (Swift)

func extractYoutubeId(fromLink link: String) -> String {
        let regexString: String = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
        let regExp = try? NSRegularExpression(pattern: regexString, options: .caseInsensitive)
        let array: [Any] = (regExp?.matches(in: link, options: [], range: NSRange(location: 0, length: (link.characters.count ))))!
        if array.count > 0 {
            let result: NSTextCheckingResult? = array.first as? NSTextCheckingResult
            return (link as NSString).substring(with: (result?.range)!)
        }

        return ""
    }
Usman Nisar
  • 2,801
  • 29
  • 39
4

A Swift 4 version using the elegant flatMap:

func extractYouTubeId(from url: String) -> String? {
    let typePattern = "(?:(?:\\.be\\/|embed\\/|v\\/|\\?v=|\\&v=|\\/videos\\/)|(?:[\\w+]+#\\w\\/\\w(?:\\/[\\w]+)?\\/\\w\\/))([\\w-_]+)"
    let regex = try? NSRegularExpression(pattern: typePattern, options: .caseInsensitive)
    return regex
        .flatMap { $0.firstMatch(in: url, range: NSMakeRange(0, url.count)) }
        .flatMap { Range($0.range(at: 1), in: url) }
        .map { String(url[$0]) }
}

This method uses a regex that detects most of the possible YouTube URL formats (.be/*, /embed/, /v/ - you can find the full list here).

Pietro Basso
  • 1,093
  • 8
  • 11
1

Your first problem is you are not escaping ? in your expression. ? is reserved character and if you want to use it in your expressions, you must escape with \ and since \ is used also to escape " character you must escape ? with double backslash something like \\?. So according to above information, following code correctly extracts the videoId

let youtubeURL = "https://www.youtube.com/watch?v=uH8o-JTHJdM"
let regex = try! NSRegularExpression(pattern: "\\?.*v=([^&]+)", options: .caseInsensitive)
let match = regex.firstMatch(in: youtubeURL, options: [], range: NSRange(location: 0, length: youtubeURL.characters.count))
if let videoIDRange = match?.rangeAt(1) {
    let substringForFirstMatch = (youtubeURL as NSString).substring(with: videoIDRange)
} else {
    //NO video URL
}
Meanteacher
  • 1,783
  • 3
  • 13
  • 42
1

Some samples of Youtube's url:

let urls: [String] = [
    "www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0",
    "http://www.youtube.com/watch?v=peFZbP64dsU",
    "http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel",
    "http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player",
    "http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player",
    "http://youtu.be/6dwqZw0j_jY",
    "http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_playe",
    "http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player",
    "http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player",
    "http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player",
    "http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0",
    "http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY",
    "1p3vcRhsY02"
]

My extension based on Islam Q. solution:

private extension String {
    var youtubeID: String? {
        let pattern = "((?<=(v|V|vi)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=vi=)|(?<=/u/[0-9_]/)|(?<=embed/))([\\w-]++)"
        let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive)
        let range = NSRange(location: 0, length: count)

        guard let result = regex?.firstMatch(in: self, range: range) else {
            return count == 11 ? self : nil
        }

        let id = (self as NSString).substring(with: result.range)
        return id.count == 11 ? id : nil
    }
}
1

Swift 5

var youtubeURLs = [
    "http://www.youtube.com/watch?v=-wtIMTCHWuI",
    "http://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1",
    "http://youtu.be/-wtIMTCHWuI",
    "http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json",
    "https://youtu.be/uJ2PZaO1N5E",
    "https://www.youtube.com/embed/M7lc1UVf-VE",
    "http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare",
    "https://www.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail"
]

func getVideoID(from urlString: String) -> String? {
    guard let url = urlString.removingPercentEncoding else { return nil }
    do {
        let regex = try NSRegularExpression.init(pattern: "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)", options: .caseInsensitive)
        let range = NSRange(location: 0, length: url.count)
        if let matchRange = regex.firstMatch(in: url, options: .reportCompletion, range: range)?.range {
            let matchLength = (matchRange.lowerBound + matchRange.length) - 1
            if range.contains(matchRange.lowerBound) &&
                range.contains(matchLength) {
                let start = url.index(url.startIndex, offsetBy: matchRange.lowerBound)
                let end = url.index(url.startIndex, offsetBy: matchLength)
                return String(url[start...end])
            }
        }
    } catch {
        print(error.localizedDescription)
    }
    return nil
}

for url in youtubeURLs {
    print("Video id: \(getVideoID(from: url) ?? "NA") for url: \(url)")
}

Result:

Video id: -wtIMTCHWuI for url: http://www.youtube.com/watch?v=-wtIMTCHWuI
Video id: -wtIMTCHWuI for url: http://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1
Video id: -wtIMTCHWuI for url: http://youtu.be/-wtIMTCHWuI
Video id: -wtIMTCHWuI for url: http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json
Video id: uJ2PZaO1N5E for url: https://youtu.be/uJ2PZaO1N5E
Video id: M7lc1UVf-VE for url: https://www.youtube.com/embed/M7lc1UVf-VE
Video id: EhxJLojIE_o for url: http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare
Video id: yZv2daTWRZU for url: https://www.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail
arjavlad
  • 369
  • 5
  • 14
0

To get video Id from Youtube Url, use code # Swift4 :

var videoId = ""

    if youtubeLink.lowercased().contains("youtu.be"){
            linkString = youtubeLink
            if let range = linkString.range(of: "be/"){
                videoId = youtubeLink[range.upperBound...].trimmingCharacters(in: .whitespaces)
            }
        }
        else if youtubeLink.lowercased().contains("youtube.com"){
            linkString = youtubeLink
            if let range = linkString.range(of: "?v="){
                videoId = youtubeLink[range.upperBound...].trimmingCharacters(in: .whitespaces)
            }
        }

Hope will be helping! :)

JaspreetKour
  • 607
  • 8
  • 9