[英]broken/corrupted output .mp4 completely unplayable... what am i doing wrong
损坏/损坏 output (.mp4) 完全无法播放。 我究竟做错了什么?
如果这是我自学的明显问题,请原谅我。
我的代码:
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()
如果 output 视频文件完全无法播放,则可能是视频写入方式有问题。
一种可能的原因是用于编写视频的编解码器与您用于播放视频的软件不兼容。 您提供的代码中使用的编解码器是“ mp4v ”,它是使用cv2.VideoWriter_fourcc
function 指定的,并作为参数传递给cv2.VideoWriter()
function。
另一种可能性是抖动过程导致帧以与 MP4 容器不兼容的格式写入。
最后,如果输入视频损坏,可能会导致 output 视频也损坏。
可能的解决方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.