import cv2 import numpy as np from pydub import AudioSegment from moviepy.editor import VideoFileClip, AudioFileClip def image_to_video(image_path, video_path,audio_path, fps=30): """ Converts an image into a video of specified duration. Parameters: - image_path: Path to the input image. - video_path: Path where the output video will be saved. - duration_sec: Duration of the video in seconds. - fps: Frames per second of the output video. """ # Load the image print("image_path",image_path) img = cv2.imread(image_path) print("image_path") if img is None: raise ValueError("Image could not be loaded. Please check the path.") # Get image dimensions height, width, layers = img.shape # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use 'XVID' if you prefer AVI format video = cv2.VideoWriter(video_path, fourcc, fps, (width, height)) audio = AudioSegment.from_file(audio_path) duration_sec = len(audio) / 1000.0 # Duration in milliseconds to seconds # Calculate the number of frames needed to achieve the desired duration num_frames = duration_sec * fps # Write the image to video file for the required number of frames for _ in range(int(num_frames)): video.write(img) # Release the video writer video.release() video_clip = VideoFileClip(video_path) audio_clip = AudioFileClip(audio_path) final_clip = video_clip.set_audio(audio_clip) final_clip.write_videofile(video_path, codec="libx264", audio_codec="aac") return video_path def rename(): import os import shutil # Define the directory to search in directory = os.getcwd() # Initialize variables for the first .jpg and .wav files found first_jpg_file = None first_wav_file = None # Search for the first .jpg and .wav files in the directory for filename in os.listdir(directory): if filename.endswith('.jpg') and first_jpg_file is None: first_jpg_file = os.path.join(directory, filename) elif filename.endswith('.wav') and first_wav_file is None: first_wav_file = os.path.join(directory, filename) print(f"Audio file renamed to {audio_path_new}") # New paths with desired names image_path_new = os.path.join(directory, 'logo.jpg') audio_path_new = os.path.join(directory, 'audio.wav') print(f"Image file renamed to {image_path_new}") # Rename (or move) the image file if found if first_jpg_file: shutil.move(first_jpg_file, image_path_new) print(f"Image file renamed to {image_path_new}") else: print("No .jpg file found.") # Rename (or move) the audio file if found if first_wav_file: shutil.move(first_wav_file, audio_path_new) print(f"Audio file renamed to {audio_path_new}") else: print("No .wav file found.") return image_path_new,audio_path_new # Example usage def image_audio_to_video(image_path, audio_path): import os dir=os.getcwd() print("dir",dir) video_path=f"/{dir}/video.mp4" print("video_path",video_path) image_to_video(image_path, video_path,audio_path) return video_path import gradio as gr def setup_interface(): """ Setup and launch the Gradio interface. """ with gr.Blocks() as demo: gr.Markdown("## Create a Video from an Image and Audio") with gr.Row(): with gr.Column(): image_upload = gr.Image(label="Upload Image",type="filepath") audio_upload = gr.Audio(label="Upload Audio",type="filepath") with gr.Column(): output_video = gr.Video(label="Output Video") # Button to initiate the process if image_upload and audio_upload : submit_btn = gr.Button("Create Video") # Function call on button press with both image and audio as inputs submit_btn.click(fn=image_audio_to_video, inputs=[image_upload, audio_upload], outputs=output_video) demo.launch(debug=True) if __name__ == "__main__": setup_interface()