viral-clip-generator / utils /caption_generator.py
yasvanthkumar's picture
Create utils/caption_generator.py
20797ac verified
import re
class CaptionGenerator:
def __init__(self):
self.caption_styles = {
'Bold Yellow with Pop': {
'color': 'yellow',
'stroke_color': 'black',
'stroke_width': 3,
'fontsize': 70,
'animation': 'pop'
},
'Red Bold with Shake': {
'color': 'red',
'stroke_color': 'white',
'stroke_width': 3,
'fontsize': 75,
'animation': 'shake'
},
'White Bold with Glow': {
'color': 'white',
'stroke_color': 'black',
'stroke_width': 4,
'fontsize': 70,
'animation': 'glow'
}
}
def generate(self, text, emotion):
"""Generate caption data for video"""
# Split text into caption chunks (max 3-4 words per caption)
words = text.split()
captions = []
chunk_size = 4
duration_per_chunk = 2.0
for i in range(0, len(words), chunk_size):
chunk = ' '.join(words[i:i+chunk_size])
captions.append({
'text': chunk.upper(),
'start': i * duration_per_chunk / chunk_size,
'duration': duration_per_chunk
})
style_name = self._get_style_for_emotion(emotion)
style = self.caption_styles.get(style_name, self.caption_styles['White Bold with Glow'])
return {
'captions': captions,
'style': style_name,
**style
}
def _get_style_for_emotion(self, emotion):
"""Map emotion to caption style"""
emotion_style_map = {
'joy': 'Bold Yellow with Pop',
'surprise': 'Red Bold with Shake',
'anger': 'Red Bold with Shake',
'neutral': 'White Bold with Glow'
}
return emotion_style_map.get(emotion, 'White Bold with Glow')
def generate_description(self, text, hook):
"""Generate YouTube description with hashtags"""
# Extract key themes
text_lower = text.lower()
# Base description
description = f"{hook}\n\n"
description += "💥 This moment is PURE GOLD! Watch until the end!\n\n"
# Add call to action
cta_options = [
"❤️ Drop a like if this amazed you!",
"🔥 Comment your thoughts below!",
"💬 Tag someone who needs to see this!",
"⚡ Share this with your friends!"
]
description += f"{cta_options[0]}\n\n"
# Generate relevant hashtags
hashtags = self._generate_hashtags(text_lower)
description += ' '.join(hashtags)
return description
def _generate_hashtags(self, text):
"""Generate relevant hashtags based on content"""
base_tags = ['#shorts', '#viral', '#trending', '#fyp']
content_tags = []
# Content-based tags
if any(word in text for word in ['tip', 'how', 'tutorial', 'learn']):
content_tags.extend(['#tutorial', '#howto', '#tips', '#educational'])
if any(word in text for word in ['funny', 'laugh', 'hilarious', 'comedy']):
content_tags.extend(['#funny', '#comedy', '#lol', '#humor'])
if any(word in text for word in ['amazing', 'incredible', 'wow', 'unbelievable']):
content_tags.extend(['#amazing', '#mindblowing', '#incredible'])
if any(word in text for word in ['fail', 'mistake', 'wrong']):
content_tags.extend(['#fail', '#fails', '#epicfail'])
if any(word in text for word in ['game', 'gaming', 'play']):
content_tags.extend(['#gaming', '#gamer', '#gameplay'])
if not content_tags:
content_tags = ['#content', '#video', '#watch']
# Combine and limit to 15 tags
all_tags = base_tags + content_tags[:11]
return all_tags[:15]