Spaces:
Sleeping
Sleeping
ogeti siva satyanarayana
commited on
Commit
·
69971c2
1
Parent(s):
fe21d8b
Create subtitle_utils.py
Browse files- backend/subtitle_utils.py +84 -0
backend/subtitle_utils.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from moviepy.editor import VideoFileClip, AudioFileClip, TextClip, CompositeVideoClip
|
| 2 |
+
from moviepy.video.tools.subtitles import SubtitlesClip
|
| 3 |
+
import srt
|
| 4 |
+
from datetime import timedelta
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def export_srt(text, duration=10, words_per_caption=6, output_path="output.srt"):
|
| 9 |
+
"""
|
| 10 |
+
Converts text into SRT subtitles and saves to output_path.
|
| 11 |
+
"""
|
| 12 |
+
lines = []
|
| 13 |
+
words = text.split()
|
| 14 |
+
start = 0
|
| 15 |
+
index = 1
|
| 16 |
+
while start < len(words):
|
| 17 |
+
end = start + words_per_caption
|
| 18 |
+
chunk = words[start:end]
|
| 19 |
+
content = " ".join(chunk)
|
| 20 |
+
start_time = timedelta(seconds=(index - 1) * duration)
|
| 21 |
+
end_time = timedelta(seconds=index * duration)
|
| 22 |
+
sub = srt.Subtitle(index=index, start=start_time, end=end_time, content=content)
|
| 23 |
+
lines.append(sub)
|
| 24 |
+
start += words_per_caption
|
| 25 |
+
index += 1
|
| 26 |
+
|
| 27 |
+
srt_data = srt.compose(lines)
|
| 28 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 29 |
+
f.write(srt_data)
|
| 30 |
+
return output_path
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def add_subtitles_and_bgm(
|
| 34 |
+
video_path,
|
| 35 |
+
srt_path,
|
| 36 |
+
bgm_path,
|
| 37 |
+
output_path="enhanced_output.mp4",
|
| 38 |
+
font="Arial-Bold",
|
| 39 |
+
font_size=36,
|
| 40 |
+
font_color="white",
|
| 41 |
+
subtitle_position=("center", "bottom")
|
| 42 |
+
):
|
| 43 |
+
"""
|
| 44 |
+
Adds subtitles from .srt and background music to the given video.
|
| 45 |
+
"""
|
| 46 |
+
# Load video
|
| 47 |
+
video = VideoFileClip(video_path)
|
| 48 |
+
|
| 49 |
+
# Parse .srt file
|
| 50 |
+
with open(srt_path, "r", encoding="utf-8") as f:
|
| 51 |
+
subtitles = list(srt.parse(f.read()))
|
| 52 |
+
|
| 53 |
+
# Create subtitle clips
|
| 54 |
+
def make_textclip(txt):
|
| 55 |
+
return TextClip(txt, font=font, fontsize=font_size, color=font_color, stroke_color='black', stroke_width=2)
|
| 56 |
+
|
| 57 |
+
subtitle_clips = []
|
| 58 |
+
for sub in subtitles:
|
| 59 |
+
txt_clip = (make_textclip(sub.content)
|
| 60 |
+
.set_position(subtitle_position)
|
| 61 |
+
.set_start(sub.start.total_seconds())
|
| 62 |
+
.set_duration((sub.end - sub.start).total_seconds()))
|
| 63 |
+
subtitle_clips.append(txt_clip)
|
| 64 |
+
|
| 65 |
+
# Background music
|
| 66 |
+
if os.path.exists(bgm_path):
|
| 67 |
+
bgm = AudioFileClip(bgm_path).volumex(0.2) # reduce volume
|
| 68 |
+
bgm = bgm.set_duration(video.duration)
|
| 69 |
+
final_audio = video.audio.volumex(0.8).audio_fadein(1).audio_fadeout(1).set_duration(video.duration)
|
| 70 |
+
final_audio = final_audio.set_audio(bgm)
|
| 71 |
+
else:
|
| 72 |
+
final_audio = video.audio
|
| 73 |
+
|
| 74 |
+
final = CompositeVideoClip([video, *subtitle_clips])
|
| 75 |
+
final = final.set_audio(final_audio)
|
| 76 |
+
|
| 77 |
+
# Export final video
|
| 78 |
+
final.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=video.fps)
|
| 79 |
+
return output_path
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# Aliases for compatibility with streamlit_ui.py
|
| 83 |
+
generate_srt_from_text = export_srt
|
| 84 |
+
enhance_video_with_subtitles_and_bgm = add_subtitles_and_bgm
|