Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,136 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
def image_to_video(
|
| 9 |
"""
|
| 10 |
-
Converts an image
|
| 11 |
-
into a video file.
|
| 12 |
|
| 13 |
Parameters:
|
| 14 |
-
-
|
| 15 |
-
-
|
|
|
|
| 16 |
- fps: Frames per second of the output video.
|
| 17 |
-
|
| 18 |
-
Returns:
|
| 19 |
-
- Path to the generated video file.
|
| 20 |
"""
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
video = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
num_frames =
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
video.release()
|
| 42 |
-
|
| 43 |
-
# Save the audio to a temporary file
|
| 44 |
-
audio_path = tempfile.mktemp('.wav')
|
| 45 |
-
sf.write(audio_path, audio_data.T, sample_rate) # Transpose if necessary for multi-channel audio
|
| 46 |
-
|
| 47 |
-
# Combine the video and audio
|
| 48 |
video_clip = VideoFileClip(video_path)
|
| 49 |
audio_clip = AudioFileClip(audio_path)
|
| 50 |
final_clip = video_clip.set_audio(audio_clip)
|
| 51 |
-
final_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")
|
| 52 |
return video_path
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
| 5 |
+
|
| 6 |
+
|
| 7 |
|
| 8 |
+
def image_to_video(image_path, video_path,audio_path, fps=30):
|
| 9 |
"""
|
| 10 |
+
Converts an image into a video of specified duration.
|
|
|
|
| 11 |
|
| 12 |
Parameters:
|
| 13 |
+
- image_path: Path to the input image.
|
| 14 |
+
- video_path: Path where the output video will be saved.
|
| 15 |
+
- duration_sec: Duration of the video in seconds.
|
| 16 |
- fps: Frames per second of the output video.
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
+
# Load the image
|
| 19 |
+
print("image_path",image_path)
|
| 20 |
+
img = cv2.imread(image_path)
|
| 21 |
+
print("image_path")
|
| 22 |
+
if img is None:
|
| 23 |
+
raise ValueError("Image could not be loaded. Please check the path.")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Get image dimensions
|
| 27 |
+
height, width, layers = img.shape
|
| 28 |
+
|
| 29 |
+
# Define the codec and create VideoWriter object
|
| 30 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use 'XVID' if you prefer AVI format
|
| 31 |
video = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
|
| 32 |
+
|
| 33 |
+
audio = AudioSegment.from_file(audio_path)
|
| 34 |
+
duration_sec = len(audio) / 1000.0 # Duration in milliseconds to seconds
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Calculate the number of frames needed to achieve the desired duration
|
| 38 |
+
num_frames = duration_sec * fps
|
| 39 |
+
|
| 40 |
+
# Write the image to video file for the required number of frames
|
| 41 |
+
for _ in range(int(num_frames)):
|
| 42 |
+
video.write(img)
|
| 43 |
+
|
| 44 |
+
# Release the video writer
|
| 45 |
video.release()
|
| 46 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
video_clip = VideoFileClip(video_path)
|
| 48 |
audio_clip = AudioFileClip(audio_path)
|
| 49 |
final_clip = video_clip.set_audio(audio_clip)
|
| 50 |
+
final_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")
|
| 51 |
return video_path
|
| 52 |
|
| 53 |
+
def rename():
|
| 54 |
+
import os
|
| 55 |
+
import shutil
|
| 56 |
+
|
| 57 |
+
# Define the directory to search in
|
| 58 |
+
directory = os.getcwd()
|
| 59 |
+
|
| 60 |
+
# Initialize variables for the first .jpg and .wav files found
|
| 61 |
+
first_jpg_file = None
|
| 62 |
+
first_wav_file = None
|
| 63 |
+
|
| 64 |
+
# Search for the first .jpg and .wav files in the directory
|
| 65 |
+
for filename in os.listdir(directory):
|
| 66 |
+
if filename.endswith('.jpg') and first_jpg_file is None:
|
| 67 |
+
first_jpg_file = os.path.join(directory, filename)
|
| 68 |
+
elif filename.endswith('.wav') and first_wav_file is None:
|
| 69 |
+
first_wav_file = os.path.join(directory, filename)
|
| 70 |
+
print(f"Audio file renamed to {audio_path_new}")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# New paths with desired names
|
| 74 |
+
image_path_new = os.path.join(directory, 'logo.jpg')
|
| 75 |
+
audio_path_new = os.path.join(directory, 'audio.wav')
|
| 76 |
+
print(f"Image file renamed to {image_path_new}")
|
| 77 |
+
|
| 78 |
+
# Rename (or move) the image file if found
|
| 79 |
+
if first_jpg_file:
|
| 80 |
+
shutil.move(first_jpg_file, image_path_new)
|
| 81 |
+
print(f"Image file renamed to {image_path_new}")
|
| 82 |
+
else:
|
| 83 |
+
print("No .jpg file found.")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# Rename (or move) the audio file if found
|
| 88 |
+
if first_wav_file:
|
| 89 |
+
shutil.move(first_wav_file, audio_path_new)
|
| 90 |
+
print(f"Audio file renamed to {audio_path_new}")
|
| 91 |
+
else:
|
| 92 |
+
print("No .wav file found.")
|
| 93 |
+
|
| 94 |
+
return image_path_new,audio_path_new
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# Example usage
|
| 99 |
+
def image_audio_to_video(image_path, audio_path):
|
| 100 |
+
|
| 101 |
+
import os
|
| 102 |
+
dir=os.getcwd()
|
| 103 |
+
print("dir",dir)
|
| 104 |
+
video_path=f"/{dir}/video.mp4"
|
| 105 |
+
print("video_path",video_path)
|
| 106 |
+
image_to_video(image_path, video_path,audio_path)
|
| 107 |
+
return video_path
|
| 108 |
+
|
| 109 |
+
import gradio as gr
|
| 110 |
+
def setup_interface():
|
| 111 |
+
"""
|
| 112 |
+
Setup and launch the Gradio interface.
|
| 113 |
+
"""
|
| 114 |
+
with gr.Blocks() as demo:
|
| 115 |
+
gr.Markdown("## Create a Video from an Image and Audio")
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column():
|
| 118 |
+
image_upload = gr.Image(label="Upload Image",type="filepath")
|
| 119 |
+
audio_upload = gr.Audio(label="Upload Audio",type="filepath")
|
| 120 |
+
with gr.Column():
|
| 121 |
+
output_video = gr.Video(label="Output Video")
|
| 122 |
+
|
| 123 |
+
# Button to initiate the process
|
| 124 |
+
if image_upload and audio_upload :
|
| 125 |
+
submit_btn = gr.Button("Create Video")
|
| 126 |
+
|
| 127 |
+
# Function call on button press with both image and audio as inputs
|
| 128 |
+
submit_btn.click(fn=image_audio_to_video,
|
| 129 |
+
inputs=[image_upload, audio_upload],
|
| 130 |
+
outputs=output_video)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
demo.launch(debug=True)
|
| 134 |
+
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
setup_interface()
|