Last active
December 5, 2025 07:22
-
-
Save bowber/1542968520be913301cded1a39f53f12 to your computer and use it in GitHub Desktop.
YouTube Music Player - Search and play music from YouTube sequentially
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| YouTube Music Player - Search and play music from YouTube sequentially | |
| Dependencies: | |
| - yt-dlp: pip install yt-dlp | |
| - mpv: sudo pacman -S mpv (or apt install mpv) | |
| - mpv-mpris: sudo pacman -S mpv-mpris (for top bar metadata badge) | |
| Installation: | |
| 1. Save this script as 'listen' (without .py extension) | |
| 2. Make it executable: chmod +x listen | |
| 3. Move to PATH: mv listen ~/.local/bin/ OR sudo mv listen /usr/local/bin/ | |
| 4. Run: listen --search "song name" | |
| Usage: listen --search "song name" | |
| """ | |
| import sys | |
| import subprocess | |
| import json | |
| import os | |
| import time | |
| def get_youtube_info(query): | |
| """Get video info from YouTube""" | |
| try: | |
| result = subprocess.run([ | |
| 'yt-dlp', | |
| '-f', 'bestaudio', | |
| '--dump-json', | |
| f'ytsearch:{query}' | |
| ], capture_output=True, text=True, timeout=30) | |
| if result.returncode != 0: | |
| return None | |
| return json.loads(result.stdout.strip()) | |
| except: | |
| return None | |
| def get_youtube_playlist(query, count=5): | |
| """Get multiple videos from YouTube""" | |
| try: | |
| result = subprocess.run([ | |
| 'yt-dlp', | |
| '--dump-json', | |
| '--flat-playlist', | |
| '-I', f'1:{count}', | |
| f'ytsearch{count}:{query}' | |
| ], capture_output=True, text=True, timeout=30) | |
| if result.returncode != 0: | |
| return [] | |
| # Parse multiple JSON objects (one per line) | |
| videos = [] | |
| for line in result.stdout.strip().split('\n'): | |
| if line.strip(): | |
| videos.append(json.loads(line)) | |
| return videos | |
| except: | |
| return [] | |
| def play_with_mpv(video_id, title, artist): | |
| """Play audio and show metadata using mpv with MPRIS""" | |
| try: | |
| player = subprocess.Popen([ | |
| 'mpv', | |
| '--no-video', | |
| '--really-quiet', | |
| f'--force-media-title={title} - {artist}', | |
| f'https://www.youtube.com/watch?v={video_id}' | |
| ]) | |
| return player | |
| except: | |
| return None | |
| def main(): | |
| if len(sys.argv) != 3 or sys.argv[1] != "--search": | |
| print("Usage: listen --search \"song name\"") | |
| return | |
| query = sys.argv[2] | |
| print(f"🔍 Searching: {query}") | |
| # Get multiple videos | |
| videos = get_youtube_playlist(query, 5) | |
| if not videos: | |
| print("❌ No results found") | |
| return | |
| print(f"📝 Found {len(videos)} related songs") | |
| for i, info in enumerate(videos, 1): | |
| title = info.get('title', 'Unknown') | |
| uploader = info.get('uploader', 'Unknown Artist') | |
| video_id = info.get('id', '') | |
| print(f"\n🎵 [{i}/{len(videos)}] Now playing: {title}") | |
| print(f"👤 Artist: {uploader}") | |
| # Show notification | |
| subprocess.run([ | |
| 'notify-send', | |
| '-i', 'audio-x-generic', | |
| '-t', '5000', | |
| f'Now Playing [{i}/{len(videos)}]', | |
| f'{title}\n{uploader}' | |
| ]) | |
| # Play with mpv (audio + MPRIS metadata) | |
| player = play_with_mpv(video_id, title, uploader) | |
| if not player: | |
| # Fallback to ffplay (no metadata) | |
| player = subprocess.Popen([ | |
| 'ffplay', | |
| '-autoexit', | |
| '-nodisp', | |
| '-loglevel', 'quiet', | |
| f'https://www.youtube.com/watch?v={video_id}' | |
| ]) | |
| try: | |
| player.wait() | |
| if i < len(videos): | |
| print(f"⏭️ Next song starting...") | |
| except KeyboardInterrupt: | |
| player.terminate() | |
| print("\n⏹️ Stopped") | |
| break | |
| print("✅ Playlist finished") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment