{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "b8dc159c-47ba-4309-97f1-bef01de80582", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 122635 videos. Starting scan with 64 threads...\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Scanning Videos: 2%|▏ | 1909/122635 [00:48<48:14, 41.72file/s] " ] } ], "source": [ "import os\n", "import cv2\n", "from pathlib import Path\n", "from concurrent.futures import ThreadPoolExecutor, as_completed\n", "from tqdm import tqdm\n", "\n", "# --- OPENCV LOG SUPPRESSION ---\n", "# OpenCV's C++ backend can be very noisy when it finds corrupted videos, \n", "# which can visually break the tqdm progress bar. This suppresses those warnings.\n", "os.environ[\"OPENCV_LOG_LEVEL\"] = \"FATAL\"\n", "os.environ[\"OPENCV_FFMPEG_LOGLEVEL\"] = \"-8\"\n", "\n", "# --- CONFIGURATION ---\n", "TARGET_FOLDER = r\"/workspace/musubi-tuner/dataset/ltxxx\" \n", "MAX_THREADS = 64\n", "# ---------------------\n", "\n", "def delete_corrupted(file_path, reason):\n", " \"\"\"Helper function to handle the deletion and message formatting.\"\"\"\n", " result_msg = f\"[CORRUPT] {reason}: {file_path.parent.name}/{file_path.name}\\n\"\n", " result_msg += f\" -> Deleting {file_path.name}...\"\n", " try:\n", " file_path.unlink() # Deletes the video\n", " return (\"CORRUPTED\", result_msg + \" SUCCESS.\")\n", " except Exception as del_e:\n", " return (\"CORRUPTED\", result_msg + f\" FAILED: {del_e}\")\n", "\n", "def process_video(file_path):\n", " \"\"\"\n", " Worker function executed by the threads. \n", " Uses OpenCV to open the container and read the first frame.\n", " \"\"\"\n", " try:\n", " # 1. Attempt to open the video container\n", " cap = cv2.VideoCapture(str(file_path))\n", " \n", " if not cap.isOpened():\n", " cap.release()\n", " return delete_corrupted(file_path, \"OpenCV could not open the container\")\n", " \n", " # 2. Attempt to decode and read the very first frame\n", " ret, frame = cap.read()\n", " cap.release()\n", " \n", " if not ret:\n", " # The container opened, but the video stream is empty or completely broken\n", " return delete_corrupted(file_path, \"OpenCV could not read the first frame\")\n", " \n", " # If it passed both checks, it is a healthy, readable video\n", " return (\"OK\", \"\")\n", " \n", " except Exception as e:\n", " return (\"ERROR\", f\"[ERROR] Unexpected error processing {file_path.name}: {e}\")\n", "\n", "def check_and_clean_videos(base_path):\n", " base_dir = Path(base_path)\n", " \n", " if not base_dir.is_dir():\n", " print(f\"Error: The path '{base_dir}' is not a valid directory.\")\n", " return\n", "\n", " video_extensions = {'.mp4', '.mkv', '.avi', '.mov', '.wmv', '.flv', '.webm', '.m4v'}\n", " files_to_check =[]\n", "\n", " # 1. Gather all matching video files first\n", " for folder in base_dir.iterdir():\n", " if folder.is_dir() and folder.name.lower().startswith(\"videos\") and folder.name.lower() != \"videos\":\n", " \n", " # Using rglob(\"*\") to search recursively in case there are subfolders\n", " for file_path in folder.rglob(\"*\"):\n", " if file_path.is_file() and file_path.suffix.lower() in video_extensions:\n", " files_to_check.append(file_path)\n", "\n", " if not files_to_check:\n", " print(\"No videos found matching the criteria.\")\n", " return\n", "\n", " print(f\"Found {len(files_to_check)} videos. Starting scan with {MAX_THREADS} threads...\\n\")\n", "\n", " # 2. Process the gathered files\n", " with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:\n", " futures = [executor.submit(process_video, path) for path in files_to_check]\n", " \n", " for future in tqdm(as_completed(futures), total=len(files_to_check), desc=\"Scanning Videos\", unit=\"file\"):\n", " status, message = future.result()\n", " \n", " # If it's anything other than OK (like a corruption or unexpected error), print it.\n", " if status != \"OK\":\n", " tqdm.write(message)\n", "\n", "if __name__ == \"__main__\":\n", " check_and_clean_videos(TARGET_FOLDER)\n", " print(\"\\nScan complete.\")" ] }, { "cell_type": "code", "execution_count": null, "id": "4670a262-884b-45b0-96ed-f4db2dfc429f", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.13" } }, "nbformat": 4, "nbformat_minor": 5 }