CSDDSFSFSAFSAF's picture
Add files using upload-large-folder tool
9848efb verified
import os
import cv2
import subprocess
def check_video_corruption(video_path):
"""检查视频文件是否损坏"""
try:
# 方法1:使用OpenCV检查
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return True
# 尝试读取第一帧
ret, frame = cap.read()
cap.release()
if not ret or frame is None:
return True
# 方法2:使用ffprobe进一步检查(如果可用)
try:
result = subprocess.run(
['ffprobe', '-v', 'error', '-select_streams', 'v:0',
'-count_frames', '-show_entries', 'stream=nb_read_frames',
'-of', 'default=nokey=1:noprint_wrappers=1', video_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=5
)
if result.returncode != 0:
return True
except (subprocess.TimeoutExpired, FileNotFoundError):
pass # ffprobe不可用,仅依赖OpenCV检查
return False
except Exception as e:
print(f"检查视频时出错 {video_path}: {e}")
return True
def find_and_check_mp4_files(root_dir):
"""查找并检查所有MP4文件"""
print(f"开始扫描目录: {root_dir}")
mp4_files = []
corrupted_files = []
# 遍历目录树查找所有.mp4文件
for root, dirs, files in os.walk(root_dir):
for file in files:
if file.lower().endswith('.mp4'):
full_path = os.path.join(root, file)
mp4_files.append(full_path)
print(f"找到 {len(mp4_files)} 个MP4文件")
# 检查每个文件
total = len(mp4_files)
for i, video_path in enumerate(mp4_files, 1):
print(f"正在检查 [{i}/{total}]: {os.path.basename(video_path)}", end="\r")
if check_video_corruption(video_path):
corrupted_files.append(video_path)
print(f"\n检查完成!")
return mp4_files, corrupted_files
def save_corrupted_files(corrupted_files, output_file="corrupted_videos.txt"):
"""将损坏的文件路径保存到文本文件"""
with open(output_file, 'w', encoding='utf-8') as f:
for file_path in corrupted_files:
f.write(file_path + '\n')
print(f"损坏的文件列表已保存到: {output_file}")
def main():
# 设置根目录
root_dir = "/data/shuimu.chen/videomarathon/downloaded_videos/panda" # 根据实际情况调整路径
# 查找并检查文件
all_mp4_files, corrupted_files = find_and_check_mp4_files(root_dir)
# 输出统计信息
print("\n" + "="*50)
print(f"统计结果:")
print(f"总MP4文件数: {len(all_mp4_files)}")
print(f"损坏文件数: {len(corrupted_files)}")
print(f"正常文件数: {len(all_mp4_files) - len(corrupted_files)}")
if corrupted_files:
print(f"\n损坏的文件:")
for file_path in corrupted_files[:10]: # 只显示前10个
print(f" - {os.path.relpath(file_path, root_dir)}")
if len(corrupted_files) > 10:
print(f" ... 还有 {len(corrupted_files) - 10} 个文件")
# 保存损坏文件列表
save_corrupted_files(corrupted_files)
else:
print("\n恭喜!没有发现损坏的视频文件。")
if __name__ == "__main__":
main()