In this article, we will see the Python script to make a video from any website’s screenshot. This will happen with only one click after pasting the given code in this article.
Here’s an alternative approach using selenium and opencv libraries to capture screenshots and generate a video.
import time import os from selenium import webdriver import cv2 def capture_website(url): # Set up Selenium WebDriver options = webdriver.ChromeOptions() options.add_argument('--headless') # Run Chrome in headless mode driver = webdriver.Chrome(options=options) # Open the webpage driver.get(url) time.sleep(5) # Wait for the page to load completely (adjust as needed) # Get the page height page_height = driver.execute_script('return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);') # Set the window size to match the page height driver.set_window_size(1280, page_height) screenshots = [] # Capture screenshots of the webpage at different scroll positions for scroll_position in range(0, page_height, 1280): driver.execute_script(f'window.scrollTo(0, {scroll_position});') time.sleep(0.2) # Wait for scrolling to settle (adjust as needed) # Capture screenshot using OpenCV screenshot = driver.get_screenshot_as_png() screenshots.append(screenshot) # Quit the driver driver.quit() return screenshots def generate_video(screenshots): frame_rate = 10 # Adjust as needed output_file = 'website_video.mp4' # Output file path # Save screenshots as temporary image files image_files = [] for i, screenshot in enumerate(screenshots): image_file = f'screenshot_{i}.png' with open(image_file, 'wb') as f: f.write(screenshot) image_files.append(image_file) # Read the first screenshot to get the frame dimensions first_screenshot = cv2.imread(image_files[0]) height, width, _ = first_screenshot.shape # Create a VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'mp4v') video_writer = cv2.VideoWriter(output_file, fourcc, frame_rate, (width, height)) # Write the screenshots to the video file for image_file in image_files: frame = cv2.imread(image_file) video_writer.write(frame) # Release the video writer and delete the temporary image files video_writer.release() for image_file in image_files: os.remove(image_file) print(f'Video created successfully: {output_file}') def create_video_from_website(url): screenshots = capture_website(url) generate_video(screenshots) # Usage example target_url = 'https://example.com' create_video_from_website(target_url)
This approach uses the selenium library with a headless Chrome browser and opencv for capturing screenshots and generating the video. The script navigates to the specified URL, captures screenshots at different scroll positions, and saves them as temporary image files. Then, it uses opencv to create a video by writing the image frames to a video file.
The output video will be saved in the same folder where your script will be placed.
Change the website name to any website in the script from “https://exmaple.com”.
Please ensure that you have the necessary dependencies installed. You can install them using pip.
pip install selenium opencv-python-headless
Give this alternative approach a try, and it should capture screenshots and generate a video from the website.