Bird
Raised Fist0
Computer Visionml~20 mins

Video writing in Computer Vision - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Video Writing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this OpenCV video writing code?
Consider the following Python code snippet that writes a video file using OpenCV. What will be the output after running this code?
Computer Vision
import cv2
import numpy as np

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 10.0, (640,480))

frame = np.zeros((480,640,3), dtype=np.uint8)
frame[:] = (0,255,0)  # green frame

for _ in range(5):
    out.write(frame)
out.release()

print('Video written with 5 green frames')
ARuntimeError because frame size does not match
BVideo written with 10 green frames
CSyntaxError due to incorrect fourcc code
DVideo written with 5 green frames
Attempts:
2 left
💡 Hint
Check how many times the frame is written in the loop.
❓ Model Choice
intermediate
1:30remaining
Which codec is best for writing a high-quality MP4 video using OpenCV?
You want to save a video file in MP4 format with good compression and quality using OpenCV's VideoWriter. Which fourcc codec should you choose?
Acv2.VideoWriter_fourcc(*'DIVX')
Bcv2.VideoWriter_fourcc(*'XVID')
Ccv2.VideoWriter_fourcc(*'MP4V')
Dcv2.VideoWriter_fourcc(*'MJPG')
Attempts:
2 left
💡 Hint
MP4 format usually uses MPEG-4 codecs.
❓ Hyperparameter
advanced
1:30remaining
What happens if the frame size parameter in VideoWriter does not match the actual frame size?
In OpenCV, when creating a VideoWriter object, you specify the frame size. What is the effect if this size does not match the size of frames you write?
AThe video file is created but frames are resized automatically
BA runtime error occurs when writing frames with mismatched size
CThe video file is created but frames are cropped to the specified size
DThe VideoWriter silently ignores the size mismatch and writes frames anyway
Attempts:
2 left
💡 Hint
OpenCV expects exact frame size match for VideoWriter.
❓ Metrics
advanced
1:00remaining
How to calculate the total duration of a video written with OpenCV?
You wrote a video with OpenCV's VideoWriter with frame rate 20 FPS and 300 frames. What is the total duration of the video in seconds?
A15 seconds
B20 seconds
C300 seconds
D6000 seconds
Attempts:
2 left
💡 Hint
Duration = number_of_frames / frames_per_second
🔧 Debug
expert
2:30remaining
Why does this OpenCV video writing code fail to save the video file?
Examine the code below. It runs without errors but the output video file is empty (0 bytes). What is the most likely cause?
Computer Vision
import cv2
import numpy as np

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 10.0, (640,480))

frame = np.zeros((480,640,3), dtype=np.uint8)
frame[:] = (255,0,0)  # blue frame

# Missing frame writing loop

out.release()
ANo frames were written to the video before releasing
BThe VideoWriter object was not released properly
CThe fourcc codec 'XVID' is not supported on this system
DThe frame size is incorrect and causes no frames to be written
Attempts:
2 left
💡 Hint
Check if any frames are actually written before release.

Practice

(1/5)
1. What is the main purpose of using cv2.VideoWriter in computer vision?
easy
A. To save a series of images as a video file
B. To read video files frame by frame
C. To apply filters on images
D. To display images on screen

Solution

  1. Step 1: Understand the function of cv2.VideoWriter

    cv2.VideoWriter is designed to save multiple images as a continuous video file.
  2. Step 2: Differentiate from other OpenCV functions

    Functions like cv2.VideoCapture read videos, while VideoWriter writes videos.
  3. Final Answer:

    To save a series of images as a video file -> Option A
  4. Quick Check:

    Video writing = save images as video [OK]
Hint: VideoWriter saves frames into video files [OK]
Common Mistakes:
  • Confusing VideoWriter with VideoCapture
  • Thinking VideoWriter displays images
  • Assuming VideoWriter applies image filters
