如何解决视频转换为图像,然后图像转换为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',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
谢谢。
解决方法
您应将cv2.VideoWriter('hiV.mp4',1,(w,h))
更改为cv2.VideoWriter('hiV.mp4',30,h))
,因为1
设置了fps,这意味着您每秒写1帧,而不是30或29.97(NTSC)作为原始视频。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。