{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "d5672d85-fde6-4860-8bf0-941405fd9559", "metadata": {}, "outputs": [], "source": [ "import subprocess\n", "from pathlib import Path\n", "from concurrent.futures import ThreadPoolExecutor, as_completed\n", "\n", "# --- CONFIGURATION ---\n", "# Set your target base directory path here\n", "TARGET_FOLDER = r\"/workspace/musubi-tuner/dataset/ltxxx\" \n", "MAX_THREADS = 16\n", "# ---------------------\n", "\n", "def process_video(file_path):\n", " \"\"\"\n", " Worker function executed by the threads. \n", " Checks a single video and deletes it if the specific Unicode error is thrown.\n", " Returns a status string to be printed by the main thread.\n", " \"\"\"\n", " # ffmpeg command to scan the video stream for errors\n", " cmd =['ffmpeg', '-v', 'error', '-i', str(file_path), '-f', 'null', '-']\n", " \n", " try:\n", " # text=True forces UTF-8 decoding. Corrupted byte headers \n", " # from ffmpeg will trigger the UnicodeDecodeError here.\n", " subprocess.run(cmd, capture_output=True, text=True)\n", " return f\"[OK] Checked: {file_path.parent.name}/{file_path.name}\"\n", " \n", " except UnicodeDecodeError as e:\n", " # Catching your specific UnicodeDecodeError\n", " result_msg = f\"\\n[CORRUPTION DETECTED] UnicodeDecodeError in {file_path.parent.name}/{file_path.name}:\\n -> {e}\\n\"\n", " result_msg += f\" -> Deleting {file_path.name}...\"\n", " \n", " try:\n", " file_path.unlink() # Deletes the video\n", " result_msg += \" SUCCESS.\"\n", " except Exception as del_e:\n", " result_msg += f\" FAILED: {del_e}\"\n", " \n", " return result_msg\n", " \n", " except FileNotFoundError:\n", " return \"ERROR_FFMPEG_MISSING\"\n", " \n", " except Exception as e:\n", " return 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", " # Must be a directory, start with \"videos\", but NOT be exactly \"videos\"\n", " if folder.is_dir() and folder.name.lower().startswith(\"videos\") and folder.name.lower() != \"videos\":\n", " for file_path in folder.iterdir():\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 using 16 threads\n", " with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:\n", " # Submit all video processing tasks to the thread pool\n", " futures = {executor.submit(process_video, path): path for path in files_to_check}\n", " \n", " # as_completed yields the results the moment a thread finishes its file\n", " for future in as_completed(futures):\n", " result = future.result()\n", " \n", " # Halt completely if ffmpeg isn't installed\n", " if result == \"ERROR_FFMPEG_MISSING\":\n", " print(\"\\n[CRITICAL ERROR] 'ffmpeg' command not found. Ensure it is installed and in your PATH.\")\n", " executor.shutdown(wait=False, cancel_futures=True) # Attempt to cancel pending threads\n", " return\n", " \n", " # Print the result returned from the worker thread\n", " print(result)\n", "\n", "if __name__ == \"__main__\":\n", " check_and_clean_videos(TARGET_FOLDER)\n", " print(\"\\nScan complete.\")" ] }, { "cell_type": "code", "execution_count": null, "id": "6713ee92-0ebb-448a-8307-217793428fd8", "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 }