2. Which of the following is the correct way to create a cv2.VideoWriter object for a 640x480 video at 30 fps using the 'XVID' codec?
easy
A. cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), 30, (640, 480))
B. cv2.VideoWriter('output.avi', 'XVID', 30, (640, 480))
C. cv2.VideoWriter('output.avi', cv2.VideoCapture_fourcc('XVID'), 30, (640, 480))
D. cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), 30, 640, 480)

Solution

  1. Step 1: Use cv2.VideoWriter_fourcc correctly

    The codec must be passed as cv2.VideoWriter_fourcc(*'XVID') to get the fourcc code.
  2. Step 2: Check the parameters order and types

    Parameters are filename, fourcc, fps, and frame size as a tuple (width, height).
  3. Final Answer:

    cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), 30, (640, 480)) -> Option A
  4. Quick Check:

    Correct codec and frame size syntax = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), 30, (640, 480)) [OK]
Hint: Use cv2.VideoWriter_fourcc(*'XVID') for codec [OK]
Common Mistakes:
  • Passing codec as string directly
  • Using cv2.VideoCapture_fourcc instead of VideoWriter_fourcc
  • Incorrect frame size format (not a tuple)
3. What will be the output of this code snippet?
import cv2
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('test.avi', fourcc, 20.0, (320,240))
frame = 255 * np.ones((240,320,3), dtype=np.uint8)
out.write(frame)
out.release()
print('Video saved')
medium
A. Nothing happens, no file is created
B. A video file named 'test.avi' with a white frame is saved and 'Video saved' is printed
C. RuntimeError because frame size does not match
D. NameError due to missing import of numpy

Solution

  1. Step 1: Check imports

    The code uses np.ones but numpy (np) is not imported, causing a NameError.
  2. Step 2: Understand error impact

    Without importing numpy, the code will fail before writing or saving video.
  3. Final Answer:

    NameError due to missing import of numpy -> Option D
  4. Quick Check:

    Missing numpy import = NameError [OK]
Hint: Always import numpy when using np [OK]
Common Mistakes:
  • Assuming code runs without numpy import
  • Ignoring frame size correctness
  • Thinking print means success
4. Identify the error in this video writing code and select the fix:
import cv2
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 25, (640, 480))
frame = cv2.imread('image.jpg')
out.write(frame)
out.release()
medium
A. Fix: Change codec to 'MJPG'
B. Fix: Resize frame to (640, 480) before writing
C. Fix: Use grayscale image instead of color
D. Fix: Increase fps to 30

Solution

  1. Step 1: Check frame size vs VideoWriter size

    VideoWriter expects frames of size (640, 480). If frame size differs, write() fails or produces errors.
  2. Step 2: Correct frame size by resizing

    Resize the loaded image to (640, 480) before writing to match VideoWriter settings.
  3. Final Answer:

    Fix: Resize frame to (640, 480) before writing -> Option B
  4. Quick Check:

    Frame size must match VideoWriter size [OK]
Hint: Match frame size to VideoWriter size [OK]
Common Mistakes:
  • Ignoring frame size mismatch
  • Changing codec unnecessarily
  • Changing fps unrelated to error
5. You want to create a video from a list of images with different sizes. Which approach correctly handles this situation?
hard
A. Use VideoWriter without resizing; it auto-adjusts frame size
B. Write images directly without VideoWriter; just save as separate files
C. Resize all images to the same size before writing to VideoWriter
D. Change codec to 'X264' to support variable frame sizes

Solution

  1. Step 1: Understand VideoWriter frame size requirement

    VideoWriter requires all frames to have the same size as specified at creation.
  2. Step 2: Handle varying image sizes

    Resize all images to a consistent size before writing to avoid errors and ensure smooth video.
  3. Final Answer:

    Resize all images to the same size before writing to VideoWriter -> Option C
  4. Quick Check:

    Consistent frame size needed for video writing [OK]
Hint: Resize images to uniform size before video writing [OK]
Common Mistakes:
  • Assuming VideoWriter auto-resizes frames
  • Trying to write images without VideoWriter
  • Changing codec to fix size issues
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard