naohiro701's picture
Update app.py
c2a2840 verified
import streamlit as st
import ffmpeg
import os
from PIL import Image
def create_video(image_path, audio_path, output_path):
"""
Create a video from an image and an audio file using FFmpeg.
:param image_path: Path to the image file
:param audio_path: Path to the audio file
:param output_path: Path to save the output video
"""
try:
# FFmpeg command to create a video
input_image = ffmpeg.input(image_path, loop=1, t=ffmpeg.probe(audio_path)['format']['duration'])
input_audio = ffmpeg.input(audio_path)
ffmpeg.output(input_image, input_audio, output_path, vcodec='libx264', acodec='aac', pix_fmt='yuv420p', shortest=None).run()
except Exception as e:
st.error(f"Error during video creation: {e}")
# Streamlit UI
st.title("Video Creator")
st.write("Upload an image and an audio file to create a video.")
# File uploaders
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
uploaded_audio = st.file_uploader("Upload an MP3 file", type=["mp3"])
if uploaded_image and uploaded_audio:
# Save uploaded files locally
image_path = f"temp_image.{uploaded_image.name.split('.')[-1]}"
audio_path = "temp_audio.mp3"
output_path = "output_video.mp4"
with open(image_path, "wb") as f:
f.write(uploaded_image.getbuffer())
with open(audio_path, "wb") as f:
f.write(uploaded_audio.getbuffer())
st.write("Files uploaded successfully.")
# Create video
st.write("Creating video...")
create_video(image_path, audio_path, output_path)
# Show and download video
if os.path.exists(output_path):
st.video(output_path)
with open(output_path, "rb") as f:
st.download_button(
label="Download Video",
data=f,
file_name="output_video.mp4",
mime="video/mp4"
)
# Cleanup temporary files
os.remove(image_path)
os.remove(audio_path)
os.remove(output_path)