-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniversal.py
More file actions
353 lines (285 loc) · 12.4 KB
/
universal.py
File metadata and controls
353 lines (285 loc) · 12.4 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"""
Universal Metadata Extractor - A cross-platform tool for extracting metadata
from various file types (images, audio, video) with detailed logging.
"""
import os
import sys
import logging
from pathlib import Path
from typing import Dict, Any, Optional
import exifread
import mutagen
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from PIL import Image, UnidentifiedImageError
# ============================================================================
# LOGGING CONFIGURATION
# ============================================================================
def setup_logging() -> tuple[logging.Logger, logging.Logger]:
"""
Configure dual logging system:
- Process logger: Console output for real-time monitoring
- Metadata logger: File output for detailed metadata reports
Returns:
tuple: (process_logger, metadata_logger)
"""
# Metadata logger (writes to file)
metadata_handler = logging.FileHandler(
'metadata_report.log',
encoding='utf-8',
mode='w'
)
metadata_handler.setFormatter(logging.Formatter('%(message)s'))
metadata_logger = logging.getLogger('metadata')
metadata_logger.setLevel(logging.INFO)
metadata_logger.addHandler(metadata_handler)
# Process logger (console output)
process_handler = logging.StreamHandler()
process_handler.setFormatter(
logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
)
process_logger = logging.getLogger('process')
process_logger.setLevel(logging.INFO)
process_logger.addHandler(process_handler)
return process_logger, metadata_logger
# ============================================================================
# UNIVERSAL METADATA EXTRACTOR
# ============================================================================
class UniversalMetadataExtractor:
"""
Extracts metadata from various file formats including images, audio, and video.
Uses specialized libraries for each file type to maximize metadata extraction.
"""
# File type mappings
IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.tiff', '.webp', '.heic', '.bmp', '.gif'}
AUDIO_EXTENSIONS = {'.mp3', '.flac', '.m4a', '.ogg', '.wav', '.aac', '.wma'}
VIDEO_EXTENSIONS = {'.mp4', '.mov', '.avi', '.mkv', '.wmv', '.flv', '.webm'}
@staticmethod
def get_image_metadata(file_path: Path) -> Dict[str, Any]:
"""
Extract metadata from image files using exifread.
Args:
file_path: Path to the image file
Returns:
Dictionary containing image metadata
"""
metadata = {}
try:
with open(file_path, 'rb') as file:
tags = exifread.process_file(file, details=False)
for tag, value in tags.items():
if tag not in ['JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'JPEGThumbnail']:
metadata[f"IMG_{tag}"] = str(value)
except Exception as e:
logging.debug(f"Could not extract image metadata from {file_path}: {e}")
return metadata
@staticmethod
def get_audio_metadata(file_path: Path) -> Dict[str, Any]:
"""
Extract metadata from audio files using mutagen.
Args:
file_path: Path to the audio file
Returns:
Dictionary containing audio metadata
"""
metadata = {}
try:
audio = mutagen.File(file_path)
if audio:
# Technical metadata
if hasattr(audio, 'info'):
for attr in dir(audio.info):
if not attr.startswith('_') and not callable(getattr(audio.info, attr)):
value = getattr(audio.info, attr)
if value is not None:
metadata[f"AUDIO_TECH_{attr}"] = str(value)
# Tag metadata (ID3, Vorbis comments, etc.)
if audio.tags:
for tag in audio.tags:
value = audio.tags[tag]
if value:
metadata[f"AUDIO_TAG_{tag}"] = str(value[0] if isinstance(value, list) else value)
except Exception as e:
logging.debug(f"Could not extract audio metadata from {file_path}: {e}")
return metadata
@staticmethod
def get_video_metadata(file_path: Path) -> Dict[str, Any]:
"""
Extract metadata from video files using hachoir.
Args:
file_path: Path to the video file
Returns:
Dictionary containing video metadata
"""
metadata = {}
try:
parser = createParser(str(file_path))
if parser:
with parser:
extracted = extractMetadata(parser)
if extracted:
for line in extracted.exportPlaintext():
if ":" in line:
key, value = line.split(":", 1)
metadata[f"VIDEO_{key.strip()}"] = value.strip()
except Exception as e:
logging.debug(f"Could not extract video metadata from {file_path}: {e}")
return metadata
@classmethod
def get_all_metadata(cls, file_path: Path) -> Dict[str, Any]:
"""
Main method to extract metadata based on file extension.
Args:
file_path: Path to the file
Returns:
Dictionary containing all extracted metadata
"""
all_metadata = {}
extension = file_path.suffix.lower()
# Image files
if extension in cls.IMAGE_EXTENSIONS:
all_metadata.update(cls.get_image_metadata(file_path))
# Audio files
elif extension in cls.AUDIO_EXTENSIONS:
all_metadata.update(cls.get_audio_metadata(file_path))
# Video files
elif extension in cls.VIDEO_EXTENSIONS:
all_metadata.update(cls.get_video_metadata(file_path))
# Basic file metadata (always included)
try:
stat_info = file_path.stat()
all_metadata.update({
"FILE_SIZE": f"{stat_info.st_size} bytes",
"CREATED": str(stat_info.st_ctime),
"MODIFIED": str(stat_info.st_mtime),
"FILE_EXTENSION": extension,
})
except Exception:
pass
return all_metadata
# ============================================================================
# FILE MANAGER
# ============================================================================
class FileManager:
"""
Manages recursive file system traversal and metadata extraction.
Handles directory exploration and generates comprehensive reports.
"""
def __init__(self, base_folder: str):
"""
Initialize FileManager with target directory.
Args:
base_folder: Root directory to scan
"""
self.base_folder = Path(base_folder).resolve()
self.ignored_dirs = {'.git', '__pycache__', '.venv', 'node_modules'}
self.ignored_files = {'.DS_Store', 'Thumbs.db', 'desktop.ini'}
def should_skip(self, path: Path) -> bool:
"""
Determine if a file or directory should be skipped.
Args:
path: Path to check
Returns:
True if should be skipped, False otherwise
"""
# Skip hidden files and directories
if any(part.startswith('.') for part in path.parts):
return True
# Skip ignored directories
if path.is_dir() and path.name in self.ignored_dirs:
return True
# Skip ignored files
if path.is_file() and path.name in self.ignored_files:
return True
return False
def run(self, process_logger: logging.Logger, metadata_logger: logging.Logger) -> None:
"""
Main execution method - recursively scans directory and extracts metadata.
Args:
process_logger: Logger for process information
metadata_logger: Logger for metadata output
"""
process_logger.info(f"Starting deep exploration of: {self.base_folder}")
process_logger.info(f"Metadata report will be saved to: metadata_report.log")
file_count = 0
supported_files = 0
# Recursive directory walk
for root, dirs, files in os.walk(self.base_folder, topdown=True):
current_path = Path(root)
# Filter out ignored directories
dirs[:] = [d for d in dirs if not self.should_skip(current_path / d)]
# Skip this directory if it should be ignored
if self.should_skip(current_path):
continue
relative_path = current_path.relative_to(self.base_folder)
# Log directory header
if str(relative_path) != '.':
metadata_logger.info(f"\n{'='*80}")
metadata_logger.info(f"DIRECTORY: {relative_path}")
metadata_logger.info(f"{'='*80}")
# Process files in current directory
for filename in sorted(files):
file_path = current_path / filename
file_count += 1
# Skip ignored files
if self.should_skip(file_path):
continue
# Skip the script itself and log files
if filename == Path(__file__).name or filename.endswith('.log'):
continue
# Extract metadata
metadata = UniversalMetadataExtractor.get_all_metadata(file_path)
# Log file information
metadata_logger.info(f"\nFILE: {filename}")
metadata_logger.info(f"PATH: {relative_path / filename}")
if metadata:
supported_files += 1
metadata_logger.info("METADATA:")
for key in sorted(metadata.keys()):
value = str(metadata[key])
# Truncate very long values
if len(value) > 500:
value = value[:497] + "..."
metadata_logger.info(f" • {key}: {value}")
else:
metadata_logger.info(" • No extractable metadata found")
# Summary
process_logger.info(f"\n{'='*60}")
process_logger.info("SCAN COMPLETE")
process_logger.info(f"{'='*60}")
process_logger.info(f"Total files scanned: {file_count}")
process_logger.info(f"Files with metadata extracted: {supported_files}")
process_logger.info(f"Metadata report saved to: metadata_report.log")
# ============================================================================
# MAIN EXECUTION
# ============================================================================
def main():
"""Main entry point for the script."""
# Setup logging
process_logger, metadata_logger = setup_logging()
# Get target directory from command line or use current directory
if len(sys.argv) > 1:
target_dir = sys.argv[1]
else:
target_dir = "."
try:
# Validate directory
target_path = Path(target_dir)
if not target_path.exists():
process_logger.error(f"Directory does not exist: {target_dir}")
sys.exit(1)
if not target_path.is_dir():
process_logger.error(f"Path is not a directory: {target_dir}")
sys.exit(1)
# Run metadata extraction
manager = FileManager(target_dir)
manager.run(process_logger, metadata_logger)
process_logger.info("\n✅ Process completed successfully!")
except KeyboardInterrupt:
process_logger.info("\n\n⚠️ Process interrupted by user")
sys.exit(130)
except Exception as e:
process_logger.error(f"\n❌ Error during execution: {e}")
sys.exit(1)
if __name__ == "__main__":
main()