Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,19 @@
|
|
| 1 |
from flask import Flask, request, jsonify, send_file
|
| 2 |
-
from moviepy.editor import ColorClip,ImageClip, concatenate_videoclips
|
| 3 |
import traceback
|
| 4 |
import uuid
|
| 5 |
import glob
|
| 6 |
-
import os
|
| 7 |
import asyncio
|
| 8 |
from image_fetcher import main
|
| 9 |
from video import create_text_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
|
@@ -84,18 +91,70 @@ def generate_video():
|
|
| 84 |
image_olst=[]
|
| 85 |
for id in range(len(lines)):
|
| 86 |
create_text_image(lines[id],id,image_olst)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
for img in image_files:
|
| 96 |
os.remove(img)
|
| 97 |
|
| 98 |
-
return send_file(
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
traceback.print_exc()
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, send_file
|
|
|
|
| 2 |
import traceback
|
| 3 |
import uuid
|
| 4 |
import glob
|
|
|
|
| 5 |
import asyncio
|
| 6 |
from image_fetcher import main
|
| 7 |
from video import create_text_image
|
| 8 |
+
from moviepy.editor import *
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import pytesseract
|
| 11 |
+
import numpy as np
|
| 12 |
+
from gtts import gTTS
|
| 13 |
+
from mutagen.mp3 import MP3
|
| 14 |
+
from gtts import gTTS
|
| 15 |
+
import os
|
| 16 |
+
from multiprocessing import Pool,cpu_count
|
| 17 |
|
| 18 |
app = Flask(__name__)
|
| 19 |
|
|
|
|
| 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)
|
| 147 |
+
clips = []
|
| 148 |
+
for id in range(len(lines)):
|
| 149 |
+
clip = VideoFileClip(f"clip{id}.mp4")
|
| 150 |
+
clips.append(clip)
|
| 151 |
+
|
| 152 |
+
final_video = concatenate_videoclips(clips)
|
| 153 |
+
final_video.write_videofile("final_output.mp4", fps=24)
|
| 154 |
for img in image_files:
|
| 155 |
os.remove(img)
|
| 156 |
|
| 157 |
+
return send_file("final_output.mp4", mimetype='video/mp4')
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
traceback.print_exc()
|