opencv / opencv/opencv-python

Video not saving for different codec formats and dimension

Open
#761 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.4k
Forks
1k
Avg merge
22h 17m
Merged PRs (30d)
3

Description

Expected behaviour

I am currently trying to write a video after applying some mediapipe operations.
The video with (size: 1080 x 1920, codec: MPEG-4 AAC, Timed Metadata, HEVC) is able to be written by the cv.VideoWriter class, but for some reason a video with (size: 720 x 1080, codec: Timed Metadata, H.264, MPEG-4 AAC) is not being written. I assumed it was due to the H.264 codec. But, I'm not sure how to fix it at the moment. Apologies, if this is an easy fix!

For additional info, the larger video was recorded from a video and the smaller video was downloaded from facebook messenger, which is why i assume they have different codec formats.

Actual behaviour

Both video formats should be able to be written using cv.VideoWriter class.
Also, i have tried saving the video without any of the mediapipe operations. The behaviour is the same. When I view the frames with cv.imshow(), the frames are flipped for the 1080 x 1980 as well.

Steps to reproduce
  • example code
# OpenCV Capture
cap = cv.VideoCapture(args.input if args.input else 0)

# Create a VideoWriter object so we can save the video output
fourcc = cv.VideoWriter_fourcc(*'mp4v')
output_frames_per_second = 30.0
out = cv.VideoWriter(os.path.join(OUTPUT_DIR, args.output_filename),  
                        fourcc, 
                        output_frames_per_second, 
                        (args.width, args.height))

while cap.isOpened():
    hasFrame, frame = cap.read()
    print(hasFrame)
    if not hasFrame:
        # cv.waitKey()
        break
    
    # Flip image vertically if model type is mediatype
    if args.model_type == 'mediapipe':
        frame = cv.flip(frame, -1)
    # Get Pose Joints
    points = pose.returnPoints(frame)

    # Draw Pose Joints on frame
    pose.drawJoints(frame, points)

    # Display Frame
    # cv.imshow('OpenPose using OpenCV', frame)

    # Write the frame to the output video file
    out.write(frame)

if args.model_type == 'media':
    pose.close()

# Stop when the video is finished
cap.release()
     
# Release the video recording
out.release()
  • operating system
    mac os Ventura
  • architecture (e.g. x86)
    ARM (silicon)
  • opencv-python version
    opencv-contrib-python==4.6.0.66
    opencv-python==4.5.5.64

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the cv.VideoWriter example with the two video dimensions and codec combinations on macOS Ventura ARM, using the reported opencv-python and opencv-contrib-python versions. Compare frame reading, flipping, and writing behavior; done means establishing why the smaller H.264 input is not saved and documenting or correcting the supported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.