Spaces:
Build error
Build error
File size: 2,152 Bytes
4409449 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import moviepy.editor as mp
import moviepy.video.fx.all as vfx
import os
import imageio
def mask_png(frames):
for frame in frames:
im = imageio.imread(frame)
im[im[:, :, 3] < 1, :] = 255
imageio.imwrite(frame, im[:, :, 0:3])
return
class Video:
def __init__(self, frame_path: str, fps: float = 12.5, res="high"):
frame_path = str(frame_path)
self.fps = fps
self._conf = {"codec": "libx264",
"fps": self.fps,
"audio_codec": "aac",
"temp_audiofile": "temp-audio.m4a",
"remove_temp": True}
if res == "low":
bitrate = "500k"
else:
bitrate = "5000k"
self._conf = {"bitrate": bitrate,
"fps": self.fps}
# Load video
# video = mp.VideoFileClip(video1_path, audio=False)
# Load with frames
frames = [os.path.join(frame_path, x)
for x in sorted(os.listdir(frame_path))]
# mask background white for videos
mask_png(frames)
video = mp.ImageSequenceClip(frames, fps=fps)
self.video = video
self.duration = video.duration
def add_text(self, text):
# needs ImageMagick
video_text = mp.TextClip(text,
font='Amiri',
color='white',
method='caption',
align="center",
size=(self.video.w, None),
fontsize=30)
video_text = video_text.on_color(size=(self.video.w, video_text.h + 5),
color=(0, 0, 0),
col_opacity=0.6)
# video_text = video_text.set_pos('bottom')
video_text = video_text.set_pos('top')
self.video = mp.CompositeVideoClip([self.video, video_text])
def save(self, out_path):
out_path = str(out_path)
self.video.subclip(0, self.duration).write_videofile(
out_path, **self._conf)
|