sreepathi-ravikumar commited on
Commit
c9030d6
·
verified ·
1 Parent(s): 7e30387

Create video2.py

Browse files
Files changed (1) hide show
  1. video2.py +73 -0
video2.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from moviepy.editor import *
2
+ from PIL import Image
3
+ import pytesseract
4
+ import numpy as np
5
+ from gtts import gTTS
6
+ from mutagen.mp3 import MP3
7
+ from gtts import gTTS
8
+ import os
9
+ import uuid
10
+
11
+ # Tamil + English casual explanation
12
+ AUDIO_DIR = "/tmp/sound"
13
+ os.makedirs(AUDIO_DIR, exist_ok=True)
14
+
15
+ CLIPS_DIR = "/tmp/video"
16
+ os.makedirs(CLIPS_DIR, exist_ok=True)
17
+
18
+
19
+ # Generate audio
20
+ # --- CONFIGURATION ---
21
+ def video_func(id):
22
+ tts = gTTS(text=lines[id], lang='ta', slow=False)
23
+ audio_name = "audio"+str(id)+".mp3"
24
+ audio_path=os.path.join(AUDIO_DIR,audio_name)
25
+ tts.save(audio_path)
26
+ if os.path.exists(audio_path):
27
+ audio = MP3(audio_path)
28
+ duration = audio.info.length
29
+ IMAGE_PATH = "images/slide"+str(id)+".png" # Ensure this path is correct
30
+ VIDEO_DURATION = duration # seconds
31
+ HIGHLIGHT_COLOR = (255, 255, 0) # Yellow highlight
32
+ HIGHLIGHT_OPACITY = 0.5 # Semi-transparent
33
+
34
+ # --- OCR STEP ---
35
+ img = Image.open(IMAGE_PATH)
36
+ data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
37
+
38
+ # Extract words and their positions
39
+ words = []
40
+ for i in range(len(data['text'])):
41
+ word = data['text'][i].strip()
42
+ if word and int(data['conf'][i]) > 60:
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
+ # --- BASE IMAGE CLIP ---
47
+ image_clip = ImageClip(IMAGE_PATH).set_duration(VIDEO_DURATION)
48
+
49
+ # --- HIGHLIGHT WORDS ONE BY ONE ---
50
+ n_words = len(words)
51
+ highlight_duration = VIDEO_DURATION / n_words
52
+
53
+ highlight_clips = []
54
+
55
+ for i, word in enumerate(words):
56
+ x, y, w, h = word['box']
57
+ start = i * highlight_duration
58
+ end = start + highlight_duration
59
+
60
+ # Create highlight rectangle
61
+ rect = ColorClip(size=(w, h), color=HIGHLIGHT_COLOR)
62
+ rect = rect.set_opacity(HIGHLIGHT_OPACITY).set_position((x, y)).set_start(start).set_end(end)
63
+
64
+ highlight_clips.append(rect)
65
+
66
+ # --- FINAL VIDEO --
67
+
68
+ final_clip = CompositeVideoClip([image_clip] + highlight_clips)
69
+ audio = AudioFileClip(audio_path)
70
+ final_clip = final_clip.set_audio(audio)
71
+ clip_name = "clip"+str(id)+".mp3"
72
+ video_path=os.path.join(CLIPS_DIR,clip_name)
73
+ final_clip.write_videofile(video_path, fps=24)