sh-5.2$ /bin/python "/home/lucas/# Code preview for the youtube channel h.py"
Starting process...
Fetching videos from channel: https://www.youtube.com/channel/UC2OseCB4QSKKQ6I30HY4eRQ
Traceback (most recent call last):
  File "/var/data/python/lib/python3.12/site-packages/pytube/helpers.py", line 57, in __getitem__
    next_item = next(self.gen)
                ^^^^^^^^^^^^^^
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lucas/# Code preview for the youtube channel h.py", line 80, in <module>
    main()
  File "/home/lucas/# Code preview for the youtube channel h.py", line 72, in main
    downloaded_videos = download_videos(CHANNEL_URL)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lucas/# Code preview for the youtube channel h.py", line 32, in download_videos
    video_urls = channel.video_urls[:max_videos]
                 ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/var/data/python/lib/python3.12/site-packages/pytube/helpers.py", line 60, in __getitem__
    raise IndexError
IndexError
sh-5.2$ 

"import os
from pytube import Channel, YouTube
from pytube.exceptions import RegexMatchError
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

# Constants
CHANNEL_URL = "https://www.youtube.com/channel/UC2OseCB4QSKKQ6I30HY4eRQ"
DOWNLOAD_FOLDER = "downloads"
CLIP_FOLDER = "clips"
CLIP_DURATION = 10  # Duration of each preview clip in seconds
MAX_VIDEOS = 5  # Adjust as needed

# Create folders for downloads and clips
os.makedirs(DOWNLOAD_FOLDER, exist_ok=True)
os.makedirs(CLIP_FOLDER, exist_ok=True)

def download_videos(channel_url, max_videos=MAX_VIDEOS):
    """
    Download the latest videos from the specified channel.
    """
    print(f"Fetching videos from channel: {channel_url}")
    try:
        channel = Channel(channel_url)
    except RegexMatchError as e:
        print(f"Error parsing channel URL: {e}")
        return []
    except Exception as e:
        print(f"Unexpected error: {e}")
        return []
    
    video_urls = channel.video_urls[:max_videos]
    downloaded_videos = []

    for url in video_urls:
        try:
            yt = YouTube(url)
            print(f"Downloading: {yt.title}")
            video = yt.streams.filter(progressive=True, file_extension="mp4").first()
            if video is None:
                print(f"No suitable stream found for {yt.title}")
                continue
            output_path = video.download(DOWNLOAD_FOLDER)
            downloaded_videos.append(output_path)
            print(f"Downloaded: {output_path}")
        except Exception as e:
            print(f"Error downloading {url}: {e}")
    
    return downloaded_videos

def create_clips(video_paths):
    """
    Extract short clips from downloaded videos.
    """
    print("Creating short clips...")
    for video_path in video_paths:
        try:
            video = VideoFileClip(video_path)
            if video.duration < CLIP_DURATION + 5:
                clip_start = max(0, video.duration - CLIP_DURATION)
            else:
                clip_start = 5  # Start at 5s or adjust as needed
            clip_end = clip_start + CLIP_DURATION
            clip_output = os.path.join(CLIP_FOLDER, f"clip_{os.path.basename(video_path)}")
            ffmpeg_extract_subclip(video_path, clip_start, clip_end, targetname=clip_output)
            print(f"Clip created: {clip_output}")
        except Exception as e:
            print(f"Error creating clip for {video_path}: {e}")

def main():
    print("Starting process...")
    downloaded_videos = download_videos(CHANNEL_URL)
    if downloaded_videos:
        create_clips(downloaded_videos)
        print("Process complete. Check the 'clips' folder for results.")
    else:
        print("No videos were downloaded. Process terminated.")

if __name__ == "__main__":
    main()"