sreepathi-ravikumar commited on
Commit
6f4f8a2
·
verified ·
1 Parent(s): 43d36df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -50
app.py CHANGED
@@ -22,6 +22,56 @@ def home():
22
  return "Flask Video Generator is Running"
23
 
24
  @app.route("/generate", methods=["POST"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def generate_video():
26
  try:
27
  data = request.get_json()
@@ -91,56 +141,6 @@ def generate_video():
91
  image_olst=[]
92
  for id in range(len(lines)):
93
  create_text_image(lines[id],id,image_olst)
94
- def video_func(id):
95
- tts = gTTS(text=lines[id], lang='ta', slow=False)
96
- filename = "audio"+str(id)+".mp3"
97
- tts.save(filename)
98
- if os.path.exists(filename):
99
- audio = MP3(filename)
100
- duration = audio.info.length
101
- IMAGE_PATH = "images/slide"+str(id)+".png" # Ensure this path is correct
102
- VIDEO_DURATION = duration # seconds
103
- HIGHLIGHT_COLOR = (255, 255, 0) # Yellow highlight
104
- HIGHLIGHT_OPACITY = 0.5 # Semi-transparent
105
-
106
- # --- OCR STEP ---
107
- img = Image.open(IMAGE_PATH)
108
- data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
109
-
110
- # Extract words and their positions
111
- words = []
112
- for i in range(len(data['text'])):
113
- word = data['text'][i].strip()
114
- if word and int(data['conf'][i]) > 60:
115
- x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
116
- words.append({'text': word, 'box': (x, y, w, h)})
117
-
118
- # --- BASE IMAGE CLIP ---
119
- image_clip = ImageClip(IMAGE_PATH).set_duration(VIDEO_DURATION)
120
-
121
- # --- HIGHLIGHT WORDS ONE BY ONE ---
122
- n_words = len(words)
123
- highlight_duration = VIDEO_DURATION / n_words
124
-
125
- highlight_clips = []
126
-
127
- for i, word in enumerate(words):
128
- x, y, w, h = word['box']
129
- start = i * highlight_duration
130
- end = start + highlight_duration
131
-
132
- # Create highlight rectangle
133
- rect = ColorClip(size=(w, h), color=HIGHLIGHT_COLOR)
134
- rect = rect.set_opacity(HIGHLIGHT_OPACITY).set_position((x, y)).set_start(start).set_end(end)
135
-
136
- highlight_clips.append(rect)
137
-
138
- # --- FINAL VIDEO --
139
-
140
- final_clip = CompositeVideoClip([image_clip] + highlight_clips)
141
- audio = AudioFileClip(filename)
142
- final_clip = final_clip.set_audio(audio)
143
- final_clip.write_videofile("clip"+str(id)+".mp4", fps=24)
144
  id=list(range(len(lines)))
145
  with Pool(processes=cpu_count()) as pool:
146
  pool.map(video_func,id)
 
22
  return "Flask Video Generator is Running"
23
 
24
  @app.route("/generate", methods=["POST"])
25
+ def video_func(id):
26
+ tts = gTTS(text=lines[id], lang='ta', slow=False)
27
+ filename = "audio"+str(id)+".mp3"
28
+ tts.save(filename)
29
+ if os.path.exists(filename):
30
+ audio = MP3(filename)
31
+ duration = audio.info.length
32
+ IMAGE_PATH = "images/slide"+str(id)+".png" # Ensure this path is correct
33
+ VIDEO_DURATION = duration # seconds
34
+ HIGHLIGHT_COLOR = (255, 255, 0) # Yellow highlight
35
+ HIGHLIGHT_OPACITY = 0.5 # Semi-transparent
36
+
37
+ # --- OCR STEP ---
38
+ img = Image.open(IMAGE_PATH)
39
+ data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
40
+
41
+ # Extract words and their positions
42
+ words = []
43
+ for i in range(len(data['text'])):
44
+ word = data['text'][i].strip()
45
+ if word and int(data['conf'][i]) > 60:
46
+ x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
47
+ words.append({'text': word, 'box': (x, y, w, h)})
48
+
49
+ # --- BASE IMAGE CLIP ---
50
+ image_clip = ImageClip(IMAGE_PATH).set_duration(VIDEO_DURATION)
51
+
52
+ # --- HIGHLIGHT WORDS ONE BY ONE ---
53
+ n_words = len(words)
54
+ highlight_duration = VIDEO_DURATION / n_words
55
+
56
+ highlight_clips = []
57
+
58
+ for i, word in enumerate(words):
59
+ x, y, w, h = word['box']
60
+ start = i * highlight_duration
61
+ end = start + highlight_duration
62
+
63
+ # Create highlight rectangle
64
+ rect = ColorClip(size=(w, h), color=HIGHLIGHT_COLOR)
65
+ rect = rect.set_opacity(HIGHLIGHT_OPACITY).set_position((x, y)).set_start(start).set_end(end)
66
+
67
+ highlight_clips.append(rect)
68
+
69
+ # --- FINAL VIDEO --
70
+
71
+ final_clip = CompositeVideoClip([image_clip] + highlight_clips)
72
+ audio = AudioFileClip(filename)
73
+ final_clip = final_clip.set_audio(audio)
74
+ final_clip.write_videofile("clip"+str(id)+".mp4", fps=24)
75
  def generate_video():
76
  try:
77
  data = request.get_json()
 
141
  image_olst=[]
142
  for id in range(len(lines)):
143
  create_text_image(lines[id],id,image_olst)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  id=list(range(len(lines)))
145
  with Pool(processes=cpu_count()) as pool:
146
  pool.map(video_func,id)