转换numpy.进入视频



在我的代码中,我正在循环播放视频的帧,并试图生成另一个mp4视频。

这是我的代码:

cap = cv2.VideoCapture(args.video)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame_width, frame_height))    
while cap.isOpened():
ret, img = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
out.release() 
break
#<code>...
#<code>...
print(type(my_image))
out.write(my_image)

每帧print(type(my_image))的输出为numpy.ndarray。当我运行代码时,我得到了output_video.mp4文件,但权重只有300 kb(它需要大约50 mb)。

我试着将每一帧保存为一个图像,看看它是否会工作,,它确实工作了。这是代码:

img = Image.fromarray(my_image, 'RGB')
img.save('frameeeee-%s.png'%i)

我编写这个函数是为了解决一个类似的问题,您需要将图像单独保存到一个文件夹中,然后可以使用frames2video将其转换为视频。

def frames2video( path_in = "/content/original_frames" , path_out = "/content/outputvideo", 
frame_rate = 30 , video_name="output_video" ):
"""
Given an input path to a folder that contains a set of frames, this function
convert them into a video and then save it in the path_out. 
You need to know the fps of the original video, are 30 by default.
"""
img_path_list = natsorted(os.listdir(path_in))
assert(len(img_path_list)>0)
img_array = []
print("[F2V] Frames to video...", end="nn")

with tqdm(total=len(img_path_list)) as pbar:
for count,filename in enumerate(img_path_list):
img = cv2.imread(path_in+"/"+filename)
if(img is None):break
height, width, layers = img.shape
img_array.append(img)
size = (width,height)
pbar.update()
if os.path.exists(path_out): shutil.rmtree(path_out)
os.mkdir(path_out)
out = cv2.VideoWriter(path_out+"/"+str(video_name)+'.mp4', cv2.VideoWriter_fourcc(*'DIVX'), frame_rate, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
print("n[F2V] Video made from "+str(count+1)+" frames", end="nn")

为了完整起见,我也发布了一个相反的函数,即给定视频提取帧的函数。

def n_frames(video):
"""
Given an input video returns the EXACT number of frames(CV2 was not precise)
"""
success = True
count = 0
while success:
success,image = video.read()
if success == False: break
count+=1
return count
def video2frames( path_in = "/content/video.mp4" , path_out = "/content/original_frames",
n_of_frames_to_save = 999999, rotate=True, frames_name = "OrigFrame" ):
"""
Given a video from path_in saves all the frames inside path_out.
The number of frames(in case of long videos) can be truncated with
the n_of_frames_to_save parameter. Rotate is used to save rotated 
frames by 90 degree. All the frames are named frames_name with an
index
"""
blur_threshold = 0
if os.path.exists(path_out): shutil.rmtree(path_out)
os.mkdir(path_out)
count = 0
success = True
vidcap = cv2.VideoCapture(path_in)
v2 = cv2.VideoCapture(path_in)
fps = vidcap.get(cv2.CAP_PROP_FPS) 
if(fps>120): 
print("CAP_PROP_FPS > 120, probabily you are using a webcam. Setting fps manually")
fps = 25

n_of_frames = n_frames(v2) # #int(video.get(cv2.CAP_PROP_FRAME_COUNT)) is not accurate, https://stackoverflow.com/questions/31472155/python-opencv-cv2-cv-cv-cap-prop-frame-count-get-wrong-numbers
if(n_of_frames_to_save < n_of_frames): n_of_frames = n_of_frames_to_save
print("[V2F] Dividing the video in " + str(n_of_frames) + " frames", end="nn")
for count in trange(n_of_frames):
success,image = vidcap.read()   
if not success: break
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if(rotate): image = cv2.rotate(image,cv2.ROTATE_90_CLOCKWISE) 
plt.imsave("%s/%s%d.png" % (path_out,frames_name+"_", count), image)
count+=1
print("n[V2F] "+str(count)+" frames saved",end="nn")
return fps

好了,我找到了一个解决方案。我注意到我有resize函数在我的代码:

my_image = cv2.resize(image_before, (1280, 720))

所以我改变了

out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame_width, frame_height))    

out = cv2.VideoWriter('outputttttt.mp4', fourcc, fps, (1280, 720))

它工作(:

)

最新更新