Как записать видеофайл из моего кода Python, который создает движущийся объект
Hi everybody. I've created a moving object and now I need to record it as a video to use it in another website. I looked at youtube videos but I couldn't find anything. I appreciate it a lot if anybody here can help me. Here is my code:
<pre>import pygame pygame.init() screen=pygame.display.set_mode((1000,1000)) pygame.display.set_caption('visual rhythm') icon=pygame.image.load("song.png") pygame.display.set_icon(icon) playerImg=pygame.image.load('spaceship.png') playerX=370 playerY=400 def player(x,y): screen.blit(playerImg,(int(x),int(y))) speed=[4000, -2000 , 2000 , -1000 , 1333 , -4000] count=0 count2=0 clock=pygame.time.Clock() while count2<len(speed): screen.fill((0,255,0)) milli=clock.tick() seconds=milli/1000.0 dm=seconds*speed[count] playerY+=dm player(playerX,playerY) pygame.display.update() if playerY>=946 or playerY<1: count+=1 count2+=1
Что я уже пробовал:
Я пробовал этот код, но он не работает!
<pre>import pygame import numpy as np import cv2 cap=cv2.VideoCapture(0) pygame.init() screen=pygame.display.set_mode((1000,1000)) pygame.display.set_caption('visual rhythm') icon=pygame.image.load("song.png") pygame.display.set_icon(icon) playerImg=pygame.image.load('spaceship.png') playerX=370 playerY=400 def player(x,y): screen.blit(playerImg,(int(x),int(y))) fourcc=cv2.VideoWriter_fourcc(*'XVID') out=cv2.VideoWriter('movement.mp4',fourcc,20.0,(640,480)) speed=[4000, -2000 , 2000 , -1000 , 1333 , -4000] count=0 count2=0 clock=pygame.time.Clock() while count2<len(speed): screen.fill((0,255,0)) milli=clock.tick() seconds=milli/1000.0 dm=seconds*speed[count] playerY+=dm player(playerX,playerY) pygame.display.update() if playerY>=946 or playerY<1: count+=1 count2+=1 cap.release() cv2.destroyAllWindows()