Spaces:
Runtime error
Runtime error
File size: 4,212 Bytes
b357832 bb6011e b357832 bb6011e b357832 bb6011e b357832 bb6011e e9d0b3c b357832 bb6011e e9d0b3c bb6011e b357832 bb6011e b357832 bb6011e b357832 bb6011e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
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() |