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
- Extracts video and audio streams from Youtube videos. Supports both adaptive and muxed streams.
- Extracts various video data like title, description, view count, channel id, etc.
- Supports age restricted videos.
- Supports videos with restricted embedding.
- Supports regon restricted videos (through a proxy).
- Supports HLS and DASH live streams.
- Supports subtitles extraction
Subtitles extraction implemented
Bug fixes
- Fixed NPE #28, thanks @niteshfel for reporting
- Implemented new overloaded extract method that takes callback as parameter:
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
}
});
Older versions
Minor changes, increased stability and logging is slightly improved
- Code updated to match latest youtube changes
- Improved age restricted videos detection
Muxed streams are now supported! Thanks to @comptoost for enchancement request
- Possibility to use YoutubeJExtractor with custom
OkHttpClient
instance via the following one argument constructor -YoutubeJExtractor(OkHttpClient client)
. It could be usefull for region restricted video (via creatingOkHttpClient
instance with proxy). - Implemented RequestExecutor class with
executeWithRetry(...)
method - now every http call will be executed up to 3 times beforeYoutubeRequestException
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.
- VideoDetails contains various video data such as title, description, author, rating, view count, etc.
- StreamingData contains two fields with the lists of adaptive streams (video and audio), *muxedStreams contains as the name implies muxed streams (streams that contain both audio and video) dashManifestUrl and hlsManifestUrl fields which are contains links to the DASH and HLS manifests (if you dealing with a live stream) and expiresInSeconds which indicates how long links will be alive.
To get all the video streams:
List<AdaptiveVideoStream> videoStreamsList = videoData.getStreamingData().getAdaptiveVideoStreams()
Each StreamItem object contains fields that describe the stream such as:
- it's extension (like mp4, ogg, etc),
- codec, bitrate, url and many others.
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.
Q: I need to play both video and audio at the same time, but your library gives me no muxed streams (or they have lower quality), is it a bug of the library?
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.