Extract YouTube Video ID Extensions

This is my first Extension [Thanks for ChatGpt]

Extract YouTube Video IDs: This extension allows you to easily extract YouTube video IDs from YouTube video URLs. Use it to retrieve the unique identifiers of YouTube videos for further processing in your Niotron projects

Extension Download :

YouTubevideoID.aix (6.5 KB)

Java Script of Extensions :

package com.example.youtubeextension;

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;

import com.google.appinventor.components.runtime.util.YailList;

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

@DesignerComponent(
        version = 1,
        description = "Extract YouTube Video IDs: This extension allows you to easily extract YouTube video IDs from YouTube video URLs. Use it to retrieve the unique identifiers of YouTube videos for further processing in your Niotron projects",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "https://www.youtube.com/s/desktop/5519da25/img/favicon.ico")

@SimpleObject(external = true)

public class YouTubevideoID extends AndroidNonvisibleComponent {

    //Activity and Context
    private Context context;
    private Activity activity;

    public YouTubevideoID(ComponentContainer container){
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }

    @SimpleFunction(description = "Extracts the YouTube video ID from the provided URL.")
    public String ExtractYouTubeVideoID(String url) {
        String pattern = "(?:https?://)?(?:www\\.)?(?:youtu\\.be/|youtube\\.com(?:/embed/|/v/|/watch\\?v=|/watch\\?feature=player_embedded&v=|/watch\\?.*?&v=))(\\w{11})(?:[?&].*?)?$";
        Pattern compiledPattern = Pattern.compile(pattern);
        Matcher matcher = compiledPattern.matcher(url);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }

    @SimpleFunction(description = "Extracts YouTube video IDs from a list of URLs.")
    public YailList ExtractYouTubeVideoIDs(YailList urls) {
        java.util.List<Object> videoIds = new java.util.ArrayList<>();
        for (Object objUrl : urls.toArray()) {
            String url = (String) objUrl;
            String videoId = ExtractYouTubeVideoID(url);
            videoIds.add(videoId);
        }
        return YailList.makeList(videoIds);
    }
}

Extensions Blocks :

If an extension developer adds any further improvements to this code, please provide the updated extension for upload here.

2 Likes