KaraokeVideoDownloader/karaoke_downloader/resolution_cli.py

172 lines
5.2 KiB
Python

#!/usr/bin/env python3
"""
Resolution Update Utility for Karaoke Playlist Downloader
Easily update the preferred video resolution in the configuration.
"""
import argparse
import json
from pathlib import Path
def update_resolution(resolution):
"""Update the resolution in config.json file."""
config_file = Path("config.json")
# Load existing config or create default
if config_file.exists():
try:
with open(config_file, "r", encoding="utf-8") as f:
config = json.load(f)
except json.JSONDecodeError:
print("❌ Error: Invalid config.json file")
return False
else:
print("📝 Creating new config.json file...")
config = {
"download_settings": {
"format": "best[height<=720][ext=mp4]/best[height<=720]/best[ext=mp4]/best",
"preferred_resolution": "720p",
"audio_format": "mp3",
"audio_quality": "0",
"subtitle_language": "en",
"subtitle_format": "srt",
"write_metadata": True,
"write_thumbnail": True,
"write_description": True,
"write_annotations": True,
"write_comments": True,
"write_subtitles": True,
"embed_metadata": True,
"add_metadata": True,
"continue_downloads": True,
"no_overwrites": True,
"ignore_errors": True,
"no_warnings": False,
},
"folder_structure": {
"downloads_dir": "downloads",
"logs_dir": "logs",
"tracking_file": "karaoke_tracking.json",
},
"logging": {
"level": "INFO",
"format": "%(asctime)s - %(levelname)s - %(message)s",
"include_console": True,
"include_file": True,
},
"platform_settings": {
"auto_detect_platform": True,
"yt_dlp_paths": {
"windows": "downloader/yt-dlp.exe",
"macos": "downloader/yt-dlp_macos",
"linux": "downloader/yt-dlp",
},
},
"yt_dlp_path": "downloader/yt-dlp.exe",
}
# Resolution mapping
resolution_map = {
"480p": "480",
"720p": "720",
"1080p": "1080",
"1440p": "1440",
"2160p": "2160",
}
if resolution not in resolution_map:
print(f"❌ Error: Invalid resolution '{resolution}'")
print(f"Valid options: {', '.join(resolution_map.keys())}")
return False
height = resolution_map[resolution]
old_resolution = config["download_settings"].get("preferred_resolution", "720p")
# Update the format string
config["download_settings"][
"format"
] = f"best[height<={height}][ext=mp4]/best[height<={height}]/best[ext=mp4]/best"
config["download_settings"]["preferred_resolution"] = resolution
# Save the updated config
try:
with open(config_file, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2, ensure_ascii=False)
print(
f"✅ Successfully updated resolution from {old_resolution} to {resolution}"
)
print(f"📝 Format string: {config['download_settings']['format']}")
return True
except Exception as e:
print(f"❌ Error saving config: {e}")
return False
def show_current_resolution():
"""Show the current resolution setting."""
config_file = Path("config.json")
if not config_file.exists():
print("📝 No config.json file found. Using default 720p resolution.")
return
try:
with open(config_file, "r", encoding="utf-8") as f:
config = json.load(f)
current_resolution = config["download_settings"].get(
"preferred_resolution", "720p"
)
current_format = config["download_settings"].get(
"format", "best[height<=720][ext=mp4]/best[height<=720]/best[ext=mp4]/best"
)
print(f"🎬 Current resolution: {current_resolution}")
print(f"📝 Format string: {current_format}")
except json.JSONDecodeError:
print("❌ Error: Invalid config.json file")
except Exception as e:
print(f"❌ Error reading config: {e}")
def main():
parser = argparse.ArgumentParser(
description="Update video resolution for Karaoke Playlist Downloader",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python update_resolution.py --show
python update_resolution.py --resolution 1080p
python update_resolution.py --resolution 720p
""",
)
parser.add_argument(
"--resolution",
"-r",
choices=["480p", "720p", "1080p", "1440p", "2160p"],
help="Set the preferred video resolution",
)
parser.add_argument(
"--show", "-s", action="store_true", help="Show current resolution setting"
)
args = parser.parse_args()
if args.show:
show_current_resolution()
elif args.resolution:
update_resolution(args.resolution)
else:
parser.print_help()
if __name__ == "__main__":
main()