问题
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?
回答1:
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
回答2:
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);
回答3:
You can try using my library, youtube-dl-android, which lets you to easily extract video link from 1000+ sites including YouTube, Facebook, etc.
Step 1: Add jitpack repository to your project build file
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Step 2: Add the dependency
dependencies {
implementation 'com.github.bawaviki.youtube-dl-android:library:0.7.+'
}
Step 3: Use Youtube-dl instance
YoutubeDLRequest request = new YoutubeDLRequest("http://youtube.com/watch?v=xxxx");
request.setOption("-o", youtubeDLDir.getAbsolutePath() + "/%(title)s.%(ext)s");
YoutubeDL.getInstance().execute(request, (progress, etaInSeconds) -> {
System.out.println(String.valueOf(progress) + "% (ETA " + String.valueOf(etaInSeconds) + " seconds)");
});
来源:https://stackoverflow.com/questions/22806431/how-to-download-youtube-video-in-android-sd-card-programmatically