import re

MUTAGEN_AVAILABLE = True
try:
    from mutagen.mp4 import MP4
except ImportError:
    MUTAGEN_AVAILABLE = False

def extract_artist_title(video_title):
    title = video_title.replace("(Karaoke Version)", "").replace("(Karaoke)", "").strip()
    if " - " in title:
        parts = title.split(" - ", 1)
        if len(parts) == 2:
            artist = parts[0].strip()
            song_title = parts[1].strip()
            return artist, song_title
    return "Unknown Artist", title

def add_id3_tags(file_path, video_title, channel_name):
    if not MUTAGEN_AVAILABLE:
        print("⚠️ mutagen not available - skipping ID3 tagging")
        return
    try:
        artist, title = extract_artist_title(video_title)
        mp4 = MP4(str(file_path))
        mp4['\xa9nam'] = title
        mp4['\xa9ART'] = artist
        mp4['\xa9alb'] = f"{channel_name} Karaoke"
        mp4['\xa9gen'] = "Karaoke"
        mp4.save()
        print(f"📝 Added ID3 tags: Artist='{artist}', Title='{title}'")
    except Exception as e:
        print(f"⚠️ Could not add ID3 tags: {e}") 