1、修改源文件moviepy/video/tools/drawing.py
,修改blit为
blit_gpu
import numpy as np
import torch # 导入torch
def blit_gpu(im1, im2, pos=None, mask=None, ismask=False):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if pos is None:
pos = [0, 0]
xp, yp = pos
x1 = max(0, -xp)
y1 = max(0, -yp)
h1, w1 = im1.shape[:2]
h2, w2 = im2.shape[:2]
xp2 = min(w2, xp + w1)
yp2 = min(h2, yp + h1)
x2 = min(w1, w2 - xp)
y2 = min(h1, h2 - yp)
xp1 = max(0, xp)
yp1 = max(0, yp)
if (xp1 >= xp2) or (yp1 >= yp2):
return im2
if not isinstance(im1, torch.Tensor): # 5.43 ms per loop / 100 loops
im1 = torch.tensor(im1, device=device)
if not isinstance(im2, torch.Tensor):
im2 = torch.tensor(im2, device=device)
blitted = im1[y1:y2, x1:x2]
new_im2 = im2.clone()
if mask is None:
new_im2[yp1:yp2, xp1:xp2] = blitted
else:
if not isinstance(mask, torch.Tensor): # 2.71 ms per loop / 10 loops
mask = torch.tensor(mask[y1:y2, x1:x2], device=device) # 1.45 ms / 100 loops
else:
mask = mask[y1:y2, x1:x2]
if len(im1.shape) == 3:
mask = mask.unsqueeze(-1).repeat(1, 1, 3)
blit_region = new_im2[yp1:yp2, xp1:xp2]
new_im2[yp1:yp2, xp1:xp2] = mask * blitted + (1 - mask) * blit_region
# return new_im2.cpu().numpy().astype("uint8") if not ismask else new_im2.cpu().numpy() # 6.13 ms / 100 loops
return new_im2 if not ismask else new_im2
2、然后修改源文件moviepy/video/VideoClip.py
在文件中导入刚才修改的代码
from .tools.drawing import blit_gpu
修改第565行返回为
return blit_gpu(img, picture, pos, mask=mask, ismask=self.ismask)
3、修改源文件moviepy\Clip.py
把iter_frames方法的以下部分
if (dtype is not None) and (frame.dtype != dtype):
frame = frame.astype(dtype)
修改为
if (dtype is not None) and (frame.dtype != dtype):
# frame = frame.astype(dtype)
frame = frame.cpu().numpy().astype(dtype)