#!/usr/bin/env python3 """ Example utility: How to download Creative Commons videos using yt-dlp from the metadata.jsonl / metadata.csv dataset. Links and metadata only, no video files included. Each video keeps its original Creative Commons license; credit the creators. """ import json import argparse import subprocess import os def download_videos(jsonl_path: str, output_dir: str = "downloads", max_count: int = 5, category: str = None): os.makedirs(output_dir, exist_ok=True) count = 0 with open(jsonl_path, "r", encoding="utf-8") as f: for line in f: item = json.loads(line) if category and item.get("category") != category: continue url = item["url"] cat = item["category"] title = item["title"] vid_id = item["video_id"] print(f"\n[{count + 1}/{max_count}] Downloading CC video: {title}") print(f"URL: {url} | Category: {cat}") dest_folder = os.path.join(output_dir, cat) os.makedirs(dest_folder, exist_ok=True) output_template = os.path.join(dest_folder, "%(id)s.%(ext)s") # yt-dlp command: 720p max to save bandwidth cmd = [ "yt-dlp", "-f", "bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720]/best", "-o", output_template, "--max-filesize", "150M", "--no-playlist", url ] try: subprocess.run(cmd, check=True) count += 1 except subprocess.CalledProcessError as e: print(f"Error downloading {vid_id}: {e}") if count >= max_count: break print(f"\nDone! Downloaded {count} sample videos to '{output_dir}'.") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Download sample CC videos using yt-dlp") parser.add_argument("--jsonl", type=str, default="metadata.jsonl", help="Path to metadata.jsonl") parser.add_argument("--count", type=int, default=3, help="Number of videos to download") parser.add_argument("--category", type=str, default="nepal_content", help="Category filter (optional)") parser.add_argument("--out", type=str, default="downloads", help="Output directory") args = parser.parse_args() download_videos(args.jsonl, args.out, args.count, args.category)