-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownloader.py
More file actions
75 lines (62 loc) · 2.3 KB
/
downloader.py
File metadata and controls
75 lines (62 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
"""
Downloads YouTube playlists or individual videos using yt-dlp.
Optimized for the lecture processing pipeline.
"""
import os
import json
import argparse
import logging
import subprocess
from pathlib import Path
# Setup Logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s'
)
logger = logging.getLogger(__name__)
DEFAULT_OUTPUT_DIR = Path.cwd() / "downloads"
def download_youtube_content(url, output_dir=DEFAULT_OUTPUT_DIR, audio_only=False):
"""
Downloads content from YouTube using yt-dlp.
"""
output_dir.mkdir(parents=True, exist_ok=True)
# Base command
cmd = [
"yt-dlp",
"--print", "filename",
"--no-playlist" if "list=" not in url else "--yes-playlist",
"--output", f"{output_dir}/%(id)s.%(ext)s",
"--write-info-json",
"--restrict-filenames",
]
if audio_only:
cmd += ["-x", "--audio-format", "wav", "--audio-quality", "0"]
else:
# Preferred format: mp4 (good for generic compatibility)
cmd += ["-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"]
cmd.append(url)
logger.info(f"Starting download: {url}")
try:
proc = subprocess.run(cmd, check=True, capture_output=True, text=True)
logger.info("Download completed successfully.")
# Log the filename if captured
if proc.stdout:
for line in proc.stdout.splitlines():
if line.strip():
logger.info(f"Downloaded: {line.strip()}")
except subprocess.CalledProcessError as e:
logger.error(f"Download failed for {url}")
logger.error(f"Error: {e.stderr}")
raise
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Download YouTube videos/playlists for processing.")
parser.add_argument("url", help="YouTube URL (Video or Playlist)")
parser.add_argument("--out", "-o", default=str(DEFAULT_OUTPUT_DIR), help="Output directory")
parser.add_argument("--audio", "-a", action="store_true", help="Download audio only (WAV)")
args = parser.parse_args()
try:
download_youtube_content(args.url, Path(args.out), args.audio)
except Exception as e:
logger.error(f"Fatal error: {e}")
exit(1)