|
| 1 | +import os |
| 2 | +import cv2 |
| 3 | +import time |
| 4 | +import yt_dlp as youtube_dl |
| 5 | +from selenium import webdriver |
| 6 | +from selenium.webdriver.chrome.service import Service |
| 7 | +from selenium.webdriver.common.by import By |
| 8 | +from selenium.webdriver.chrome.options import Options |
| 9 | +from webdriver_manager.chrome import ChromeDriverManager |
| 10 | + |
| 11 | +# Function to set up Selenium and handle ads on YouTube |
| 12 | +def setup_selenium_and_skip_ads(youtube_url): |
| 13 | + # Configure Selenium options (headless for background operation) |
| 14 | + chrome_options = Options() |
| 15 | + chrome_options.add_argument("--headless") |
| 16 | + chrome_options.add_argument("--disable-gpu") |
| 17 | + chrome_options.add_argument("--no-sandbox") |
| 18 | + chrome_options.add_argument("--disable-dev-shm-usage") |
| 19 | + |
| 20 | + # Set up Selenium WebDriver (automatically downloads the correct ChromeDriver) |
| 21 | + driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) |
| 22 | + |
| 23 | + # Open YouTube URL |
| 24 | + driver.get(youtube_url) |
| 25 | + |
| 26 | + # Wait for ads (you can adjust this wait time depending on the network speed) |
| 27 | + time.sleep(10) |
| 28 | + |
| 29 | + # Check if there is a "Skip Ad" button |
| 30 | + try: |
| 31 | + skip_button = driver.find_element(By.CLASS_NAME, "ytp-ad-skip-button") |
| 32 | + if skip_button: |
| 33 | + skip_button.click() |
| 34 | + print("Ad skipped") |
| 35 | + except: |
| 36 | + print("No skippable ad found or already skipped") |
| 37 | + |
| 38 | + # Get the current URL after ads (if redirected) |
| 39 | + current_url = driver.current_url |
| 40 | + |
| 41 | + # Close the browser after handling the ad |
| 42 | + driver.quit() |
| 43 | + |
| 44 | + return current_url |
| 45 | + |
| 46 | +# Function to download the video using yt-dlp |
| 47 | +def download_youtube_video(youtube_url): |
| 48 | + ydl_opts = { |
| 49 | + 'format': 'best', # Get the best quality video |
| 50 | + 'outtmpl': 'video.mp4', # Name of the downloaded video |
| 51 | + } |
| 52 | + with youtube_dl.YoutubeDL(ydl_opts) as ydl: |
| 53 | + ydl.download([youtube_url]) |
| 54 | + |
| 55 | +# Function to extract frames using OpenCV |
| 56 | +def extract_frames(video_path, output_folder): |
| 57 | + if not os.path.exists(output_folder): |
| 58 | + os.makedirs(output_folder) |
| 59 | + |
| 60 | + cap = cv2.VideoCapture(video_path) |
| 61 | + frame_count = 0 |
| 62 | + |
| 63 | + while True: |
| 64 | + ret, frame = cap.read() |
| 65 | + if not ret: |
| 66 | + break |
| 67 | + |
| 68 | + # Save each frame as an image |
| 69 | + frame_path = os.path.join(output_folder, f"frame_{frame_count:05d}.jpg") |
| 70 | + cv2.imwrite(frame_path, frame) |
| 71 | + |
| 72 | + frame_count += 1 |
| 73 | + |
| 74 | + cap.release() |
| 75 | + print(f"Extracted {frame_count} frames.") |
| 76 | + |
| 77 | +# Main flow |
| 78 | +def main(youtube_url): |
| 79 | + print("Processing YouTube video...") |
| 80 | + |
| 81 | + # Step 1: Handle YouTube ads and get the final video URL |
| 82 | + processed_url = setup_selenium_and_skip_ads(youtube_url) |
| 83 | + |
| 84 | + # Step 2: Download the video using yt-dlp |
| 85 | + print(f"Downloading video from: {processed_url}") |
| 86 | + download_youtube_video(processed_url) |
| 87 | + |
| 88 | + # Step 3: Extract frames from the downloaded video |
| 89 | + print("Extracting frames from video...") |
| 90 | + extract_frames('video.mp4', 'frames') |
| 91 | + |
| 92 | + print("Process completed.") |
| 93 | + |
| 94 | +# Example usage |
| 95 | +if __name__ == "__main__": |
| 96 | + youtube_video_url = "https://www.youtube.com/watch?v=7Y5q41D8_hs" # Replace with your video URL |
| 97 | + main(youtube_video_url) |
0 commit comments