Spaces:
Sleeping
Sleeping
Update video2.py
Browse files
video2.py
CHANGED
|
@@ -14,50 +14,58 @@ CLIPS_DIR = "/tmp/video"
|
|
| 14 |
for path in [IMAGE_DIR, AUDIO_DIR, CLIPS_DIR]:
|
| 15 |
os.makedirs(path, exist_ok=True)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
tts.save(audio_path)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
img = Image.open(img_path)
|
| 37 |
-
data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
|
| 44 |
-
words.append({'text': word, 'box': (x, y, w, h)})
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
highlight_duration = duration / n_words if n_words else duration
|
| 50 |
|
| 51 |
-
|
| 52 |
-
for i, word in enumerate(words):
|
| 53 |
-
x, y, w, h = word['box']
|
| 54 |
-
rect = ColorClip(size=(w, h), color=(255, 255, 0))
|
| 55 |
-
rect = rect.set_opacity(0.5).set_position((x, y))
|
| 56 |
-
rect = rect.set_start(i * highlight_duration).set_end((i + 1) * highlight_duration)
|
| 57 |
-
highlight_clips.append(rect)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
| 14 |
for path in [IMAGE_DIR, AUDIO_DIR, CLIPS_DIR]:
|
| 15 |
os.makedirs(path, exist_ok=True)
|
| 16 |
|
| 17 |
+
# Generate audio
|
| 18 |
+
# --- CONFIGURATION ---
|
| 19 |
+
def video_func(id,lines):
|
| 20 |
+
tts = gTTS(text=lines[id], lang='ta', slow=False)
|
| 21 |
+
audio_name = "audio"+str(id)+".mp3"
|
| 22 |
+
audio_path=os.path.join(AUDIO_DIR,audio_name)
|
| 23 |
+
tts.save(audio_path)
|
| 24 |
+
if os.path.exists(audio_path):
|
| 25 |
+
audio = MP3(audio_path)
|
| 26 |
+
duration = audio.info.length
|
| 27 |
+
IMAGE_PATH = os.path.join(IMAGE_DIR,f"slide{id}.png") # Ensure this path is correct
|
| 28 |
+
VIDEO_DURATION = duration # seconds
|
| 29 |
+
HIGHLIGHT_COLOR = (255, 255, 0) # Yellow highlight
|
| 30 |
+
HIGHLIGHT_OPACITY = 0.5 # Semi-transparent
|
| 31 |
|
| 32 |
+
# --- OCR STEP ---
|
| 33 |
+
img = Image.open(IMAGE_PATH)
|
| 34 |
+
data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
|
|
|
|
| 35 |
|
| 36 |
+
# Extract words and their positions
|
| 37 |
+
words = []
|
| 38 |
+
for i in range(len(data['text'])):
|
| 39 |
+
word = data['text'][i].strip()
|
| 40 |
+
if word and int(data['conf'][i]) > 60:
|
| 41 |
+
x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
|
| 42 |
+
words.append({'text': word, 'box': (x, y, w, h)})
|
| 43 |
|
| 44 |
+
# --- BASE IMAGE CLIP ---
|
| 45 |
+
image_clip = ImageClip(IMAGE_PATH).set_duration(VIDEO_DURATION)
|
| 46 |
|
| 47 |
+
# --- HIGHLIGHT WORDS ONE BY ONE ---
|
| 48 |
+
n_words = len(words)
|
| 49 |
+
highlight_duration = VIDEO_DURATION / n_words
|
| 50 |
|
| 51 |
+
highlight_clips = []
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
for i, word in enumerate(words):
|
| 54 |
+
x, y, w, h = word['box']
|
| 55 |
+
start = i * highlight_duration
|
| 56 |
+
end = start + highlight_duration
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Create highlight rectangle
|
| 59 |
+
rect = ColorClip(size=(w, h), color=HIGHLIGHT_COLOR)
|
| 60 |
+
rect = rect.set_opacity(HIGHLIGHT_OPACITY).set_position((x, y)).set_start(start).set_end(end)
|
| 61 |
|
| 62 |
+
highlight_clips.append(rect)
|
|
|
|
| 63 |
|
| 64 |
+
# --- FINAL VIDEO --
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
final_clip = CompositeVideoClip([image_clip] + highlight_clips)
|
| 67 |
+
audio = AudioFileClip(audio_path)
|
| 68 |
+
final_clip = final_clip.set_audio(audio)
|
| 69 |
+
clip_name = "clip"+str(id)+".mp4"
|
| 70 |
+
video_path=os.path.join(CLIPS_DIR,clip_name)
|
| 71 |
+
final_clip.write_videofile(video_path, fps=24)
|