File size: 5,228 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
{
 "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
}