Changchiiiiii
Changchiiiiii

Reputation: 1

Python OpenCV Cannot Save Video Using argparse Output Path but Works with Hardcoded Path

I'm writing a resize_video.py script that resizes each frame of a video and then reconstructs the video. I want to run the script using the following command:

python resize_video.py --input_video input.mp4 --output output.mp4

I use argparse to get the input and output file paths, I expect the output_video_path be the args.output.

import cv2
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--input_video", required=True, help="Path to input video file")
parser.add_argument("--output", required=True, help="Path to output video file")
args = parser.parse_args()

output_video_path = args.output  # Using argparse parameter
# output_video_path = "output_video.mp4"  # Hardcoded path

cap = cv2.VideoCapture(args.input_video)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) // 2)  # Resize video
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) // 2)

out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    resized_frame = cv2.resize(frame, (width, height))
    out.write(resized_frame)

cap.release()
out.release()

And I encounter a problem:

When I set

output_video_path = args.output

the script runs but does not generate the output video. However, when I hardcode output_video_path = "output_video.mp4", the video is saved correctly.

What could be causing this issue, and how can I fix it?

Upvotes: 0

Views: 28

Answers (0)

Related Questions