Android based library that allows you to download or play audio and video from Youtube, in other words - youtube-dl for android
GPL-2.0 License
YoutubeJExtractor is Android extractor library that allows you to extract video and audio from any youtube video along with some other data such as a video title, description, author, thumbnails and others.
This library was initially created for my android app Youtube audio player
Subtitles extraction implemented
Bug fixes
youtubeJExtractor.extract(videoId, new JExtractorCallback() {
@Override
public void onSuccess(YoutubeVideoData videoData) {
// use extracted data
}
@Override
public void onNetworkException(YoutubeRequestException e) {
// may be a connection problem, ask user to check his internet connection
}
@Override
public void onError(Exception exception) {
// some serious problem occured, just show some error message
}
});
Minor changes, increased stability and logging is slightly improved
Muxed streams are now supported! Thanks to @comptoost for enchancement request
OkHttpClient
instance via the following one argument constructor - YoutubeJExtractor(OkHttpClient client)
. It could be usefull for region restricted video (via creating OkHttpClient
instance with proxy).executeWithRetry(...)
method - now every http call will be executed up to 3 times before YoutubeRequestException
throw, it will increase stability. YoutubeJExtractor youtubeJExtractor = new YoutubeJExtractor();
YoutubeVideoData videoData;
try {
videoData = youtubeJExtractor.extract(videoId);
}
catch (ExtractionException e) {
// Something really bad happened, nothing we can do except just show some error notification to the user
}
catch (YoutubeRequestException e) {
// Possibly there are some connection problems, ask user to check the internet connection and then retry
}
YoutubeVideoData is an object that contains data for the requested video split across two main objects: VideoDetails and StreamingData.
To get all the video streams:
List<AdaptiveVideoStream> videoStreamsList = videoData.getStreamingData().getAdaptiveVideoStreams()
Each StreamItem object contains fields that describe the stream such as:
Check AdaptiveVideoStream.class
and AdaptiveAudioStream.class
for the details.
Live streams are also supported by YoutubeJextractor, but you have to treat them differently than the regular videos, to play live content you have to use DASH or HLS manifests. Your code will look like this:
YoutubeVideoData videoData = youtubeJExtractor.extract("stream_video_id");
if (videoData.getVideoDetails().isLiveContent()) {
String dashManifest = videoData.getStreamingData().getDashManifestUrl();
// or use HLS manifest via getHlsManifestUrl() method
}
else {
...
}
Then you have to decide how to deal with manifest url, it depends on how you're gonna play media content, for instance, if you are using ExoPlayer, please refer to the DASH and HLS guides.
Subtitles extraction is very simple:
youtubeJExtractor.extractSubtitles(videoId)
This line will return a map where key is language and value is a list of subtitle lines with time codes. **Note that autogenerated subtitles are not currently supported.
Muxed stream is a stream that contains video and audio at the same time. My library extracts muxed streams along with demuxed that contain only video or audio.
No, it's not, my library extracts everything what youtube provides for the specific video, but for some youtube's internal reasons they have different set of streams available for each video, sometimes there are muxed streams sometimes there aren't. But it's not a problem because you can take separately audio and video stream and then combine them into a single, muxed stream, with any quality you want!
Depends on how you're gonna play your streams, for instance if you're using ExoPlayer (what I personally recommend for media playback in andoird) you need to take a look at MergingMediaSource.class:
MediaSource videoSource = new ProgressiveMediaSource.Factory(...)
.createMediaSource(videoUri);
MediaSource audioSource = new ProgressiveMediaSource.Factory(...)
.createMediaSource(audioUri);
MergingMediaSource mergedSource = new MergingMediaSource(videoSource, audioSource);
Works on API 19+.
Youtube-dl - the idea and implementation were influenced by Youtube-dl
Distributed under the GPL v2 License. See LICENSE.md for terms and conditions.