ogeti siva satyanarayana commited on
Commit
f5d12b4
Β·
1 Parent(s): e079ab1

Update video_enhancer.py

Browse files
Files changed (1) hide show
  1. video_enhancer.py +100 -47
video_enhancer.py CHANGED
@@ -1,47 +1,100 @@
1
- # video_enhancer.py
2
-
3
- from moviepy.editor import TextClip, CompositeVideoClip, AudioFileClip, VideoFileClip
4
- from moviepy.video.tools.subtitles import SubtitlesClip
5
- from gtts import gTTS
6
- from datetime import timedelta
7
- import os
8
-
9
- # Constants
10
- FONT = "Arial"
11
- FONT_SIZE = 36
12
- FONT_COLOR = "white"
13
- BG_COLOR = "black"
14
- BGM_PATH = "default_bgm.mp3" # bundled with your repo
15
-
16
- def generate_srt_from_text(text, output_srt_path, duration_per_line=4):
17
- """Generate .srt subtitles file from multi-line text."""
18
- lines = text.strip().split("\n")
19
- with open(output_srt_path, "w", encoding="utf-8") as f:
20
- for i, line in enumerate(lines):
21
- start_time = timedelta(seconds=i * duration_per_line)
22
- end_time = timedelta(seconds=(i + 1) * duration_per_line)
23
- f.write(f"{i+1}\n")
24
- f.write(f"{str(start_time)[:-3].replace('.', ',')} --> {str(end_time)[:-3].replace('.', ',')}\n")
25
- f.write(f"{line}\n\n")
26
-
27
- def create_subtitle_clips_from_srt(srt_path):
28
- """Convert .srt to a SubtitlesClip usable in moviepy."""
29
- generator = lambda txt: TextClip(txt, font=FONT, fontsize=FONT_SIZE, color=FONT_COLOR, bg_color=BG_COLOR)
30
- return SubtitlesClip(srt_path, generator)
31
-
32
- def enhance_video_with_subtitles_and_bgm(input_video_path, output_video_path, srt_path, bgm_path=BGM_PATH):
33
- video = VideoFileClip(input_video_path)
34
- subs = create_subtitle_clips_from_srt(srt_path)
35
- video = CompositeVideoClip([video, subs.set_position(('center','bottom'))])
36
-
37
- if os.path.exists(bgm_path):
38
- bgm = AudioFileClip(bgm_path).volumex(0.2)
39
- bgm = bgm.set_duration(video.duration)
40
- video = video.set_audio(bgm)
41
-
42
- video.write_videofile(output_video_path, codec="libx264", audio_codec="aac")
43
-
44
- # Example usage:
45
- # text = """Hello there\nThis is an AI-generated video\nThank you for watching"""
46
- # generate_srt_from_text(text, "output.srt")
47
- # enhance_video_with_subtitles_and_bgm("input.mp4", "final_output.mp4", "output.srt")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # streamlit_ui.py
2
+ import streamlit as st
3
+ import requests
4
+ import base64
5
+ import tempfile
6
+ from backend.subtitle_utils import generate_srt_from_text, enhance_video_with_subtitles_and_bgm
7
+
8
+ st.set_page_config(
9
+ page_title="Prompta - Text to Media Generator",
10
+ page_icon="πŸŽ™οΈ",
11
+ layout="wide",
12
+ initial_sidebar_state="expanded"
13
+ )
14
+ st.title("πŸŽ™οΈπŸ–ΌοΈπŸŽžοΈ Prompta - Text to Media Generator")
15
+
16
+ API_BASE = "http://localhost:8000"
17
+
18
+ def render_media(file_bytes, media_type, label):
19
+ b64 = base64.b64encode(file_bytes).decode()
20
+ if media_type == "audio":
21
+ st.audio(f"data:audio/wav;base64,{b64}", format="audio/wav")
22
+ elif media_type == "video":
23
+ st.video(f"data:video/mp4;base64,{b64}")
24
+ elif media_type == "image":
25
+ st.image(file_bytes, caption=label, use_column_width=True)
26
+
27
+ st.sidebar.header("πŸ› οΈ Settings")
28
+ TOKEN = st.sidebar.text_input("πŸ”‘ API Token", type="password")
29
+ HEADERS = {"Authorization": f"Bearer {TOKEN}"} if TOKEN else {}
30
+
31
+ tab = st.sidebar.radio("Select Task", ["Text to Audio", "Text to Image", "Text to Video"])
32
+
33
+ if tab == "Text to Audio":
34
+ st.subheader("🎀 Text to Audio")
35
+ text = st.text_area("Enter text")
36
+ voice = st.selectbox("Choose voice/language", ["en-US", "hi-IN", "te-IN", "ta-IN"])
37
+
38
+ if st.button("πŸ”Š Generate Audio"):
39
+ with st.spinner("Generating audio..."):
40
+ r = requests.post(f"{API_BASE}/audio/generate", json={"text": text, "voice": voice}, headers=HEADERS)
41
+ if r.status_code == 200:
42
+ render_media(r.content, "audio", "Generated Audio")
43
+ else:
44
+ st.error(f"❌ Failed: {r.json().get('detail')}")
45
+
46
+ elif tab == "Text to Image":
47
+ st.subheader("πŸ–ΌοΈ Text to Image")
48
+ prompt = st.text_area("Enter image prompt")
49
+ model = st.selectbox("Choose model", ["sdxl", "deepfloyd", "kandinsky"])
50
+
51
+ if st.button("🧠 Generate Image"):
52
+ with st.spinner("Generating image..."):
53
+ r = requests.post(f"{API_BASE}/image/generate", json={"prompt": prompt, "model": model}, headers=HEADERS)
54
+ if r.status_code == 200:
55
+ render_media(r.content, "image", "Generated Image")
56
+ else:
57
+ st.error(f"❌ Failed: {r.json().get('detail')}")
58
+
59
+ elif tab == "Text to Video":
60
+ st.subheader("🎞️ Text to Video")
61
+ prompt = st.text_area("Enter video prompt")
62
+ tone = st.selectbox("Tone", ["formal", "casual", "emotional", "documentary"])
63
+ domain = st.selectbox("Domain", ["health", "education", "governance", "entertainment"])
64
+ environment = st.selectbox("Environment", ["urban", "rural", "nature", "futuristic"])
65
+
66
+ transcript = st.text_area("Transcript (optional - for subtitles)", height=100)
67
+ enhance = st.checkbox("✨ Add Subtitles and Background Music")
68
+
69
+ if st.button("🎬 Generate Video"):
70
+ with st.spinner("Generating video..."):
71
+ r = requests.post(
72
+ f"{API_BASE}/video/generate",
73
+ json={"prompt": prompt, "tone": tone, "domain": domain, "environment": environment},
74
+ headers=HEADERS
75
+ )
76
+ if r.status_code == 200:
77
+ video_bytes = r.content
78
+ if enhance and transcript:
79
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_vid:
80
+ tmp_vid.write(video_bytes)
81
+ tmp_vid_path = tmp_vid.name
82
+
83
+ srt_path = generate_srt_from_text(transcript, output_path="streamlit_subs.srt")
84
+ enhanced_path = "streamlit_final_video.mp4"
85
+ enhance_video_with_subtitles_and_bgm(
86
+ video_path=tmp_vid_path,
87
+ srt_path=srt_path,
88
+ bgm_path="default_bgm.mp3",
89
+ output_path=enhanced_path
90
+ )
91
+
92
+ with open(enhanced_path, "rb") as f:
93
+ render_media(f.read(), "video", "Enhanced Video")
94
+ else:
95
+ render_media(video_bytes, "video", "Generated Video")
96
+ else:
97
+ st.error(f"❌ Failed: {r.json().get('detail')}")
98
+
99
+ st.sidebar.markdown("---")
100
+ st.sidebar.info("Built with ❀️ for AI GovTech Challenge 2025")