| import uuid |
| import os |
| from moviepy.editor import ( |
| VideoFileClip, |
| ImageClip, |
| TextClip, |
| AudioFileClip, |
| CompositeVideoClip, |
| ColorClip |
| ) |
|
|
|
|
| def generate_sample_video(): |
| output_path = f"/app/generated_{uuid.uuid4().hex[:6]}.mp4" |
| bg_clip = ColorClip(size=(1280, 720), color=(10, 10, 10), duration=5) |
| txt_clip = TextClip( |
| "This is MoviePy on Hugging Face", fontsize=70, color='white', font="DejaVu-Serif" |
| ).set_duration(5).set_position(("center", "center")) |
|
|
| final_clip = CompositeVideoClip([bg_clip, txt_clip]) |
| final_clip.write_videofile(output_path, fps=24, codec="libx264") |
| return output_path |
|
|
|
|
| async def combine_video_audio(video_file, audio_file): |
| video_path = f"/app/temp_video_{uuid.uuid4().hex[:6]}.mp4" |
| audio_path = f"/app/temp_audio_{uuid.uuid4().hex[:6]}.mp3" |
| output_path = f"/app/output_{uuid.uuid4().hex[:6]}.mp4" |
|
|
| with open(video_path, "wb") as f: |
| f.write(await video_file.read()) |
| with open(audio_path, "wb") as f: |
| f.write(await audio_file.read()) |
|
|
| videoclip = VideoFileClip(video_path) |
| audioclip = AudioFileClip(audio_path) |
| videoclip = videoclip.set_audio(audioclip) |
|
|
| videoclip.write_videofile(output_path, codec="libx264", fps=24) |
|
|
| os.remove(video_path) |
| os.remove(audio_path) |
|
|
| return output_path |
|
|