naohiro701's picture
Create app.py
4e0d1e9 verified
raw
history blame
2.42 kB
import streamlit as st
from moviepy.editor import AudioFileClip, ImageClip, concatenate_videoclips
from moviepy.video.VideoClip import TextClip
import tempfile
import os
def generate_video(image_path, audio_path, output_path):
"""
Generates a video from an image and an audio file.
Args:
image_path (str): Path to the image file.
audio_path (str): Path to the audio file.
output_path (str): Path where the output video will be saved.
"""
# Load audio and image
audio_clip = AudioFileClip(audio_path)
image_clip = ImageClip(image_path).set_duration(audio_clip.duration)
# Resize the image to fit standard video dimensions (optional)
image_clip = image_clip.resize(height=720)
# Set audio to the image clip
video = image_clip.set_audio(audio_clip)
# Write the final video file
video.write_videofile(output_path, codec="libx264", audio_codec="aac")
# Streamlit App
st.title("Video Creator: Image + Audio")
# Upload the audio file
uploaded_audio = st.file_uploader("Upload your MP3 file", type=["mp3"])
# Upload the image file
uploaded_image = st.file_uploader("Upload your image file", type=["jpg", "jpeg", "png"])
# Temporary directories to handle file uploads
if uploaded_audio and uploaded_image:
with tempfile.TemporaryDirectory() as temp_dir:
# Save uploaded audio to temporary file
audio_path = os.path.join(temp_dir, uploaded_audio.name)
with open(audio_path, "wb") as f:
f.write(uploaded_audio.read())
# Save uploaded image to temporary file
image_path = os.path.join(temp_dir, uploaded_image.name)
with open(image_path, "wb") as f:
f.write(uploaded_image.read())
# Output video path
output_video_path = os.path.join(temp_dir, "output_video.mp4")
# Generate the video
st.write("Creating video... Please wait.")
generate_video(image_path, audio_path, output_video_path)
# Allow user to download the video
with open(output_video_path, "rb") as video_file:
st.download_button(
label="Download the video",
data=video_file,
file_name="output_video.mp4",
mime="video/mp4"
)
st.success("Video created successfully!")
else:
st.info("Please upload both an MP3 file and an image file to create the video.")