UNPKG

newpipe-extractor-js

Version:
149 lines (127 loc) • 5.77 kB
#!/usr/bin/env ts-node import { initializeNewPipe, extractStreamInfo, getBestAudioStream, getBestVideoStream, isUrlSupported, setPreferredLocalization } from '../src/index'; async function main() { try { console.log('šŸš€ NewPipe Extractor JS - Basic Usage Example\n'); // Initialize the extractor console.log('1. Initializing NewPipe Extractor...'); initializeNewPipe(); // Set preferred localization setPreferredLocalization({ languageCode: 'en', countryCode: 'US' }); console.log('āœ… Initialized with English (US) localization\n'); // Test URLs const testUrls = [ 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', // Rick Astley - Never Gonna Give You Up 'https://youtu.be/dQw4w9WgXcQ', // Short YouTube URL 'dQw4w9WgXcQ', // Just the video ID 'https://www.example.com/not-supported' // Unsupported URL ]; // Check URL support console.log('2. Checking URL support:'); for (const url of testUrls) { const supported = isUrlSupported(url); console.log(` ${supported ? 'āœ…' : 'āŒ'} ${url}`); } console.log(); // Extract stream information const videoUrl = testUrls[0]; // Use the first supported URL console.log(`3. Extracting stream info for: ${videoUrl}`); try { const streamInfo = await extractStreamInfo(videoUrl); console.log('\nšŸ“ŗ Stream Information:'); console.log(` Title: ${streamInfo.name}`); console.log(` Uploader: ${streamInfo.uploaderName}`); console.log(` Duration: ${formatDuration(streamInfo.length)}`); console.log(` Views: ${formatNumber(streamInfo.viewCount)}`); console.log(` Upload Date: ${streamInfo.uploadDate || 'Unknown'}`); console.log(` Description: ${streamInfo.description?.substring(0, 100)}...`); console.log(` Tags: ${streamInfo.tags.slice(0, 5).join(', ')}`); // Audio streams console.log('\nšŸŽµ Audio Streams:'); if (streamInfo.audioStreams.length === 0) { console.log(' No audio streams found'); } else { console.log(` Found ${streamInfo.audioStreams.length} audio stream(s):`); streamInfo.audioStreams.forEach((stream, index) => { console.log(` ${index + 1}. ${stream.format} - ${stream.bitrate || 'Unknown'} bps - ${stream.quality || 'Unknown'} quality`); }); const bestAudio = getBestAudioStream(streamInfo); if (bestAudio) { console.log(`\n šŸ† Best Audio Stream:`); console.log(` Format: ${bestAudio.format}`); console.log(` Bitrate: ${bestAudio.bitrate || 'Unknown'} bps`); console.log(` Quality: ${bestAudio.quality || 'Unknown'}`); console.log(` URL: ${bestAudio.url?.substring(0, 80)}...`); } } // Video streams console.log('\nšŸŽ¬ Video Streams:'); if (streamInfo.videoStreams.length === 0) { console.log(' No combined video+audio streams found'); } else { console.log(` Found ${streamInfo.videoStreams.length} combined stream(s):`); streamInfo.videoStreams.forEach((stream, index) => { console.log(` ${index + 1}. ${stream.format} - ${stream.resolution || 'Unknown'} - ${stream.bitrate || 'Unknown'} bps`); }); const bestVideo = getBestVideoStream(streamInfo); if (bestVideo) { console.log(`\n šŸ† Best Video Stream:`); console.log(` Format: ${bestVideo.format}`); console.log(` Resolution: ${bestVideo.resolution || 'Unknown'}`); console.log(` Bitrate: ${bestVideo.bitrate || 'Unknown'} bps`); console.log(` URL: ${bestVideo.url?.substring(0, 80)}...`); } } // Video-only streams if (streamInfo.videoOnlyStreams.length > 0) { console.log('\nšŸ“¹ Video-Only Streams:'); console.log(` Found ${streamInfo.videoOnlyStreams.length} video-only stream(s):`); streamInfo.videoOnlyStreams.slice(0, 5).forEach((stream, index) => { console.log(` ${index + 1}. ${stream.format} - ${stream.resolution || 'Unknown'} - ${stream.bitrate || 'Unknown'} bps`); }); } // DASH/HLS manifests if (streamInfo.dashMpdUrl) { console.log(`\nšŸŽÆ DASH Manifest: ${streamInfo.dashMpdUrl.substring(0, 80)}...`); } if (streamInfo.hlsUrl) { console.log(`šŸ“ŗ HLS Manifest: ${streamInfo.hlsUrl.substring(0, 80)}...`); } console.log('\nāœ… Extraction completed successfully!'); } catch (error) { console.error('āŒ Extraction failed:', error instanceof Error ? error.message : 'Unknown error'); } } catch (error) { console.error('āŒ Error:', error instanceof Error ? error.message : 'Unknown error'); } } function formatDuration(seconds: number): string { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); const secs = seconds % 60; if (hours > 0) { return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`; } else { return `${minutes}:${secs.toString().padStart(2, '0')}`; } } function formatNumber(num: number): string { if (num >= 1000000) { return `${(num / 1000000).toFixed(1)}M`; } else if (num >= 1000) { return `${(num / 1000).toFixed(1)}K`; } else { return num.toString(); } } // Run the example if this file is executed directly if (require.main === module) { main().catch(console.error); }