Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
import os
|
| 3 |
+
from moviepy.editor import (
|
| 4 |
+
VideoFileClip,
|
| 5 |
+
ImageClip,
|
| 6 |
+
TextClip,
|
| 7 |
+
AudioFileClip,
|
| 8 |
+
CompositeVideoClip,
|
| 9 |
+
ColorClip
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def generate_sample_video():
|
| 14 |
+
output_path = f"/app/generated_{uuid.uuid4().hex[:6]}.mp4"
|
| 15 |
+
bg_clip = ColorClip(size=(1280, 720), color=(10, 10, 10), duration=5)
|
| 16 |
+
txt_clip = TextClip(
|
| 17 |
+
"This is MoviePy on Hugging Face", fontsize=70, color='white', font="DejaVu-Serif"
|
| 18 |
+
).set_duration(5).set_position(("center", "center"))
|
| 19 |
+
|
| 20 |
+
final_clip = CompositeVideoClip([bg_clip, txt_clip])
|
| 21 |
+
final_clip.write_videofile(output_path, fps=24, codec="libx264")
|
| 22 |
+
return output_path
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
async def combine_video_audio(video_file, audio_file):
|
| 26 |
+
video_path = f"/app/temp_video_{uuid.uuid4().hex[:6]}.mp4"
|
| 27 |
+
audio_path = f"/app/temp_audio_{uuid.uuid4().hex[:6]}.mp3"
|
| 28 |
+
output_path = f"/app/output_{uuid.uuid4().hex[:6]}.mp4"
|
| 29 |
+
|
| 30 |
+
with open(video_path, "wb") as f:
|
| 31 |
+
f.write(await video_file.read())
|
| 32 |
+
with open(audio_path, "wb") as f:
|
| 33 |
+
f.write(await audio_file.read())
|
| 34 |
+
|
| 35 |
+
videoclip = VideoFileClip(video_path)
|
| 36 |
+
audioclip = AudioFileClip(audio_path)
|
| 37 |
+
videoclip = videoclip.set_audio(audioclip)
|
| 38 |
+
|
| 39 |
+
videoclip.write_videofile(output_path, codec="libx264", fps=24)
|
| 40 |
+
|
| 41 |
+
os.remove(video_path)
|
| 42 |
+
os.remove(audio_path)
|
| 43 |
+
|
| 44 |
+
return output_path
|