sreepathi-ravikumar commited on
Commit
b3b964d
·
verified ·
1 Parent(s): 87f10b8

Update video2.py

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