Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesrequired files for web app execution
- app.py +95 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import asyncio
|
| 3 |
+
import re
|
| 4 |
+
import numpy as np
|
| 5 |
+
import whisper
|
| 6 |
+
import edge_tts
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from moviepy import VideoFileClip, TextClip, CompositeVideoClip, AudioFileClip, vfx, concatenate_audioclips, AudioArrayClip, CompositeAudioClip
|
| 9 |
+
|
| 10 |
+
# TikTok-style Pop-up Animation
|
| 11 |
+
def apply_tiktok_animation(clip, duration=0.2):
|
| 12 |
+
def zoom(t):
|
| 13 |
+
if t < duration / 2:
|
| 14 |
+
return 0.5 + 1.4 * (t / duration)
|
| 15 |
+
elif t < duration:
|
| 16 |
+
return 1.2 - 0.4 * ((t - duration/2) / (duration/2))
|
| 17 |
+
return 1.0
|
| 18 |
+
return clip.with_effects([vfx.Resize(zoom)])
|
| 19 |
+
|
| 20 |
+
async def generate_video(text, video_path, voice, voice_speed, font_size, font_color, stroke_width, stroke_color):
|
| 21 |
+
try:
|
| 22 |
+
# 1. Generate Speech
|
| 23 |
+
parts = re.split(r'\[PAUSE\s+(\d+(?:\.\d+)?)\]', text)
|
| 24 |
+
audio_clips = []
|
| 25 |
+
temp_files = []
|
| 26 |
+
for i, part in enumerate(parts):
|
| 27 |
+
if i % 2 == 0:
|
| 28 |
+
if part.strip():
|
| 29 |
+
chunk_file = f"temp_chunk_{i}.mp3"
|
| 30 |
+
communicate = edge_tts.Communicate(part.strip(), voice, rate=voice_speed)
|
| 31 |
+
await communicate.save(chunk_file)
|
| 32 |
+
audio_clips.append(AudioFileClip(chunk_file))
|
| 33 |
+
temp_files.append(chunk_file)
|
| 34 |
+
else:
|
| 35 |
+
silence = AudioArrayClip(np.zeros((int(44100 * float(part)), 2)), fps=44100)
|
| 36 |
+
audio_clips.append(silence)
|
| 37 |
+
|
| 38 |
+
combined_audio = concatenate_audioclips(audio_clips)
|
| 39 |
+
|
| 40 |
+
# 2. Process Video
|
| 41 |
+
video_clip = VideoFileClip(video_path)
|
| 42 |
+
if video_clip.duration < combined_audio.duration:
|
| 43 |
+
final_video_clip = video_clip.with_effects([vfx.Loop(duration=combined_audio.duration)])
|
| 44 |
+
else:
|
| 45 |
+
final_video_clip = video_clip.subclipped(0, combined_audio.duration)
|
| 46 |
+
|
| 47 |
+
if final_video_clip.audio is not None:
|
| 48 |
+
bg_audio = final_video_clip.audio.with_volume_scaled(0.5)
|
| 49 |
+
final_video_clip = final_video_clip.with_audio(CompositeAudioClip([bg_audio, combined_audio]))
|
| 50 |
+
else:
|
| 51 |
+
final_video_clip = final_video_clip.with_audio(combined_audio)
|
| 52 |
+
|
| 53 |
+
# 3. Subtitles
|
| 54 |
+
temp_audio = "temp_full.mp3"
|
| 55 |
+
combined_audio.write_audiofile(temp_audio, logger=None)
|
| 56 |
+
model = whisper.load_model("tiny") # Using 'tiny' for faster processing in free cloud tier
|
| 57 |
+
result = model.transcribe(temp_audio, word_timestamps=True)
|
| 58 |
+
|
| 59 |
+
subtitle_clips = []
|
| 60 |
+
for segment in result['segments']:
|
| 61 |
+
for word_info in segment.get('words', []):
|
| 62 |
+
txt_clip = TextClip(
|
| 63 |
+
text=word_info['word'].strip().upper(),
|
| 64 |
+
font_size=font_size, color=font_color,
|
| 65 |
+
stroke_color=stroke_color, stroke_width=stroke_width,
|
| 66 |
+
method='caption', size=(int(final_video_clip.w * 0.9), None)
|
| 67 |
+
).with_start(word_info['start']).with_duration(word_info['end'] - word_info['start']).with_position(('center', int(final_video_clip.h * 0.65)))
|
| 68 |
+
|
| 69 |
+
subtitle_clips.append(apply_tiktok_animation(txt_clip))
|
| 70 |
+
|
| 71 |
+
# 4. Export
|
| 72 |
+
output_path = "output_video.mp4"
|
| 73 |
+
final_video = CompositeVideoClip([final_video_clip] + subtitle_clips)
|
| 74 |
+
final_video.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=24, logger=None)
|
| 75 |
+
|
| 76 |
+
return output_path, "Done!"
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return None, str(e)
|
| 79 |
+
|
| 80 |
+
# UI
|
| 81 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 82 |
+
gr.Markdown("# 🎬 tts-captions-app")
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column():
|
| 85 |
+
txt = gr.Textbox(label="Text", value="HELLO WORLD! [PAUSE 0.5] Testing Hugging Face Spaces!")
|
| 86 |
+
vid = gr.File(label="Background Video", type="filepath")
|
| 87 |
+
voice = gr.Dropdown(label="Voice", choices=["en-US-ChristopherNeural", "en-US-AriaNeural"], value="en-US-ChristopherNeural")
|
| 88 |
+
btn = gr.Button("Generate")
|
| 89 |
+
with gr.Column():
|
| 90 |
+
out_v = gr.Video()
|
| 91 |
+
out_t = gr.Textbox(label="Log")
|
| 92 |
+
|
| 93 |
+
btn.click(generate_video, [txt, vid, voice, gr.State("+20%"), gr.State(80), gr.State("white"), gr.State(10), gr.State("black")], [out_v, out_t])
|
| 94 |
+
|
| 95 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
moviepy>=2.2.1
|
| 3 |
+
edge-tts
|
| 4 |
+
openai-whisper
|
| 5 |
+
numpy
|
| 6 |
+
torch
|
| 7 |
+
asyncio
|