File size: 2,516 Bytes
c9030d6
 
 
 
 
 
 
a9a000b
5796090
a9a000b
 
7fee81b
 
40ff02c
 
c9030d6
a9a000b
7fee81b
a9a000b
21107e9
40ff02c
9441663
 
 
1a4258f
dad8135
9441663
 
 
 
 
 
 
 
 
 
ee90b65
9441663
 
 
b3b964d
9441663
 
 
 
 
 
 
ee90b65
9441663
 
b3b964d
9441663
 
 
b3b964d
9441663
b3b964d
9441663
 
 
 
b3b964d
9441663
 
 
b3b964d
9441663
b3b964d
9441663
b3b964d
9441663
 
 
 
 
09a8cbc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from moviepy.editor import *
from PIL import Image
import pytesseract
import numpy as np
from gtts import gTTS
from mutagen.mp3 import MP3
import os
from pathlib import Path

# Use /app/data which we created with proper permissions
BASE_DIR = "/app/data"
IMAGE_DIR = "/tmp/images"
os.makedirs(IMAGE_DIR, exist_ok=True)
AUDIO_DIR = os.path.join(BASE_DIR, "sound")
CLIPS_DIR = os.path.join(BASE_DIR, "video")

# Create directories (no chmod needed)
for path in [BASE_DIR, AUDIO_DIR, CLIPS_DIR]:
    Path(path).mkdir(parents=True, exist_ok=True)
    

# Generate audio
# --- CONFIGURATION ---
def video_func(id,lines):
  print(id,lines[id])
  tts = gTTS(text=lines[id], lang='en', slow=False)
  audio_name = "audio"+str(id)+".mp3"
  audio_path=os.path.join(AUDIO_DIR,audio_name)
  tts.save(audio_path)
  if os.path.exists(audio_path):
      audio = MP3(audio_path)
      duration = audio.info.length
  IMAGE_PATH = os.path.join(IMAGE_DIR,f"slide{id}.png")   # Ensure this path is correct
  VIDEO_DURATION = duration               # seconds
  HIGHLIGHT_COLOR = (255, 255, 0)   # Yellow highlight
  HIGHLIGHT_OPACITY = 0.5           # Semi-transparent

  # --- OCR STEP ---
  img = Image.open(IMAGE_PATH)
  data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)

  # Extract words and their positions
  words = []
  for i in range(len(data['text'])):
      word = data['text'][i].strip()
      if word and int(data['conf'][i]) > 60:
          x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
          words.append({'text': word, 'box': (x, y, w, h)})

  # --- BASE IMAGE CLIP ---
  image_clip = ImageClip(IMAGE_PATH).set_duration(VIDEO_DURATION)

  # --- HIGHLIGHT WORDS ONE BY ONE ---
  n_words = len(words)
  highlight_duration = VIDEO_DURATION / n_words

  highlight_clips = []

  for i, word in enumerate(words):
      x, y, w, h = word['box']
      start = i * highlight_duration
      end = start + highlight_duration

      # Create highlight rectangle
      rect = ColorClip(size=(w, h), color=HIGHLIGHT_COLOR)
      rect = rect.set_opacity(HIGHLIGHT_OPACITY).set_position((x, y)).set_start(start).set_end(end)

      highlight_clips.append(rect)

  # --- FINAL VIDEO --

  final_clip = CompositeVideoClip([image_clip] + highlight_clips)
  audio = AudioFileClip(audio_path)
  final_clip = final_clip.set_audio(audio)
  clip_name = "clip"+str(id)+".mp4"
  video_path=os.path.join(CLIPS_DIR,clip_name)
  final_clip.write_videofile(video_path, fps=24,audio=True)