Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import asyncio
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
from moviepy.editor import AudioFileClip
|
|
|
|
| 7 |
|
| 8 |
# Get all available voices
|
| 9 |
async def get_voices():
|
|
@@ -26,16 +27,28 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
| 26 |
await communicate.save(tmp_path)
|
| 27 |
return tmp_path, None
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Generate SRT file based on user preferences
|
| 30 |
def generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
segment_duration = audio_duration / (total_words // words_per_line // lines_per_paragraph) # Calculate duration based on total segments
|
| 35 |
-
|
| 36 |
current_time = 0
|
| 37 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 38 |
-
for i in range(0,
|
| 39 |
# Gather lines based on the defined words per line
|
| 40 |
lines = words[i:i + words_per_line]
|
| 41 |
line_text = ' '.join(lines)
|
|
@@ -73,6 +86,9 @@ async def text_to_audio_and_srt(text, voice, rate, pitch, words_per_line, lines_
|
|
| 73 |
audio_clip = AudioFileClip(audio_path)
|
| 74 |
audio_duration = audio_clip.duration
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
# Generate SRT file based on the entire text
|
| 77 |
base_name = os.path.splitext(audio_path)[0]
|
| 78 |
srt_path = f"{base_name}_subtitle.srt"
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
from moviepy.editor import AudioFileClip
|
| 7 |
+
import speech_recognition as sr
|
| 8 |
|
| 9 |
# Get all available voices
|
| 10 |
async def get_voices():
|
|
|
|
| 27 |
await communicate.save(tmp_path)
|
| 28 |
return tmp_path, None
|
| 29 |
|
| 30 |
+
# Function to analyze audio and get speech timing
|
| 31 |
+
def analyze_audio(audio_path):
|
| 32 |
+
recognizer = sr.Recognizer()
|
| 33 |
+
with sr.AudioFile(audio_path) as source:
|
| 34 |
+
audio_data = recognizer.record(source)
|
| 35 |
+
# Recognize speech using Google Web Speech API
|
| 36 |
+
try:
|
| 37 |
+
text = recognizer.recognize_google(audio_data)
|
| 38 |
+
return text
|
| 39 |
+
except sr.UnknownValueError:
|
| 40 |
+
return ""
|
| 41 |
+
except sr.RequestError:
|
| 42 |
+
return ""
|
| 43 |
+
|
| 44 |
# Generate SRT file based on user preferences
|
| 45 |
def generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph):
|
| 46 |
+
total_segments = (len(words) // words_per_line) // lines_per_paragraph + 1
|
| 47 |
+
segment_duration = audio_duration / total_segments # Calculate duration for each segment
|
| 48 |
+
|
|
|
|
|
|
|
| 49 |
current_time = 0
|
| 50 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 51 |
+
for i in range(0, len(words), words_per_line):
|
| 52 |
# Gather lines based on the defined words per line
|
| 53 |
lines = words[i:i + words_per_line]
|
| 54 |
line_text = ' '.join(lines)
|
|
|
|
| 86 |
audio_clip = AudioFileClip(audio_path)
|
| 87 |
audio_duration = audio_clip.duration
|
| 88 |
|
| 89 |
+
# Analyze audio to get the actual spoken text
|
| 90 |
+
spoken_text = analyze_audio(audio_path)
|
| 91 |
+
|
| 92 |
# Generate SRT file based on the entire text
|
| 93 |
base_name = os.path.splitext(audio_path)[0]
|
| 94 |
srt_path = f"{base_name}_subtitle.srt"
|