How to Get Facebook Video id From url Using Swift Script Code?

Hello Friends Today, through this tutorial, I will tell you How to Get Facebook Video id From url Using Swift Script Code?.To extract the video ID from a Facebook video URL in Swift, you can use regular expressions. Here’s an example:

import Foundation

func getFacebookVideoId(from url: String) -> String? {

// Define the regular expression pattern to match Facebook video URLs
let pattern = "(?:https?://)?(?:www\\.)?(?:facebook\\.com/.*/videos/|facebook\\.com/video\\.php\\?v=)(\\d+)(?:\\S+)?"
do {

// Create a regular expression object
let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)

// Search for the pattern in the input URL
let matches = regex.matches(in: url, options: [], range: NSRange(location: 0, length: url.utf16.count))

// If a match is found, return the video ID; otherwise, return nil
if let match = matches.first {
let range = Range(match.range(at: 1), in: url)
return { String(url[$0]) }
} else {
return nil
} catch {
print("Error creating regular expression: \(error)")
return nil

// Example usage
let facebookVideoUrl = ""
if let videoId = getFacebookVideoId(from: facebookVideoUrl) {

print("Facebook Video ID: \(videoId)")
} else {
print("Invalid Facebook Video URL")

This Swift function `getFacebookVideoId` takes a Facebook video URL as input and returns the video ID using a regular expression. The example usage demonstrates how to call this function with a Facebook video URL.

Please note that regular expressions for URL parsing might not cover all edge cases and variations, so it’s essential to test the function with different Facebook video URLs. Additionally, if Facebook provides an API for retrieving video information, using that API would be a more robust solution.