music-fetch: duration filter (11 min default, --allow-long-songs); populate dungeon-synth themed ambience tracks (9 tracks across 8 ambiences)
This commit is contained in:
+24
-3
@@ -13,6 +13,7 @@ Usage:
|
||||
python3 tools/music-fetch.py tavern --url "https://youtu.be/..."
|
||||
python3 tools/music-fetch.py tavern --replace
|
||||
python3 tools/music-fetch.py tavern --dry-run
|
||||
python3 tools/music-fetch.py tavern --allow-long-songs
|
||||
"""
|
||||
|
||||
import argparse
|
||||
@@ -79,9 +80,13 @@ def make_stem(ambience, existing_files):
|
||||
# ── YouTube search / info ─────────────────────────────────
|
||||
def search_videos(query, max_results=20):
|
||||
YoutubeDL = _import_ytdlp()
|
||||
ydl = YoutubeDL({"quiet": True, "no_warnings": True})
|
||||
info = ydl.extract_info(f"ytsearch{max_results}:{query}", download=False)
|
||||
return info.get("entries", [])
|
||||
ydl = YoutubeDL({"quiet": True, "no_warnings": True, "ignoreerrors": True})
|
||||
try:
|
||||
info = ydl.extract_info(f"ytsearch{max_results}:{query}", download=False)
|
||||
except Exception:
|
||||
return []
|
||||
entries = info.get("entries", []) if info else []
|
||||
return [e for e in entries if e is not None]
|
||||
|
||||
|
||||
def get_video_info(url):
|
||||
@@ -272,6 +277,8 @@ def main():
|
||||
help="Show what would be fetched without downloading")
|
||||
parser.add_argument("--max-results", type=int, default=20,
|
||||
help="Number of search results to consider (default: 20)")
|
||||
parser.add_argument("--allow-long-songs", action="store_true",
|
||||
help="Allow tracks longer than 10 minutes")
|
||||
args = parser.parse_args()
|
||||
|
||||
name = args.ambience.strip().lower()
|
||||
@@ -310,6 +317,20 @@ def main():
|
||||
print("No results found.")
|
||||
sys.exit(1)
|
||||
source_desc = f"top {len(candidates)} results for \"{query}\""
|
||||
# Filter to short tracks (< 10 min) unless --allow-long-songs
|
||||
if not args.allow_long_songs:
|
||||
short = [v for v in candidates if v.get("duration") is not None and v["duration"] <= 660]
|
||||
if not short:
|
||||
# Retry with a shorter-focused query
|
||||
short_query = f"10 minutes {query}"
|
||||
print(f" no short tracks found, retrying with: \"{short_query}\"")
|
||||
candidates = search_videos(short_query, args.max_results)
|
||||
short = [v for v in candidates if v.get("duration") is not None and v["duration"] <= 660]
|
||||
if short:
|
||||
candidates = short
|
||||
if not candidates:
|
||||
print("All results were longer than 10 minutes. Use --allow-long-songs to include them.")
|
||||
sys.exit(1)
|
||||
# Pick randomly from the pool
|
||||
video = random.choice(candidates)
|
||||
candidates = [video]
|
||||
|
||||
Reference in New Issue
Block a user