Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,10 @@ from diffusers.utils import export_to_video
|
|
6 |
from PIL import Image, ImageOps
|
7 |
from gtts import gTTS
|
8 |
from pydub import AudioSegment
|
9 |
-
|
|
|
|
|
|
|
10 |
import ffmpeg
|
11 |
import requests
|
12 |
from io import BytesIO
|
@@ -100,19 +103,33 @@ def generate_video(prompt, image_url):
|
|
100 |
AudioSegment.from_mp3("voice.mp3").export("voice.wav", format="wav")
|
101 |
|
102 |
# Step 6: Subtitles
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
# Step 7: Merge video + audio + subtitles
|
118 |
final_output = "final_with_audio.mp4"
|
|
|
6 |
from PIL import Image, ImageOps
|
7 |
from gtts import gTTS
|
8 |
from pydub import AudioSegment
|
9 |
+
try:
|
10 |
+
import whisper
|
11 |
+
except ImportError:
|
12 |
+
whisper = None
|
13 |
import ffmpeg
|
14 |
import requests
|
15 |
from io import BytesIO
|
|
|
103 |
AudioSegment.from_mp3("voice.mp3").export("voice.wav", format="wav")
|
104 |
|
105 |
# Step 6: Subtitles
|
106 |
+
if whisper is not None:
|
107 |
+
try:
|
108 |
+
model = whisper.load_model("base", device="cpu")
|
109 |
+
result = model.transcribe("voice.wav", task="transcribe", language="en")
|
110 |
+
|
111 |
+
# Generate SRT subtitles manually since result["srt"] might not be available
|
112 |
+
srt_content = ""
|
113 |
+
for i, segment in enumerate(result["segments"]):
|
114 |
+
start_time = format_time(segment["start"])
|
115 |
+
end_time = format_time(segment["end"])
|
116 |
+
text = segment["text"].strip()
|
117 |
+
srt_content += f"{i + 1}\n{start_time} --> {end_time}\n{text}\n\n"
|
118 |
+
|
119 |
+
with open("subtitles.srt", "w", encoding="utf-8") as f:
|
120 |
+
f.write(srt_content)
|
121 |
+
except Exception as e:
|
122 |
+
print(f"Whisper transcription failed: {e}")
|
123 |
+
# Create a simple subtitle with the original prompt
|
124 |
+
srt_content = f"1\n00:00:00,000 --> 00:00:05,000\n{prompt}\n\n"
|
125 |
+
with open("subtitles.srt", "w", encoding="utf-8") as f:
|
126 |
+
f.write(srt_content)
|
127 |
+
else:
|
128 |
+
print("Whisper not available, using prompt as subtitle")
|
129 |
+
# Create a simple subtitle with the original prompt
|
130 |
+
srt_content = f"1\n00:00:00,000 --> 00:00:05,000\n{prompt}\n\n"
|
131 |
+
with open("subtitles.srt", "w", encoding="utf-8") as f:
|
132 |
+
f.write(srt_content)
|
133 |
|
134 |
# Step 7: Merge video + audio + subtitles
|
135 |
final_output = "final_with_audio.mp4"
|