-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideoReEncoderWithMetadata.py
More file actions
578 lines (463 loc) · 20.8 KB
/
Copy pathvideoReEncoderWithMetadata.py
File metadata and controls
578 lines (463 loc) · 20.8 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import subprocess
import json
import os
from pathlib import Path
from datetime import datetime, timedelta
import pytz
def convert_timezone(timestamp_str, target_timezone_str, treat_as_local=True):
"""Convert timestamp to target timezone with proper offset.
Args:
timestamp_str: Original timestamp string (e.g., '2011-12-25T10:34:19.000000Z' or '2011-06-25 14:09:33')
target_timezone_str: Target timezone (e.g., 'America/New_York')
treat_as_local: If True, treats the time as already being in the target timezone
and just adds the correct offset. If False, converts from UTC.
"""
if not timestamp_str:
return None
try:
# Remove Z and any existing timezone info
clean_timestamp = timestamp_str.replace('Z', '').split('+')[0].split('-05:00')[0].split('-04:00')[0]
# Try multiple timestamp formats
dt_naive = None
formats = [
'%Y-%m-%dT%H:%M:%S.%f', # ISO format with microseconds
'%Y-%m-%dT%H:%M:%S', # ISO format without microseconds
'%Y-%m-%d %H:%M:%S', # Space-separated format (common in AVI)
'%Y-%m-%d %H:%M:%S.%f', # Space-separated with microseconds
]
for fmt in formats:
try:
dt_naive = datetime.strptime(clean_timestamp, fmt)
break
except ValueError:
continue
if dt_naive is None:
print(f" Warning: Could not parse timestamp format: '{timestamp_str}'")
return timestamp_str
# Get target timezone
target_tz = pytz.timezone(target_timezone_str)
if treat_as_local:
# Treat the time as already being in the local timezone
# This handles the case where cameras record local time but mark it as UTC
dt_local = target_tz.localize(dt_naive)
else:
# Convert from actual UTC to target timezone
dt_utc = pytz.utc.localize(dt_naive)
dt_local = dt_utc.astimezone(target_tz)
# Format back to ISO 8601 with timezone offset
return dt_local.isoformat()
except Exception as e:
print(f" Warning: Could not convert timestamp '{timestamp_str}': {e}")
return timestamp_str
def get_video_metadata(input_file):
"""Extract metadata from video file using ffprobe."""
cmd = [
'ffprobe',
'-v', 'quiet',
'-print_format', 'json',
'-show_format',
'-show_streams',
input_file
]
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return json.loads(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Error reading metadata from {input_file}: {e}")
return None
except json.JSONDecodeError as e:
print(f"Error parsing metadata from {input_file}: {e}")
return None
def extract_metadata_fields(metadata):
"""Extract relevant metadata fields from ffprobe output."""
fields = {}
if not metadata:
return fields
# Get format-level metadata
format_tags = metadata.get('format', {}).get('tags', {})
# Extract creation_time (try multiple variations)
creation_time = format_tags.get('creation_time') or format_tags.get('com.apple.quicktime.creationdate')
if creation_time:
fields['creation_time'] = creation_time
# Extract make
make = (format_tags.get('make') or
format_tags.get('com.apple.quicktime.make') or
format_tags.get('Make'))
if make:
fields['make'] = make
# Extract model
model = (format_tags.get('model') or
format_tags.get('com.apple.quicktime.model') or
format_tags.get('Model'))
if model:
fields['model'] = model
# Extract comment (always extract this for mapping)
comment = format_tags.get('comment') or format_tags.get('comment-eng')
if comment:
fields['comment'] = comment
# Extract original_format
original_format = format_tags.get('original_format') or format_tags.get('original_format-eng')
if original_format:
fields['original_format'] = original_format
return fields
def build_ffmpeg_command(input_file, output_file, metadata_fields, rotation=None):
"""Build the ffmpeg command with appropriate metadata and optional rotation.
Args:
input_file: Input video file path
output_file: Output video file path
metadata_fields: Dictionary of metadata to apply
rotation: Rotation option (None, 'cw', 'ccw', '180')
"""
cmd = [
'ffmpeg',
'-i', input_file,
'-map_metadata', '0'
]
# Add rotation filter if specified
if rotation:
if rotation == 'cw': # 90 degrees clockwise
cmd.extend(['-vf', 'transpose=1'])
elif rotation == 'ccw': # 90 degrees counter-clockwise
cmd.extend(['-vf', 'transpose=2'])
elif rotation == '180': # 180 degrees
cmd.extend(['-vf', 'transpose=2,transpose=2'])
# Add encoding parameters
cmd.extend([
'-c:v', 'libx264',
'-crf', '22',
'-preset', 'medium',
'-pix_fmt', 'yuv420p',
'-color_primaries', 'bt709',
'-color_trc', 'bt709',
'-colorspace', 'bt709',
'-c:a', 'aac',
'-b:a', '128k',
'-movflags', '+faststart'
])
# Add format-level metadata
if 'creation_time' in metadata_fields:
cmd.extend(['-metadata', f'creation_time={metadata_fields["creation_time"]}'])
# Also set for video and audio streams
cmd.extend(['-metadata:s:v:0', f'creation_time={metadata_fields["creation_time"]}'])
cmd.extend(['-metadata:s:a:0', f'creation_time={metadata_fields["creation_time"]}'])
# Add format-level metadata
if 'creation_time' in metadata_fields:
cmd.extend(['-metadata', f'creation_time={metadata_fields["creation_time"]}'])
# Also set for video and audio streams
cmd.extend(['-metadata:s:v:0', f'creation_time={metadata_fields["creation_time"]}'])
cmd.extend(['-metadata:s:a:0', f'creation_time={metadata_fields["creation_time"]}'])
# Add format-level metadata
if 'creation_time' in metadata_fields:
cmd.extend(['-metadata', f'creation_time={metadata_fields["creation_time"]}'])
# Also set for video and audio streams
cmd.extend(['-metadata:s:v:0', f'creation_time={metadata_fields["creation_time"]}'])
cmd.extend(['-metadata:s:a:0', f'creation_time={metadata_fields["creation_time"]}'])
if 'make' in metadata_fields:
cmd.extend(['-metadata', f'make={metadata_fields["make"]}'])
if 'model' in metadata_fields:
cmd.extend(['-metadata', f'model={metadata_fields["model"]}'])
if 'comment' in metadata_fields:
cmd.extend(['-metadata', f'comment={metadata_fields["comment"]}'])
if 'original_format' in metadata_fields:
cmd.extend(['-metadata', f'original_format={metadata_fields["original_format"]}'])
# Clear rotation metadata if rotation was applied
if rotation:
cmd.extend(['-metadata:s:v:0', 'rotate=0'])
cmd.append(output_file)
return cmd
def modernize_video(input_file, output_file, timezone=None, comment_mappings=None, rotation=None, date_override=None):
"""Modernize a single video file.
Args:
input_file: Path to input video
output_file: Path to output video
timezone: Target timezone for timestamp conversion
comment_mappings: Dictionary mapping comments to make/model
rotation: Rotation option ('cw', 'ccw', '180', or None)
date_override: ISO format date string to override all timestamps
"""
print(f"\nProcessing: {input_file}")
if rotation:
rotation_text = {'cw': '90° clockwise', 'ccw': '90° counter-clockwise', '180': '180°'}
print(f" Rotation: {rotation_text.get(rotation, rotation)}")
# Get metadata from original file
metadata = get_video_metadata(input_file)
metadata_fields = extract_metadata_fields(metadata)
# Apply comment-based make/model mapping
if comment_mappings and 'comment' in metadata_fields:
comment = metadata_fields['comment']
if comment in comment_mappings:
mapping = comment_mappings[comment]
if mapping['make']:
metadata_fields['make'] = mapping['make']
print(f" Applying Make: {mapping['make']} (from comment mapping)")
if mapping['model']:
metadata_fields['model'] = mapping['model']
print(f" Applying Model: {mapping['model']} (from comment mapping)")
# Apply date override if specified (takes precedence over timezone conversion)
if date_override:
metadata_fields['creation_time'] = date_override
print(f" Date override: {date_override}")
# Otherwise apply timezone conversion if specified and creation_time exists
elif timezone and 'creation_time' in metadata_fields:
original_time = metadata_fields['creation_time']
converted_time = convert_timezone(original_time, timezone)
if converted_time != original_time:
metadata_fields['creation_time'] = converted_time
print(f" Converted time: {original_time} → {converted_time}")
# If no creation_time exists but timezone is set, note this
elif timezone and 'creation_time' not in metadata_fields:
print(f" Note: No creation_time in original file, timezone conversion skipped")
if metadata_fields:
print(
f" Metadata: {', '.join(f'{k}={v[:30]}...' if len(str(v)) > 30 else f'{k}={v}' for k, v in metadata_fields.items())}")
else:
print(" No metadata found, proceeding without metadata flags")
# Build and run ffmpeg command
cmd = build_ffmpeg_command(input_file, output_file, metadata_fields, rotation)
try:
print(f" Converting to: {output_file}")
subprocess.run(cmd, check=True, capture_output=True)
print(f" ✓ Success!")
return True
except subprocess.CalledProcessError as e:
print(f" ✗ Error converting {input_file}: {e}")
if e.stderr:
print(f" FFmpeg error: {e.stderr.decode()}")
return False
def collect_unique_comments(folder_path, video_extensions):
"""Scan folder and collect all unique comment values."""
folder = Path(folder_path)
unique_comments = set()
video_files = [f for f in folder.iterdir()
if f.is_file()
and f.suffix.lower() in video_extensions
and '_modernized' not in f.stem]
print("\nScanning for unique comments...")
for video_file in video_files:
metadata = get_video_metadata(str(video_file))
fields = extract_metadata_fields(metadata)
if 'comment' in fields:
unique_comments.add(fields['comment'])
return unique_comments
def prompt_comment_mappings(unique_comments):
"""Prompt user to map comments to Make/Model."""
if not unique_comments:
return {}
print(f"\n{'=' * 60}")
print("COMMENT TO MAKE/MODEL MAPPING")
print(f"{'=' * 60}")
print(f"Found {len(unique_comments)} unique comment(s) in videos.")
print("For each comment, you can specify Make and Model to apply.")
print("Press Enter to skip (keep existing or leave blank).\n")
mappings = {}
for comment in sorted(unique_comments):
print(f"\nComment: \"{comment}\"")
make = input(" Enter Make (e.g., Apple, OLYMPUS): ").strip()
model = input(" Enter Model (e.g., iPhone 6S, E-M10): ").strip()
mappings[comment] = {
'make': make if make else None,
'model': model if model else None
}
print(f"\n{'=' * 60}\n")
return mappings
def prompt_rotation_mappings(video_files):
"""Prompt user to specify which files need rotation."""
print(f"\n{'=' * 60}")
print("VIDEO ROTATION (Optional)")
print(f"{'=' * 60}")
print("You can specify files that need to be rotated during processing.")
print("Leave blank if no files need rotation.\n")
rotate_input = input("Enter filenames to rotate (comma-separated) or press Enter to skip: ").strip()
if not rotate_input:
return {}
# Parse the comma-separated filenames
files_to_rotate = [f.strip() for f in rotate_input.split(',')]
rotation_mappings = {}
print("\nFor each file, specify rotation:")
print(" cw = 90° clockwise")
print(" ccw = 90° counter-clockwise")
print(" 180 = 180°")
for filename in files_to_rotate:
# Try to find matching file (case-insensitive, with or without extension)
matching_files = []
filename_lower = filename.lower()
for video_file in video_files:
# Exact match on full name (with extension)
if filename_lower == video_file.name.lower():
matching_files = [video_file]
break
# Exact match on stem (without extension)
elif filename_lower == video_file.stem.lower():
matching_files.append(video_file)
if not matching_files:
print(f"\n⚠ Warning: No file found matching '{filename}', skipping")
continue
if len(matching_files) > 1:
print(f"\n⚠ Warning: Multiple files match '{filename}':")
for f in matching_files:
print(f" - {f.name}")
print(" Skipping this entry. Be more specific.")
continue
matched_file = matching_files[0]
print(f"\nFile: {matched_file.name}")
rotation = input(" Rotation (cw/ccw/180): ").strip().lower()
if rotation in ['cw', 'ccw', '180']:
rotation_mappings[matched_file.name] = rotation
print(f" ✓ Will rotate {rotation}")
else:
print(f" ✗ Invalid rotation '{rotation}', skipping")
if rotation_mappings:
print(f"\n✓ {len(rotation_mappings)} file(s) will be rotated")
print(f"{'=' * 60}\n")
return rotation_mappings
def prompt_date_overrides(video_files):
"""Prompt user to specify which files need date/time overrides."""
print(f"\n{'=' * 60}")
print("DATE/TIME OVERRIDES (Optional)")
print(f"{'=' * 60}")
print("You can manually set the creation date/time for specific files.")
print("This overrides any existing timestamps and timezone conversion.")
print("Leave blank if no files need date overrides.\n")
date_input = input("Enter filenames that need date adjustments (comma-separated) or press Enter to skip: ").strip()
if not date_input:
return {}
# Parse the comma-separated filenames
files_for_dates = [f.strip() for f in date_input.split(',')]
date_mappings = {}
print("\nFor each file, enter the date/time.")
print("Format: YYYY-MM-DD HH:MM:SS (24-hour format)")
print("Example: 2007-01-01 00:00:00")
for filename in files_for_dates:
# Try to find matching file (case-insensitive, with or without extension)
matching_files = []
filename_lower = filename.lower()
for video_file in video_files:
# Exact match on full name (with extension)
if filename_lower == video_file.name.lower():
matching_files = [video_file]
break
# Exact match on stem (without extension)
elif filename_lower == video_file.stem.lower():
matching_files.append(video_file)
if not matching_files:
print(f"\n⚠ Warning: No file found matching '{filename}', skipping")
continue
if len(matching_files) > 1:
print(f"\n⚠ Warning: Multiple files match '{filename}':")
for f in matching_files:
print(f" - {f.name}")
print(" Skipping this entry. Be more specific.")
continue
matched_file = matching_files[0]
print(f"\nFile: {matched_file.name}")
date_str = input(" Date/Time (YYYY-MM-DD HH:MM:SS): ").strip()
# Allow colons in date part (common mistake: 2011:01:01 instead of 2011-01-01)
# Split by space to separate date and time parts
parts = date_str.split()
if len(parts) == 2:
date_part = parts[0].replace(':', '-') # Fix date separators
time_part = parts[1] # Leave time as-is (already has colons)
date_str = f"{date_part} {time_part}"
# Validate format
try:
datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
# Convert to ISO format
iso_date = date_str.replace(' ', 'T')
date_mappings[matched_file.name] = iso_date
print(f" ✓ Will set date to {date_str}")
except ValueError:
print(f" ✗ Invalid date format '{date_str}', skipping")
if date_mappings:
print(f"\n✓ {len(date_mappings)} file(s) will have date overrides")
print(f"{'=' * 60}\n")
return date_mappings
def batch_modernize(folder_path, video_extensions=None):
"""Batch modernize all videos in a folder."""
if video_extensions is None:
video_extensions = {'.mov', '.mp4', '.avi', '.mkv', '.m4v'}
folder = Path(folder_path)
if not folder.exists():
print(f"Error: Folder '{folder_path}' does not exist")
return
# Find all video files
video_files = [f for f in folder.iterdir()
if f.is_file()
and f.suffix.lower() in video_extensions
and '_modernized' not in f.stem]
if not video_files:
print(f"No video files found in '{folder_path}'")
return
print(f"Found {len(video_files)} video(s) to process")
# Ask about timezone conversion
print(f"\n{'=' * 60}")
print("TIMEZONE ADJUSTMENT (Optional)")
print(f"{'=' * 60}")
print("Many cameras record local time but incorrectly mark it as UTC.")
print("This option adds the correct timezone offset to your timestamps.")
print("\nExample: Video recorded at 10:34 AM EST on Dec 25, 2011")
print(" Current: 2011-12-25T10:34:19.000000Z (incorrect UTC marker)")
print(" Corrected: 2011-12-25T10:34:19-05:00 (correct EST offset)")
print("\nCommon US timezones: America/New_York, America/Chicago,")
print(" America/Denver, America/Los_Angeles")
print("Leave blank to keep original timestamps.\n")
timezone = input("Enter timezone where videos were recorded (or press Enter to skip): ").strip()
if timezone:
try:
# Validate timezone
pytz.timezone(timezone)
print(f"✓ Will add correct timezone offset for {timezone}")
print(" (DST will be calculated automatically based on each video's date)")
except pytz.exceptions.UnknownTimeZoneError:
print(f"✗ Unknown timezone '{timezone}', keeping original timestamps")
timezone = None
else:
timezone = None
# Collect unique comments and prompt for mappings
unique_comments = collect_unique_comments(folder_path, video_extensions)
comment_mappings = prompt_comment_mappings(unique_comments)
# Prompt for rotation mappings
rotation_mappings = prompt_rotation_mappings(video_files)
# Prompt for date overrides
date_mappings = prompt_date_overrides(video_files)
# Process videos
print(f"\n{'=' * 60}")
print("PROCESSING VIDEOS")
print(f"{'=' * 60}")
successful = 0
failed = 0
for video_file in video_files:
# Create output filename
# Convert AVI to MOV for better compatibility
if video_file.suffix.lower() == '.avi':
output_name = f"{video_file.stem}_modernized.mov"
else:
output_name = f"{video_file.stem}_modernized{video_file.suffix}"
output_file = video_file.parent / output_name
# Skip if output already exists
if output_file.exists():
print(f"\nSkipping {video_file.name} (output already exists)")
continue
# Check if this file needs rotation
rotation = rotation_mappings.get(video_file.name, None)
# Check if this file has a date override
date_override = date_mappings.get(video_file.name, None)
# Process the video
if modernize_video(str(video_file), str(output_file), timezone, comment_mappings, rotation, date_override):
successful += 1
else:
failed += 1
print(f"\n{'=' * 50}")
print(f"Batch processing complete!")
print(f" Successful: {successful}")
print(f" Failed: {failed}")
print(f"{'=' * 50}")
if __name__ == "__main__":
import sys
# Get folder path from command line argument or use current directory
if len(sys.argv) > 1:
folder_path = sys.argv[1]
else:
folder_path = input("Enter folder path (or press Enter for current directory): ").strip()
if not folder_path:
folder_path = "."
batch_modernize(folder_path)