import os,sys
import random
import subprocess
import json

def get_video_duration(youtube_url):
    """Fetch video duration in seconds using yt-dlp."""
    cmd = ["yt-dlp", "--print-json", youtube_url]
    result = subprocess.run(cmd, capture_output=True, text=True, check=True)
    metadata = json.loads(result.stdout)
    return metadata["duration"]

def download_video_segment(youtube_url, start_time, output_path="raw_clip.mp4"):
    """
    Download a short subclip from a YouTube video.
    This fetches a slightly longer segment to ensure no missing frames.
    """
    cmd = [
        "yt-dlp", 
        "--cookies"," /home/cameronsmith/cookies.txt ", 
        "-f", "best[ext=mp4]", youtube_url,
        "--download-sections", f"*{start_time-0.5}-{start_time+2.0}",
        
        "-o", output_path,
    ]
    print(cmd)
    subprocess.run(cmd, check=True)

def trim_video(input_path, output_path, start_offset=0.5, duration=1.2):
    """
    Trim the video precisely using ffmpeg.
    This removes any black frames added by YouTube encoding.
    """
    #cmd = ["ffmpeg", "-y", "-i", input_path, "-ss", str(start_offset),
    #   "-t", str(duration), "-c:v", "mpeg4", "-c:a", "aac", output_path]
    cmd = [
    "ffmpeg", "-y", "-i", input_path, "-ss", str(start_offset),
    "-t", str(duration),
    "-c:v", "mpeg4", "-q:v", "1",  # Lower q:v means better quality (1-5 range)
    "-b:v", "5M",  # Boost video bitrate to 5 Mbps
    #"-vf", "scale=-1:720",  # Resize to 720p while keeping aspect ratio
    "-c:a", "aac", "-b:a", "192k",  # High-quality audio
    output_path
]


    subprocess.run(cmd, check=True)

def main(youtube_url, num_clips=1):
    print("Fetching video duration...")
    #video_duration = get_video_duration(youtube_url)
    video_duration=60*120 # just hacking this because slow to query video duration, change to seconds in videos if not 2 hrs

    print("Selecting random timestamps...")
    clip_durations = []
    for _ in range(num_clips):
        start_time = random.uniform(0.5, video_duration - 2.0)  # Avoids edge cases
        clip_durations.append(start_time)

    for i, start_time in enumerate(clip_durations):
        raw_clip_path = f"{sys.argv[2]}/raw_clip_{i+1}.mp4"
        final_clip_path = f"{sys.argv[2]}/clip_{i+1}.mp4"

        print(f"Downloading raw segment for clip {i+1} from {start_time:.2f}s...")
        download_video_segment(youtube_url, start_time, raw_clip_path)

        print(f"Trimming to precise 40 frames...")
        trim_video(raw_clip_path, final_clip_path)

        os.remove(raw_clip_path)  # Clean up raw file

    print("Download complete.")

if __name__ == "__main__":
    url = sys.argv[1]#"https://www.youtube.com/watch?v=r11kS1unliM"#input("Enter YouTube URL: ")
    main(url)

