OpenCV生成的视频,遇到了无法播放的问题,百思不得解;
show the code:
def cut_video(src_path,dst_path,name,inv):
vid = cv2.VideoCapture(os.path.join(src_path,name))
fps = vid.get(cv2.CAP_PROP_FPS)
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
print('fps is ', fps,width,height)
if not os.path.exists(dst_path):
os.makedirs(dst_path,exist_ok=True)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(filename=os.path.join(dst_path, name), fourcc=fourcc, fps=fps,
frameSize = (int(height), int(width)))
num = 0
while vid.isOpened() and num < int(fps * inv):
is_read, frame = vid.read()
if is_read:
video_writer.write(frame)
num += 1
else:
break
video_writer.release()
print('process ok!')
return
好像没查到有哪里错误,直到 frameSize的顺序;
图像的维度是这样的:height ,width ,channel = img.shape;
但是frameSize的顺序是相反的:width,height;
#src_path 对应原始视频的路径;
#dst_path 对应生成的视频路径,name为视频的文件名;
#inv == 改变之后的视频时长;
def cut_video(src_path,dst_path,name,inv):
vid = cv2.VideoCapture(os.path.join(src_path,name))
fps = vid.get(cv2.CAP_PROP_FPS)
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
print('fps is ', fps,width,height)
if not os.path.exists(dst_path):
os.makedirs(dst_path,exist_ok=True)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(filename=os.path.join(dst_path, name), fourcc=fourcc, fps=fps,
#Warning-----------------注意顺序是,width,height,一定要对应原始图像的大小
frameSize=(int(width), int(height)))
###############################
# frameSize = (int(height), int(width)))
num = 0
while vid.isOpened() and num < int(fps * inv):
is_read, frame = vid.read()
if is_read:
video_writer.write(frame)
num += 1
else:
break
video_writer.release()
print('process ok!')
return
上面函数实现减短视频时长的功能,有问题欢迎随时交流!!!