You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MediaPipe生成带姿态关键点的视频无法打开问题求助

问题

尝试保存MediaPipe生成的带姿态关键点的视频,终端无报错且文件已保存,但无法打开,报错信息:

播放失败:文件无法播放,可能格式不支持、扩展名错误或文件损坏,错误码0xc10100be
(德语原文:Wiedergabe nicht möglich,Die Datei kann nicht wiedergegeben werden. Das Dateiformat wird möglicherweise nicht unterstützt, die Dateierweiterung ist möglicherweise falsch oder die Datei ist beschädigt. 0xc10100be)

使用的代码如下:

import cv2
import mediapipe as mp
import numpy as np #for saving


mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose

# Video Input (Replace 'your_video_file.mp4' with your video file path)
#cap = cv2.VideoCapture('Video_Reemt_Arm_1.mp4')
cap = cv2.VideoCapture('Reemt_Arms.mp4')
#cap = cv2.VideoCapture('Reemt_Feet.mp4')
#cap = cv2.VideoCapture('Reemt_Atmen_PTK.mp4') #dificil de detectar, intenta reconocimiento facial!
             
#cap = cv2.VideoCapture('Sam_Feet.mp4')
#cap = cv2.VideoCapture('Sam_Body.mp4')
#cap = cv2.VideoCapture('Sam_Atmen.mp4')

width = 600
height = 550

# Set the output video file name and parameters
output_file = 'output_video_with_pose.mp4'
fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # Specify the codec (other options include 'XVID', 'MJPG', etc.)
out = cv2.VideoWriter('output_video.mp4', fourcc, 30.0, (width, height))

# Resizable window
cv2.namedWindow('Media Pipe Pose Estimation', cv2.WINDOW_NORMAL)

with mp_pose.Pose(
        min_detection_confidence=0.5,
        min_tracking_confidence=0.5) as pose:
    while cap.isOpened():
        success, image = cap.read()
        if not success:
            print("End of video")
            break

        # Drawing
        image.flags.writeable = False
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        results = pose.process(image)

        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        mp_drawing.draw_landmarks(
            image,
            results.pose_landmarks,
            mp_pose.POSE_CONNECTIONS,
            landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style()
        )

        # Resize the window to match the video frame size
        cv2.resizeWindow('Media Pipe Pose Estimation', width, height)
        cv2.imshow('Media Pipe Pose Estimation', image)
        out.write(image)
        
        if cv2.waitKey(1) & 0xFF == 27:
            break

cap.release()
out.release() 
cv2.destroyAllWindows()

已尝试的方法:

  • 更换VideoWriter编码(如XVID、MJPG、H264),示例代码:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_file = 'output_video_with_pose.avi'  # Use .avi extension
out = cv2.VideoWriter(output_file, fourcc, 30.0, (width, height))
  • 更新OpenCV
  • 使用不同的媒体播放器
  • 检查原视频
  • 调试
解决方案

核心问题出在输出视频参数与实际写入帧不匹配,具体如下:

1. 帧尺寸不匹配

你手动指定了width=600和height=550,但原视频的实际帧尺寸大概率不是这个数值,直接写入原尺寸帧到指定尺寸的VideoWriter中,会导致视频文件结构损坏。

正确做法是从原视频获取真实尺寸:

# 从原视频提取实际宽高
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

如果需要自定义输出尺寸,必须在写入前对每帧图像做resize处理:

image = cv2.resize(image, (target_width, target_height))
out.write(image)

2. 帧率不匹配

手动设置的30.0帧率可能与原视频不一致,同样会导致播放异常,应从原视频获取真实帧率:

fps = cap.get(cv2.CAP_PROP_FPS)

修正后的完整代码

import cv2
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose

# 读取原视频
cap = cv2.VideoCapture('Reemt_Arms.mp4')

# 获取原视频的真实宽、高、帧率
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)

# 设置输出参数,确保编码与扩展名匹配
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output_video_with_pose.mp4', fourcc, fps, (width, height))

cv2.namedWindow('Media Pipe Pose Estimation', cv2.WINDOW_NORMAL)

with mp_pose.Pose(
        min_detection_confidence=0.5,
        min_tracking_confidence=0.5) as pose:
    while cap.isOpened():
        success, image = cap.read()
        if not success:
            print("End of video")
            break

        image.flags.writeable = False
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        results = pose.process(image)

        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        mp_drawing.draw_landmarks(
            image,
            results.pose_landmarks,
            mp_pose.POSE_CONNECTIONS,
            landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style()
        )

        # 写入帧(确保尺寸与VideoWriter一致)
        out.write(image)

        cv2.resizeWindow('Media Pipe Pose Estimation', width, height)
        cv2.imshow('Media Pipe Pose Estimation', image)
        
        if cv2.waitKey(1) & 0xFF == 27:
            break

cap.release()
out.release() 
cv2.destroyAllWindows()

额外注意事项

  • 编码与扩展名必须匹配:mp4v对应.mp4,XVID对应.avi,MJPG对应.avi或.mp4
  • 若仍无法播放,可尝试cv2.VideoWriter_fourcc(*'avc1')(H.264编码),需确保OpenCV支持该编码
  • 必须调用out.release()完成文件写入,否则文件会因未收尾而损坏

内容的提问来源于stack exchange,提问作者user22801597

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 16:28:17