{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "e782d207-09de-4ca6-a40a-7e2d65d26998", "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: 4%|▍ | 4672/122635 [01:51<37:18, 52.69file/s] " ] } ], "source": [ "import os\n", "import av\n", "from pathlib import Path\n", "from concurrent.futures import ThreadPoolExecutor, as_completed\n", "from tqdm import tqdm\n", "\n", "# --- PYAV LOG SUPPRESSION ---\n", "# Suppresses verbose warnings from PyAV so it doesn't break the progress bar\n", "av.logging.set_level(av.logging.FATAL)\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}\\n\"\n", " result_msg += f\" -> File: {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 PyAV to open the container, precisely mirroring the musubi_tuner dataloader.\n", " \"\"\"\n", " try:\n", " # Opening the container triggers the metadata avdict_to_dict parse.\n", " # This is the exact line that failed in your stack trace.\n", " with av.open(str(file_path)) as container:\n", " \n", " # Optionally, we can also force it to read the first frame \n", " # to catch standard physical corruptions while we are here.\n", " stream = container.streams.video[0]\n", " for frame in container.decode(stream):\n", " break # Just decode one frame and stop\n", "\n", " return (\"OK\", \"\")\n", " \n", " except UnicodeDecodeError as e:\n", " # Catches the exact error breaking your dataloader\n", " return delete_corrupted(file_path, f\"UnicodeDecodeError (Metadata corrupted): {e}\")\n", " \n", " except av.error.InvalidDataError as e:\n", " # Catches natively caught PyAV stream corruptions\n", " return delete_corrupted(file_path, f\"PyAV InvalidDataError (Stream corrupted): {e}\")\n", " \n", " except Exception as e:\n", " # Catches any other reason PyAV might refuse to read the file\n", " return delete_corrupted(file_path, f\"PyAV Error: {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\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", " # Print if corrupted\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": "71101124-eb53-4a33-a4af-e3467d222365", "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 }