【问题标题】:video to images then images to video in python视频到图像然后图像到python中的视频
【发布时间】:2020-10-23 19:14:44
【问题描述】:

我正在尝试将以下视频转换为图像 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

谢谢。

【问题讨论】:

    标签: python video opencv-python


    【解决方案1】:

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

    【讨论】:

    • 是的,我在发布问题之前就这样做了,但还是一样。它不像原来的那样。它很接近,但不一样。因此,如果您有其他解决方案,我将不胜感激。感谢您的贡献。
    • 您可以发送更改值后得到的输出吗?
    • 请确保 1 分钟。
    • 我也有那种奇怪的口吃。我也尝试了不同的编解码器,但这并没有解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    相关资源
    最近更新 更多