Download Youtube Videos From Link Android Studio Project With sourcecode

What does it take to create an object that travels at 1% the speed of light?
SHARE

A set of mobile payment and checkout screens designed in Adobe XD. The set contains both light and dark screens that can be used to create a shopping payment app

First Add dependencies in your project

implementation 'com.github.HaarigerHarald:android-youtubeExtractor:master-SNAPSHOT'

Now Add maven { url "https://jitpack.io" } link in build gradel(Project)

{
                 repositories {
                 google()
                 jcenter()
                 maven { url "https://jitpack.io" }
                }
              }

AndroidMenifest.xml

<uses-permission android:name="android.permission.INTERNET" />

Now Add Editbox where user can add Youtube link and one button to download that video from link

Android_activity.xml

"1.0" encoding="utf-8"?>
                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".Youtube">

  <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textSize="20sp"
      android:inputType="textPersonName"
      android:layout_marginTop="20dp"
      android:padding="10dp"
      android:id="@+id/link"/>

  <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="40dp"
      android:padding="10dp"
      android:onClick="downloadVideo"
      android:layout_marginLeft="40dp"
      android:layout_marginRight="40dp"
      android:text="Download"
      android:textSize="28sp"
      android:textColor="#13711F" />

MainActivity.class

package com.anyjson.earn.flashdownloader;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.SparseArray;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import at.huber.youtubeExtractor.VideoMeta;
import at.huber.youtubeExtractor.YouTubeExtractor;
import at.huber.youtubeExtractor.YtFile;

public class Youtube extends AppCompatActivity {
    EditText editText;
    String newlink;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youtube);
        editText = findViewById(R.id.link);

    }

    public void downloadVideo(View view) {
        String values = editText.getText().toString();
        YouTubeExtractor youTubeExtractor = new YouTubeExtractor(Youtube.this) {
            @Override
            protected void onExtractionComplete(@Nullable SparseArray<YtFile> ytFiles, @Nullable VideoMeta videoMeta) {
                if (ytFiles != null) {
                    int tag;
                    if (ytFiles.get(137) != null) { // 1080p MP4 format
                        tag = 137;
                    } else if (ytFiles.get(22) != null) { // 720p MP4 format
                        tag = 22;
                    } else if (ytFiles.get(135) != null) { // 480p MP4 format
                        tag = 135;
                    } else {
                        // No suitable format found
                        Toast.makeText(Youtube.this, "Sorry, the video could not be downloaded.", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    newlink = ytFiles.get(tag).getUrl();
                    String title = "MyVideo";
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(newlink));
                    request.setTitle(title);
                    request.setDescription("Downloading...");
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, title + ".mp4");
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    downloadManager.enqueue(request);
                }
            }
        };
        youTubeExtractor.execute(values);
    }
}

The Parker Solar Probe

Code explanation

This is an Android Java class named Youtube that extends AppCompatActivity. It defines a method called downloadVideo which is called when the user taps on a button in the corresponding layout file. This method extracts the download URL of a YouTube video from a user-provided link, and then downloads the video using Android's DownloadManager service.

Here's a detailed explanation of the code:

  • The class defines two instance variables: an EditText named editText to hold the user-provided YouTube link, and a String named newlink to hold the URL of the downloaded video.
  • In the onCreate method, the editText variable is initialized with a reference to an EditText widget in the layout file using its ID R.id.link.
  • The downloadVideo method is called when the user taps on a button in the layout file. It first gets the text entered by the user in the editText widget and converts it to a string using getText().toString().
  • A new instance of the YouTubeExtractor class is created with Youtube.this as its context. This class is used to extract the video download URL from the YouTube link entered by the user. It overrides the onExtractionComplete method to handle the result of the extraction process.
  • In the overridden onExtractionComplete method, the extracted video URL is passed as a SparseArray of YtFile objects, along with some metadata about the video. The method checks whether a suitable format is available for downloading the video. It does this by looking for specific video format tags (137 for 1080p MP4, 22 for 720p MP4, 135 for 480p MP4). If a suitable format is found, its download URL is stored in the newlink variable.
  • If no suitable format is found, a toast message is displayed to the user, indicating that the video could not be downloaded.
  • A new instance of the DownloadManager.Request class is created with the video URL stored in newlink. This class is used to configure the download request, such as setting the title, description, and destination directory of the downloaded file.
  • The DownloadManager.Request object is passed to the enqueue method of a new DownloadManager object, which is created using getSystemService(Context.DOWNLOAD_SERVICE). This starts the download process.
  • When the download is complete, a notification is displayed to the user indicating that the download is complete. The notification includes the title of the downloaded video and a link to the file in the downloads directory.