naohiro701 commited on
Commit
4e0d1e9
·
verified ·
1 Parent(s): 0ba31b5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from moviepy.editor import AudioFileClip, ImageClip, concatenate_videoclips
3
+ from moviepy.video.VideoClip import TextClip
4
+ import tempfile
5
+ import os
6
+
7
+ def generate_video(image_path, audio_path, output_path):
8
+ """
9
+ Generates a video from an image and an audio file.
10
+
11
+ Args:
12
+ image_path (str): Path to the image file.
13
+ audio_path (str): Path to the audio file.
14
+ output_path (str): Path where the output video will be saved.
15
+ """
16
+ # Load audio and image
17
+ audio_clip = AudioFileClip(audio_path)
18
+ image_clip = ImageClip(image_path).set_duration(audio_clip.duration)
19
+
20
+ # Resize the image to fit standard video dimensions (optional)
21
+ image_clip = image_clip.resize(height=720)
22
+
23
+ # Set audio to the image clip
24
+ video = image_clip.set_audio(audio_clip)
25
+
26
+ # Write the final video file
27
+ video.write_videofile(output_path, codec="libx264", audio_codec="aac")
28
+
29
+ # Streamlit App
30
+ st.title("Video Creator: Image + Audio")
31
+
32
+ # Upload the audio file
33
+ uploaded_audio = st.file_uploader("Upload your MP3 file", type=["mp3"])
34
+
35
+ # Upload the image file
36
+ uploaded_image = st.file_uploader("Upload your image file", type=["jpg", "jpeg", "png"])
37
+
38
+ # Temporary directories to handle file uploads
39
+ if uploaded_audio and uploaded_image:
40
+ with tempfile.TemporaryDirectory() as temp_dir:
41
+ # Save uploaded audio to temporary file
42
+ audio_path = os.path.join(temp_dir, uploaded_audio.name)
43
+ with open(audio_path, "wb") as f:
44
+ f.write(uploaded_audio.read())
45
+
46
+ # Save uploaded image to temporary file
47
+ image_path = os.path.join(temp_dir, uploaded_image.name)
48
+ with open(image_path, "wb") as f:
49
+ f.write(uploaded_image.read())
50
+
51
+ # Output video path
52
+ output_video_path = os.path.join(temp_dir, "output_video.mp4")
53
+
54
+ # Generate the video
55
+ st.write("Creating video... Please wait.")
56
+ generate_video(image_path, audio_path, output_video_path)
57
+
58
+ # Allow user to download the video
59
+ with open(output_video_path, "rb") as video_file:
60
+ st.download_button(
61
+ label="Download the video",
62
+ data=video_file,
63
+ file_name="output_video.mp4",
64
+ mime="video/mp4"
65
+ )
66
+ st.success("Video created successfully!")
67
+ else:
68
+ st.info("Please upload both an MP3 file and an image file to create the video.")