sreepathi-ravikumar commited on
Commit
d36e81b
·
verified ·
1 Parent(s): 987a915

Update video2.py

Browse files
Files changed (1) hide show
  1. video2.py +66 -55
video2.py CHANGED
@@ -4,78 +4,89 @@ 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
  import moviepy.config as mpc
11
 
12
- # Optional: define and make a temp folder inside /tmp
 
13
  os.environ["TMPDIR"] = "/tmp"
14
- mpc.change_settings({"FFMPEG_BINARY": "ffmpeg"})
15
 
16
- # Tamil + English casual explanation
17
  IMAGE_DIR = "/tmp/images"
18
- os.makedirs(IMAGE_DIR, exist_ok=True)
19
-
20
  AUDIO_DIR = "/tmp/sound"
21
- os.makedirs(AUDIO_DIR, exist_ok=True)
22
-
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"
31
- audio_path=os.path.join(AUDIO_DIR,audio_name)
32
- tts.save(audio_path)
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)
81
- return video_path
 
 
 
 
 
 
 
4
  import numpy as np
5
  from gtts import gTTS
6
  from mutagen.mp3 import MP3
 
7
  import os
8
  import uuid
9
  import moviepy.config as mpc
10
 
11
+ # Set FFmpeg path explicitly
12
+ os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/bin/ffmpeg"
13
  os.environ["TMPDIR"] = "/tmp"
 
14
 
15
+ # Create directories with full permissions
16
  IMAGE_DIR = "/tmp/images"
 
 
17
  AUDIO_DIR = "/tmp/sound"
 
 
18
  CLIPS_DIR = "/tmp/video"
 
19
 
20
+ for dir_path in [IMAGE_DIR, AUDIO_DIR, CLIPS_DIR]:
21
+ os.makedirs(dir_path, exist_ok=True)
22
+ os.chmod(dir_path, 0o777) # Ensure full permissions
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ def video_func(id, lines):
25
+ print(f"Processing video for id={id}")
26
+ # Generate audio
27
+ tts = gTTS(text=lines[id], lang='ta', slow=False)
28
+ audio_name = f"audio{id}.mp3"
29
+ audio_path = os.path.join(AUDIO_DIR, audio_name)
30
+ print(f"Saving audio to {audio_path}")
31
+ tts.save(audio_path)
32
 
33
+ # Verify audio file exists
34
+ if not os.path.exists(audio_path):
35
+ raise FileNotFoundError(f"Audio file {audio_path} was not created")
36
+
37
+ audio = MP3(audio_path)
38
+ duration = audio.info.length
39
+ print(f"Audio duration: {duration} seconds")
40
 
41
+ # Set image path
42
+ IMAGE_PATH = os.path.join(IMAGE_DIR, f"slide{id}.png")
43
+ if not os.path.exists(IMAGE_PATH):
44
+ raise FileNotFoundError(f"Image file {IMAGE_PATH} not found")
45
+ print(f"Using image: {IMAGE_PATH}")
46
 
47
+ VIDEO_DURATION = duration
48
+ HIGHLIGHT_COLOR = (255, 255, 0) # Yellow highlight
49
+ HIGHLIGHT_OPACITY = 0.5 # Semi-transparent
50
 
51
+ # OCR step
52
+ img = Image.open(IMAGE_PATH)
53
+ data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
54
 
55
+ # Extract words and their positions
56
+ words = []
57
+ for i in range(len(data['text'])):
58
+ word = data['text'][i].strip()
59
+ if word and int(data['conf'][i]) > 60:
60
+ x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
61
+ words.append({'text': word, 'box': (x, y, w, h)})
62
+ print(f"Found {len(words)} words in image")
63
 
64
+ # Base image clip
65
+ image_clip = ImageClip(IMAGE_PATH).set_duration(VIDEO_DURATION)
 
66
 
67
+ # Highlight words one by one
68
+ n_words = len(words)
69
+ highlight_duration = VIDEO_DURATION / n_words if n_words > 0 else VIDEO_DURATION
70
 
71
+ highlight_clips = []
72
+ for i, word in enumerate(words):
73
+ x, y, w, h = word['box']
74
+ start = i * highlight_duration
75
+ end = start + highlight_duration
76
+ rect = ColorClip(size=(w, h), color=HIGHLIGHT_COLOR)
77
+ rect = rect.set_opacity(HIGHLIGHT_OPACITY).set_position((x, y)).set_start(start).set_end(end)
78
+ highlight_clips.append(rect)
79
 
80
+ # Final video
81
+ final_clip = CompositeVideoClip([image_clip] + highlight_clips)
82
+ audio = AudioFileClip(audio_path)
83
+ final_clip = final_clip.set_audio(audio)
84
+
85
+ clip_name = f"clip{id}.mp4"
86
+ video_path = os.path.join(CLIPS_DIR, clip_name)
87
+ print(f"Writing video to {video_path}")
88
+
89
+ # Write video with verbose output for debugging
90
+ final_clip.write_videofile(video_path, fps=24, verbose=True, logger='bar')
91
+ print(f"Video saved to {video_path}")
92
+ return video_path