How to download Youtube video in android SD card programmatically

前端 未结 3 1926
眼角桃花
眼角桃花 2020-12-16 06:10

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 pla

3条回答
  •  粉色の甜心
    2020-12-16 06:35

    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 mExtractionCallback = new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                bindVideoResult(response.body());
            }
    
            @Override
            public void onFailure(Call 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 ytFiles) {
                if (ytFiles != null) {
                    int itag = 22;
    // Here you can get download url
                    String downloadUrl = ytFiles.get(itag).getUrl();
                }
            }
        };
    
        ytEx.execute(youtubeLink);
    

提交回复
热议问题