import moviepy.editor as mp
import numpy as np
from PIL import Image
import os
import requests
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
def create_thumbnails(video_url, output_dir, number=60, width=160, height=90, columns=10):
# Create the output directory if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Download the video from the URL
video_path = os.path.join(output_dir, 'temp_video.mp4')
os.system(f"ffmpeg -i {video_url} -c copy {video_path}")
# Load the video
with mp.VideoFileClip(video_path) as video:
# Calculate the interval between thumbnails
duration = video.duration
interval = duration / number
# Create a list to store the thumbnails
thumbnails = []
for i in range(number):
# Get the frame at the specific time
frame = video.get_frame(i * interval)
# Convert the frame to an image
img = Image.fromarray(frame)
# Resize the image
img = img.resize((width, height))
thumbnails.append(img)
# Create a blank image to paste the thumbnails
rows = (number + columns - 1) // columns
result_img = Image.new('RGB', (columns * width, rows * height))
for i, thumbnail in enumerate(thumbnails):
x = (i % columns) * width
y = (i // columns) * height
result_img.paste(thumbnail, (x, y))
# Save the result image
output_path = os.path.join(output_dir, 'output_thumbnail.jpg')
result_img.save(output_path)
# Remove the temporary video file
os.remove(video_path)
# Example usage
create_thumbnails('https://raw.githubusercontent.com/Huyenuiio/Data-src/main/video/Tokidoki%20Bosotto%20Russiago%20de%20Dereru%20Tonari%20no%20Alya-san/ep1/output.m3u8', 'output_directory')