|
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 |
|
import rust_combiner |
|
import shutil |
|
|
|
|
|
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") |
|
|
|
|
|
for path in [BASE_DIR, AUDIO_DIR, CLIPS_DIR]: |
|
Path(path).mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
) |
|
print(f"Created {clip_file}") |
|
|
|
def video_com(lines): |
|
video_path = f"/tmp/video_{uuid.uuid4().hex}.mp4" |
|
clips = [] |
|
for id in range(len(lines)): |
|
clip = f"/app/data/video/clip{id}.mp4" |
|
clips.append(clip) |
|
|
|
video_path = rust_combiner.combine_clips(clips) |
|
return video_path |
|
|