Update app.py
Browse files
app.py
CHANGED
|
@@ -197,27 +197,38 @@ def add_transcript_to_video(video_path, translated_json, output_path):
|
|
| 197 |
text_clips = []
|
| 198 |
|
| 199 |
logger.debug("Full translated_json: %s", translated_json)
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
font_path = "./NotoSansSC-Regular.ttf"
|
| 204 |
|
| 205 |
for entry in translated_json:
|
|
|
|
|
|
|
| 206 |
# Ensure `entry` is a dictionary with keys "start", "end", and "translated"
|
| 207 |
if isinstance(entry, dict) and "translated" in entry:
|
| 208 |
txt_clip = TextClip(
|
| 209 |
-
text=entry["translated"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
).with_start(entry["start"]).with_duration(entry["end"] - entry["start"]).with_position(('bottom')).with_opacity(0.7)
|
| 211 |
text_clips.append(txt_clip)
|
| 212 |
else:
|
| 213 |
raise ValueError(f"Invalid entry format: {entry}")
|
| 214 |
-
|
| 215 |
# Overlay all text clips on the original video
|
| 216 |
final_video = CompositeVideoClip([video] + text_clips)
|
| 217 |
|
| 218 |
# Write the result to a file
|
| 219 |
final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
|
| 220 |
-
|
| 221 |
# Mock functions for platform actions and analytics
|
| 222 |
def mock_post_to_platform(platform, content_title):
|
| 223 |
return f"Content '{content_title}' successfully posted on {platform}!"
|
|
|
|
| 197 |
text_clips = []
|
| 198 |
|
| 199 |
logger.debug("Full translated_json: %s", translated_json)
|
| 200 |
+
|
| 201 |
+
# Define relative font size based on video height (adjust this value as necessary)
|
| 202 |
+
subtitle_font_size = video.h // 15 # 1/15th of video height (you can tweak this for better results)
|
| 203 |
+
|
| 204 |
+
# Set maximum width for subtitle wrapping (80% of video width)
|
| 205 |
+
max_subtitle_width = video.w * 0.8 # Adjust this value as needed for wrapping
|
| 206 |
|
| 207 |
font_path = "./NotoSansSC-Regular.ttf"
|
| 208 |
|
| 209 |
for entry in translated_json:
|
| 210 |
+
logger.debug("Processing entry: %s", entry)
|
| 211 |
+
|
| 212 |
# Ensure `entry` is a dictionary with keys "start", "end", and "translated"
|
| 213 |
if isinstance(entry, dict) and "translated" in entry:
|
| 214 |
txt_clip = TextClip(
|
| 215 |
+
text=entry["translated"],
|
| 216 |
+
font=font_path,
|
| 217 |
+
method='caption',
|
| 218 |
+
color='yellow',
|
| 219 |
+
fontsize=subtitle_font_size, # Use relative font size
|
| 220 |
+
size=(max_subtitle_width, None) # Restrict the width to ensure wrapping
|
| 221 |
).with_start(entry["start"]).with_duration(entry["end"] - entry["start"]).with_position(('bottom')).with_opacity(0.7)
|
| 222 |
text_clips.append(txt_clip)
|
| 223 |
else:
|
| 224 |
raise ValueError(f"Invalid entry format: {entry}")
|
| 225 |
+
|
| 226 |
# Overlay all text clips on the original video
|
| 227 |
final_video = CompositeVideoClip([video] + text_clips)
|
| 228 |
|
| 229 |
# Write the result to a file
|
| 230 |
final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
|
| 231 |
+
|
| 232 |
# Mock functions for platform actions and analytics
|
| 233 |
def mock_post_to_platform(platform, content_title):
|
| 234 |
return f"Content '{content_title}' successfully posted on {platform}!"
|