【问题标题】:Resizing video using opencv and saving it使用opencv调整视频大小并保存
【发布时间】:2020-09-05 18:35:17
【问题描述】:

我正在尝试使用 opencv 重新调整视频大小,然后将其保存回我的系统。代码有效并且没有给出任何错误,但输出视频文件已损坏。我正在使用的fourcc 是mp4v 与.mp4 配合得很好,但输出视频仍然损坏。需要帮助。

import numpy as np
    import cv2
    import sys
    import re
    vid=""
    
    if len(sys.argv)==3:
        vid=sys.argv[1]
        compress=int(sys.argv[2])
    else:
        print("File not mentioned or compression not given")
        exit()
    
    if re.search('.mp4',vid):
        print("Loading")
    else:
        exit()
    
    cap = cv2.VideoCapture(0)
    ret, frame = cap.read()
    
    def rescale_frame(frame, percent=75):
        width = int(frame.shape[1] * percent/ 100)
        height = int(frame.shape[0] * percent/ 100)
        dim = (width, height)
        return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
    
    FPS= 15.0
    FrameSize=(frame.shape[1], frame.shape[0])
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    
    out = cv2.VideoWriter('Video_output.mp4', fourcc, FPS, FrameSize, 0)
    
    while(cap.isOpened()):
        ret, frame = cap.read()
    
        # check for successfulness of cap.read()
        if not ret: break
        
        rescaled_frame=rescale_frame(frame,percent=compress)
        # Save the video
        out.write(rescaled_frame)
    
        cv2.imshow('frame',rescaled_frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
             break
    
    cap.release()
    out.release()
    cv2.destroyAllWindows()

【问题讨论】:

  • 有关保存视频的任何问题请参考this
  • out = cv2.VideoWriter('Video_output.mp4',fourcc, FPS, FrameSize) ...删除 0 出人意料地解决了这个问题。但是我不知道它背后的逻辑。

标签: python opencv fourcc


【解决方案1】:

问题在于VideoWriter 初始化。

你初始化了:

out = cv2.VideoWriter('Video_output.mp4', fourcc, FPS, FrameSize, 0)

最后一个参数0表示isColor = False。您是说,您要将帧转换为灰度然后保存。但是您的代码中没有转换。

此外,您正在根据compress 参数调整代码中每一帧的大小。

如果我使用默认压缩参数:

cap = cv2.VideoCapture(0)

if cap.isOpened():
    ret, frame = cap.read()
    rescaled_frame = rescale_frame(frame)
    (h, w) = rescaled_frame.shape[:2]
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    writer = cv2.VideoWriter('Video_output.mp4',
                             fourcc, 15.0,
                             (w, h), True)
else:
    print("Camera is not opened")

现在我们已经用所需的维度初始化了VideoWriter


完整代码:

import time
import cv2


def rescale_frame(frame_input, percent=75):
    width = int(frame_input.shape[1] * percent / 100)
    height = int(frame_input.shape[0] * percent / 100)
    dim = (width, height)
    return cv2.resize(frame_input, dim, interpolation=cv2.INTER_AREA)


cap = cv2.VideoCapture(0)

if cap.isOpened():
    ret, frame = cap.read()
    rescaled_frame = rescale_frame(frame)
    (h, w) = rescaled_frame.shape[:2]
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    writer = cv2.VideoWriter('Video_output.mp4',
                             fourcc, 15.0,
                             (w, h), True)
else:
    print("Camera is not opened")

while cap.isOpened():
    ret, frame = cap.read()

    rescaled_frame = rescale_frame(frame)

    # write the output frame to file
    writer.write(rescaled_frame)

    cv2.imshow("Output", rescaled_frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break


cv2.destroyAllWindows()
cap.release()
writer.release()

可能的问题:我不想更改我的VideoWriter 参数,我该怎么办?

回答:那你需要把你的相框改成灰度图:

while cap.isOpened():
    # grab the frame from the video stream and resize it to have a
    # maximum width of 300 pixels
    ret, frame = cap.read()

    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

【讨论】:

  • 感谢这么详细的解释。我用我要压缩到的帧的大小初始化了 VideoWriter。但是现在我明白了为什么我得到了一个损坏的文件,这要归功于你的回答。
  • 很高兴,如果我能帮忙:)
猜你喜欢
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2011-07-13
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多