Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download Youtube video in android SD card programmatically

I have YouTube link "https://www.youtube.com/watch?v=ySuYTKkgIvg"

How can I store this video from YouTube to my SD card?

I can play video using YouTube player by android YouTube DATA API but don't know how to download it is any API / code who can help me to do this?

like image 845
Android is everything for me Avatar asked Apr 02 '14 09:04

Android is everything for me


People also ask

How can I download YouTube video with code?

Go to the video URL and add “ss” before the “youtube.com…” and click enter. You will be directed to another page where you will save the video. This page is the parent website of ssyoutube.com, known as savefrom.net. This page will display all the information regarding the YouTube video you want to download.

How can I download YouTube videos to my Android phone for free 2022?

You can use YouTube downloaders for Android like TubeMate or VidMate. These apps allow you to download YouTube videos in one click, and you can also use them to download YouTube videos on your iPhone and computer too.


2 Answers

Youtube API allows you to search & list videos and obtain the mediaplayer URL, so that you can play videos within your web page.

Look here for downloading video from Youtube

Youtube Video Data API

Youtube Java Data API - Getting Started

It does not allow you to download the byte content of videos - because Google is protecting their own rights and the rights of the content creators.

Youtube Google API Terms of Service

like image 62
Lavekush Agrawal Avatar answered Sep 24 '22 19:09

Lavekush Agrawal


I am late but this solution is best for me just try this.

METHOD 1

Add into your gradle file

 allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }

And dependencies

 compile 'com.github.Commit451.YouTubeExtractor:youtubeextractor:2.1.0'

Add this small code and you done. Demo HERE

public class MainActivity extends AppCompatActivity {

    private static final String YOUTUBE_ID = "ea4-5mrpGfE";

    private final YouTubeExtractor mExtractor = YouTubeExtractor.create();


    private Callback<YouTubeExtractionResult> mExtractionCallback = new Callback<YouTubeExtractionResult>() {
        @Override
        public void onResponse(Call<YouTubeExtractionResult> call, Response<YouTubeExtractionResult> response) {
            bindVideoResult(response.body());
        }

        @Override
        public void onFailure(Call<YouTubeExtractionResult> call, Throwable t) {
            onError(t);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        For android youtube extractor library  com.github.Commit451.YouTubeExtractor:youtubeextractor:2.1.0'
        mExtractor.extract(YOUTUBE_ID).enqueue(mExtractionCallback);

    }


    private void onError(Throwable t) {
        t.printStackTrace();
        Toast.makeText(MainActivity.this, "It failed to extract. So sad", Toast.LENGTH_SHORT).show();
    }


    private void bindVideoResult(YouTubeExtractionResult result) {

//        Here you can get download url link
        Log.d("OnSuccess", "Got a result with the best url: " + result.getBestAvailableQualityVideoUri());

        Toast.makeText(this, "result : " + result.getSd360VideoUri(), Toast.LENGTH_SHORT).show();
    }
}

You can get download link in bindVideoResult() method.

METHOD 2

Using this library android-youtubeExtractor

Add into gradle file

repositories {
    maven { url "https://jitpack.io" }
}

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

Here is the code for getting download url.

       String youtubeLink = "http://youtube.com/watch?v=xxxx";

    YouTubeUriExtractor ytEx = new YouTubeUriExtractor(this) {
        @Override
        public void onUrisAvailable(String videoId, String videoTitle, SparseArray<YtFile> ytFiles) {
            if (ytFiles != null) {
                int itag = 22;
// Here you can get download url
                String downloadUrl = ytFiles.get(itag).getUrl();
            }
        }
    };

    ytEx.execute(youtubeLink);
like image 23
Arpit Patel Avatar answered Sep 26 '22 19:09

Arpit Patel