import gradio as gr import os import subprocess import cv2 from moviepy.editor import VideoFileClip, concatenate_videoclips import math from huggingface_hub import snapshot_download model_ids = [ 'runwayml/stable-diffusion-v1-5', 'lllyasviel/sd-controlnet-depth', 'lllyasviel/sd-controlnet-canny', 'lllyasviel/sd-controlnet-openpose', ] for model_id in model_ids: model_name = model_id.split('/')[-1] snapshot_download(model_id, local_dir=f'checkpoints/{model_name}') def get_frame_count_in_duration(filepath): video = cv2.VideoCapture(filepath) fps = video.get(cv2.CAP_PROP_FPS) frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) duration = frame_count / fps width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) video.release() return gr.update(maximum=frame_count) def get_video_dimension(filepath): video = cv2.VideoCapture(filepath) width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(video.get(cv2.CAP_PROP_FPS)) video.release() return width, height, fps def resize_video(input_path, output_path, width): # Load the video clip video = VideoFileClip(input_path) # Calculate the new height while maintaining the aspect ratio height = int(video.size[1] * (width / video.size[0])) # Resize the video resized_video = video.resize(width=width, height=height) # Write the resized video to the output path resized_video.write_videofile(output_path, codec='libx264') return output_path def run_inference(prompt, video_path, condition, video_length): # Specify the input and output paths input_vid = video_path resized_vid = 'resized.mp4' # Call the function to resize the video video_path = resize_video(input_vid, resized_vid, width=512) width, height, fps = get_video_dimension(video_path) print(f"{width} x {height} | {fps}") output_path = 'output/' os.makedirs(output_path, exist_ok=True) # Construct the final video path video_path_output = os.path.join(output_path, f"{prompt}.mp4") # Check if the file already exists if os.path.exists(video_path_output): # Delete the existing file os.remove(video_path_output) if video_length > 12: command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{video_path}' --output_path '{output_path}' --width {width} --height {height} --fps {fps} --video_length {video_length} --is_long_video" else: command = f"python inference.py --prompt '{prompt}' --condition '{condition}' --video_path '{video_path}' --output_path '{output_path}' --width {width} --height {height} --fps {fps} --video_length {video_length}" subprocess.run(command, shell=True) # Construct the video path video_path_output = os.path.join(output_path, f"{prompt}.mp4") return "done", video_path_output css=""" #col-container {max-width: 510px; margin-left: auto; margin-right: auto;} """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.Markdown("""