Spaces:
Sleeping
Sleeping
| from moviepy.editor import * | |
| from PIL import Image | |
| import pytesseract | |
| import numpy as np | |
| from gtts import gTTS | |
| from mutagen.mp3 import MP3 | |
| import uuid | |
| import os | |
| from pathlib import Path | |
| import rust_highlight | |
| # Use /app/data which we created with proper permissions | |
| BASE_DIR = "/app/data" | |
| IMAGE_DIR = "/tmp/images" | |
| os.makedirs(IMAGE_DIR, exist_ok=True) | |
| AUDIO_DIR = os.path.join(BASE_DIR, "sound") | |
| CLIPS_DIR = os.path.join(BASE_DIR, "video") | |
| # Create directories (no chmod needed) | |
| for path in [BASE_DIR, AUDIO_DIR, CLIPS_DIR]: | |
| Path(path).mkdir(parents=True, exist_ok=True) | |
| # Generate audio | |
| def audio_func(id,lines): | |
| tts = gTTS(text=lines[id], lang='en', slow=False) | |
| audio_name = "audio"+str(id)+".mp3" | |
| audio_path=os.path.join(AUDIO_DIR,audio_name) | |
| tts.save(audio_path) | |
| if os.path.exists(audio_path): | |
| audio = MP3(audio_path) | |
| duration = audio.info.length | |
| return duration,audio_path | |
| # --- CONFIGURATION --- | |
| def video_func(id, lines): | |
| duration, audio_path = audio_func(id, lines) | |
| image_path = os.path.join(IMAGE_DIR, f"slide{id}.png") | |
| img = Image.open(image_path) | |
| data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT) | |
| words = [] | |
| for i in range(len(data['text'])): | |
| txt = data['text'][i].strip() | |
| if txt and int(data['conf'][i]) > 60: | |
| box = ( | |
| data['left'][i], | |
| data['top'][i], | |
| data['width'][i], | |
| data['height'][i], | |
| ) | |
| words.append((txt, box)) | |
| clip_file = rust_highlight.render_video( | |
| id=id, | |
| image_path=image_path, | |
| audio_path=audio_path, | |
| duration=duration, | |
| words=words, | |
| output_dir=CLIPS_DIR # Add your output directory here | |
| ) | |
| print(f"Created {clip_file}") | |
| return f"/app/data/video/clip{id}.mp4" | |