我在 Windows XP 中使用 Python 2.5。我正在尝试使用此功能将 pygame 图像列表制作成视频文件。我在互联网上找到了该功能并对其进行了编辑。它起初工作,然后停止工作。这是它打印出来的:
Making video...
Formating 114 Frames...
starting loop
making encoder
Frame 1 process 1
Frame 1 process 2
Frame 1 process 2.5
这是错误:
Traceback (most recent call last):
File "ScreenCapture.py", line 202, in <module>
makeVideoUpdated(record_files, video_file)
File "ScreenCapture.py", line 151, in makeVideoUpdated
d = enc.encode(da)
pymedia.video.vcodec.VCodecError: Failed to encode frame( error code is 0 )
这是我的代码:
def makeVideoUpdated(files, outFile, outCodec='mpeg1video', info1=0.1):
fw = open(outFile, 'wb')
if (fw == None) :
print "Cannot open file " + outFile
return
if outCodec == 'mpeg1video' :
bitrate= 2700000
else:
bitrate= 9800000
start = time.time()
enc = None
frame = 1
print "Formating "+str(len(files))+" Frames..."
print "starting loop"
for img in files:
if enc == None:
print "making encoder"
params= {'type': 0,
'gop_size': 12,
'frame_rate_base': 125,
'max_b_frames': 90,
'height': img.get_height(),
'width': img.get_width(),
'frame_rate': 90,
'deinterlace': 0,
'bitrate': bitrate,
'id': vcodec.getCodecID(outCodec)
}
enc = vcodec.Encoder(params)
# Create VFrame
print "Frame "+str(frame)+" process 1"
bmpFrame= vcodec.VFrame(vcodec.formats.PIX_FMT_RGB24,
img.get_size(),
# Covert image to 24bit RGB
(pygame.image.tostring(img, "RGB"), None, None)
)
print "Frame "+str(frame)+" process 2"
# Convert to YUV, then codec
da = bmpFrame.convert(vcodec.formats.PIX_FMT_YUV420P)
print "Frame "+str(frame)+" process 2.5"
d = enc.encode(da) #THIS IS WHERE IT STOPS
print "Frame "+str(frame)+" process 3"
fw.write(d.data)
print "Frame "+str(frame)+" process 4"
frame += 1
print "savng file"
fw.close()
有人能告诉我为什么我有这个错误以及如何解决它吗?files 参数是 pygame 图像列表,outFile 是路径,outCodec 是默认值,info1 不再使用。
更新 1 这是我用来制作 pygame 图像列表的代码。
from PIL import ImageGrab
import time, pygame
pygame.init()
f = [] #This is the list that contains the images
fps = 1
for n in range(1, 100):
info = ImageGrab.grab()
size = info.size
mode = info.mode
data = info.tostring()
info = pygame.image.fromstring(data, size, mode)
f.append(info)
time.sleep(fps)