askrobots / dbbasic-video
Public
Video analysis: download, keyframes, transcripts, thumbnail grids and AI summaries of what's on screen.
download_video.py · 1.5 KB · 60 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Download video from YouTube |
| 4 | """ |
| 5 | |
| 6 | import argparse |
| 7 | import subprocess |
| 8 | import sys |
| 9 | |
| 10 | |
| 11 | def download_video(url, output="video.mp4", format_spec="bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"): |
| 12 | """Download video using yt-dlp.""" |
| 13 | print(f"Downloading video from: {url}") |
| 14 | |
| 15 | cmd = [ |
| 16 | "yt-dlp", |
| 17 | "-f", format_spec, |
| 18 | "-o", output, |
| 19 | url |
| 20 | ] |
| 21 | |
| 22 | try: |
| 23 | result = subprocess.run(cmd, check=True, capture_output=False) |
| 24 | print(f"\nVideo downloaded to: {output}") |
| 25 | return output |
| 26 | except subprocess.CalledProcessError as e: |
| 27 | print(f"Error downloading video: {e}", file=sys.stderr) |
| 28 | return None |
| 29 | except FileNotFoundError: |
| 30 | print("Error: yt-dlp not found. Install with: pip install yt-dlp", file=sys.stderr) |
| 31 | return None |
| 32 | |
| 33 | |
| 34 | def main(): |
| 35 | parser = argparse.ArgumentParser( |
| 36 | description="Download video from YouTube" |
| 37 | ) |
| 38 | parser.add_argument( |
| 39 | "url", |
| 40 | help="YouTube URL" |
| 41 | ) |
| 42 | parser.add_argument( |
| 43 | "-o", "--output", |
| 44 | default="video.mp4", |
| 45 | help="Output filename (default: video.mp4)" |
| 46 | ) |
| 47 | parser.add_argument( |
| 48 | "-f", "--format", |
| 49 | default="bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best", |
| 50 | help="Format specification for yt-dlp" |
| 51 | ) |
| 52 | |
| 53 | args = parser.parse_args() |
| 54 | |
| 55 | result = download_video(args.url, args.output, args.format) |
| 56 | return 0 if result else 1 |
| 57 | |
| 58 | |
| 59 | if __name__ == "__main__": |
| 60 | sys.exit(main()) |