The StreamExtractor and StreamInfo classes are the core components for extracting detailed information about videos, audio streams, and live streams from supported services.
Before extracting any data, you must initialize NewPipe with a downloader implementation:
import org.schabi.newpipe.extractor.NewPipe;import org.schabi.newpipe.extractor.localization.Localization;// Initialize with your downloaderNewPipe.init(yourDownloaderInstance);// Optional: Set localizationNewPipe.setupLocalization(Localization.fromLocale(Locale.US));
2
Extract Stream Info
Use StreamInfo.getInfo() to extract all available information from a stream URL:
StreamInfo info = StreamInfo.getInfo(url);StreamType type = info.getStreamType();switch (type) { case VIDEO_STREAM: System.out.println("Regular video stream"); break; case AUDIO_STREAM: System.out.println("Audio-only stream (e.g., SoundCloud)"); break; case LIVE_STREAM: System.out.println("Live stream"); // Note: duration will be 0 for live streams break; case AUDIO_LIVE_STREAM: System.out.println("Audio-only live stream"); break; case POST_LIVE_STREAM: System.out.println("Recorded live stream"); break; case NONE: System.out.println("Unknown stream type"); break;}
import org.schabi.newpipe.extractor.StreamingService;// Get service by nameStreamingService youtube = NewPipe.getService("YouTube");StreamInfo info = StreamInfo.getInfo(youtube, videoUrl);// Or get service by IDStreamingService service = NewPipe.getService(0); // YouTube = 0StreamInfo info2 = StreamInfo.getInfo(service, videoUrl);// Get all available servicesList<StreamingService> services = NewPipe.getServices();for (StreamingService s : services) { System.out.println(s.getServiceInfo().getName());}
Always handle exceptions when extracting stream data. Network issues, unavailable content, and parsing errors can occur.
import org.schabi.newpipe.extractor.exceptions.*;try { StreamInfo info = StreamInfo.getInfo(url); // Check for extraction errors List<Throwable> errors = info.getErrors(); if (!errors.isEmpty()) { System.out.println("Extraction completed with errors:"); for (Throwable error : errors) { System.err.println("- " + error.getMessage()); } }} catch (ContentNotAvailableException e) { System.err.println("Content not available: " + e.getMessage());} catch (GeographicRestrictionException e) { System.err.println("Content blocked in your region");} catch (AgeRestrictedContentException e) { System.err.println("Age-restricted content");} catch (ExtractionException e) { System.err.println("Extraction error: " + e.getMessage());} catch (IOException e) { System.err.println("Network error: " + e.getMessage());}
The StreamInfo class gracefully handles partial extraction failures. Check the getErrors() list to see which fields failed to extract while still accessing successfully extracted data.