File size: 5,763 Bytes
157a4c0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | {
"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
}
|