How to Get Facebook Video id From url Using Java?

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

Here’s an example:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FacebookVideoUrlParser {

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

// Create a Pattern object
Pattern r = Pattern.compile(pattern);

// Create matcher object
Matcher m = r.matcher(url);

// If a match is found, return the video ID; otherwise, return null
return m.find() ? m.group(1) : null;
}

public static void main(String[] args) {

String facebookVideoUrl = "https://www.facebook.com/facebook/videos/123456789/";
String videoId = getFacebookVideoId(facebookVideoUrl);

if (videoId != null) {
System.out.println("Facebook Video ID: " + videoId);
} else {
System.out.println("Invalid Facebook Video URL");
}
}
}

This Java program defines a `getFacebookVideoId` method that takes a Facebook video URL as input and returns the video ID using a regular expression. The `main` method provides an example usage with a Facebook video URL.

Make sure to test the code with different Facebook video URLs to ensure it covers the variations you expect. Keep in mind that web scraping approaches like this one may break if Facebook changes the structure of their URLs or page content. If Facebook provides an API for retrieving video information, that would be a more reliable solution.