0

Broken/ corrupted output (.mp4) completely unplayable. What am I doing wrong?

Forgive me if this is an obvious issue I am self taught.

My code:

import cv2
import numpy as np

# Load the input mp4 file
cap = cv2.VideoCapture(r'C:\Users\JAKE\OneDrive\Desktop\mirror.mp4')

# Get the frames per second (fps) and frame size of the input video
fps = cap.get(cv2.CAP_PROP_FPS)
frame_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

# Define the codec and create a video writer object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, fps, frame_size)

# Read each frame from the input video
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    # Apply Floyd-Steinberg dithering to the frame
    dithered = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    dithered = np.array(dithered, dtype=np.int16)
    for y in range(1, dithered.shape[0] - 1):
        for x in range(1, dithered.shape[1] - 1):
            old_pixel = dithered[y, x]
            new_pixel = 255 if old_pixel > 128 else 0
            dithered[y, x] = new_pixel
            quant_error = old_pixel - new_pixel
            dithered[y, x + 1] += quant_error * 7 // 16
            dithered[y + 1, x - 1] += quant_error * 3 // 16
            dithered[y + 1, x] += quant_error * 5 // 16
            dithered[y + 1, x + 1] += quant_error * 1 // 16
    dithered = np.array(dithered, dtype=np.uint8)

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

# Release the video capture and writer objects
cap.release()
out.release() 
VC.One
  • 14,790
  • 4
  • 25
  • 57

0 Answers0