Spaces:
Sleeping
Sleeping
Update video2.py
Browse files
video2.py
CHANGED
|
@@ -5,88 +5,59 @@ 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 |
-
#
|
| 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
|
| 21 |
-
os.makedirs(
|
| 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 |
-
|
| 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}
|
| 36 |
-
|
| 37 |
audio = MP3(audio_path)
|
| 38 |
duration = audio.info.length
|
| 39 |
-
print(f"Audio duration: {duration} seconds")
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
raise FileNotFoundError(f"Image file {IMAGE_PATH} not found")
|
| 45 |
-
print(f"Using image: {IMAGE_PATH}")
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 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 |
-
|
| 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 =
|
| 70 |
|
| 71 |
highlight_clips = []
|
| 72 |
for i, word in enumerate(words):
|
| 73 |
x, y, w, h = word['box']
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
rect =
|
| 77 |
-
rect = rect.set_opacity(HIGHLIGHT_OPACITY).set_position((x, y)).set_start(start).set_end(end)
|
| 78 |
highlight_clips.append(rect)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 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
|
|
|
|
| 5 |
from gtts import gTTS
|
| 6 |
from mutagen.mp3 import MP3
|
| 7 |
import os
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Define paths
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
IMAGE_DIR = "/tmp/images"
|
| 11 |
AUDIO_DIR = "/tmp/sound"
|
| 12 |
CLIPS_DIR = "/tmp/video"
|
| 13 |
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|