视频转换为图像,然后图像转换为python视频

时间:2020-10-23 19:14:44

标签: python video opencv-python

我正在尝试将以下视频转换为图像 https://www.signingsavvy.com/media/mp4-ld/24/24851.mp4 但是,我已经通过使用OpenCV做到了

# Importing all necessary libraries 
import cv2 
import os 

# Read the video from specified path 
cam = cv2.VideoCapture("C:\Users\ahmad\Hi_ASL.mp4")
print(cam.get(cv2.CAP_PROP_FPS))
try: 

# creating a folder named data 
    if not os.path.exists('data'): 
        os.makedirs('data') 

# if not created then raise error 
except OSError: 
    print ('Error: Creating directory of data') 

# frame 
currentframe = 0

while(True): 

    # reading from frame 
    ret,frame = cam.read() 

    if ret: 
        # if video is still left continue creating images 
        name = './data/frame' + str(currentframe) + '.jpg'
#         print ('Creating...' + name) 

        # writing the extracted images 
        cv2.imwrite(name, frame) 

        # increasing counter so that it will 
        # show how many frames are created 
        currentframe += 1
    else: 
        break
#     ret,frame = cam.read() 

# Release all space and windows once done 
cam.release() 
cv2.destroyAllWindows()

完成后。我想将这些图像转换为视频,就像上面的图像一样,我编写了这段代码

img = [img for img in os.listdir('data')]
frame = cv2.imread('data\' + img[0])
h , w , l = frame.shape
vid = cv2.VideoWriter('hiV.mp4' , 0 ,1 ,(w,h))

for imgg in img:
    vid.write(cv2.imread('data\' + imgg))

cv2.destroyAllWindows()
vid.release()

问题是使用OpenCV将图像组合到视频中的结果与原始视频不同。那么,有什么问题呢?我希望它与原始版本相同。 上面代码的结果是此视频https://drive.google.com/file/d/16vwT35wzc95tBleK5VCpZJQkaLxSiKVd/view?usp=sharing

谢谢。

1 个答案:

答案 0 :(得分:0)

您应将cv2.VideoWriter('hiV.mp4' , 0 ,1 ,(w,h))更改为cv2.VideoWriter('hiV.mp4' , 0 ,30 ,(w,h)),因为1设置了fps,这意味着您每秒写1帧,而不是30或29.97(NTSC)作为原始视频。