sreepathi-ravikumar commited on
Commit
9441663
·
verified ·
1 Parent(s): ee90b65

Update video2.py

Browse files
Files changed (1) hide show
  1. video2.py +45 -37
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
- def video_func(id, lines):
18
- print(f"Processing video for id={id}")
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # Generate audio
21
- tts = gTTS(text=lines[id], lang='ta', slow=False)
22
- audio_path = os.path.join(AUDIO_DIR, f"audio{id}.mp3")
23
- tts.save(audio_path)
24
 
25
- if not os.path.exists(audio_path):
26
- raise FileNotFoundError(f"Audio file not created: {audio_path}")
 
 
 
 
 
27
 
28
- audio = MP3(audio_path)
29
- duration = audio.info.length
30
 
31
- img_path = os.path.join(IMAGE_DIR, f"slide{id}.png")
32
- if not os.path.exists(img_path):
33
- raise FileNotFoundError(f"Missing image: {img_path}")
34
 
35
- # OCR
36
- img = Image.open(img_path)
37
- data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
38
 
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
- image_clip = ImageClip(img_path).set_duration(duration)
 
 
47
 
48
- n_words = len(words)
49
- highlight_duration = duration / n_words if n_words else duration
50
 
51
- highlight_clips = []
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
- final = CompositeVideoClip([image_clip] + highlight_clips)
60
- final = final.set_audio(AudioFileClip(audio_path))
61
-
62
- video_path = os.path.join(CLIPS_DIR, f"clip{id}.mp4")
63
- final.write_videofile(video_path, fps=24, verbose=True)
 
 
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)