-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvideo_utils.py
More file actions
1467 lines (1287 loc) · 62.9 KB
/
Copy pathvideo_utils.py
File metadata and controls
1467 lines (1287 loc) · 62.9 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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import os
import shutil
import subprocess
import time
import sys
from pathlib import Path
from datetime import datetime
# Windows-specific subprocess flag to prevent command windows from popping up
CREATE_NO_WINDOW = 0x08000000 if sys.platform == 'win32' else 0
# Optional libs
HAS_MUTAGEN = False
try:
from mutagen.mp4 import MP4
HAS_MUTAGEN = True
except Exception:
HAS_MUTAGEN = False
HAS_PYAV = False
try:
import av
HAS_PYAV = True
except Exception:
HAS_PYAV = False
HAS_VLC = False
try:
import vlc
HAS_VLC = True
except Exception:
HAS_VLC = False
# PIL for frame rotation during video conversion
HAS_PIL = False
try:
from PIL import Image as PILImage
HAS_PIL = True
except Exception:
HAS_PIL = False
def sanitize_path(path):
"""Sanitize file path by stripping trailing invalid characters and normalizing.
Fixes issues where paths may have trailing braces, spaces, or other invalid chars.
Args:
path: Input path (str or Path)
Returns:
Path object with sanitized absolute path
"""
if path is None:
return None
path_str = str(path).strip()
# Strip common trailing invalid characters
path_str = path_str.rstrip('{}\t ')
# Convert to Path and resolve to absolute
return Path(path_str).resolve()
def _get_video_rotation(file_path):
"""Detect rotation metadata from a video file.
Snapchat videos are often stored in landscape resolution with a rotation
metadata tag that tells players to display them in portrait. When re-encoding,
this rotation must be applied to the frames to preserve correct orientation.
Returns:
int: Clockwise rotation in degrees (0, 90, 180, 270) needed to display
the video frames correctly. 0 means no rotation needed.
"""
rotation = 0
# Try ffprobe first (most reliable)
if check_ffmpeg():
try:
import json as _json
cmd = [
'ffprobe', '-v', 'error', '-select_streams', 'v:0',
'-show_entries', 'stream_tags=rotate:stream_side_data_list',
'-of', 'json', str(file_path)
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10, creationflags=CREATE_NO_WINDOW)
if result.returncode == 0 and result.stdout.strip():
data = _json.loads(result.stdout)
streams = data.get('streams', [])
if streams:
tags = streams[0].get('tags', {})
if 'rotate' in tags:
# The 'rotate' tag directly gives the CW rotation needed
rotation = int(tags['rotate'])
# Only fall back to Display Matrix if 'rotate' tag was not found.
# IMPORTANT: The Display Matrix 'rotation' value has the OPPOSITE
# sign convention from the 'rotate' tag. rotate=90 (CW) corresponds
# to Display Matrix rotation=-90. We negate the display matrix value
# to obtain the clockwise rotation needed.
# (Newer ffmpeg versions drop the 'rotate' tag entirely, so this
# fallback is essential for those builds.)
if rotation == 0:
side_data = streams[0].get('side_data_list', [])
for sd in side_data:
if sd.get('side_data_type') == 'Display Matrix' and 'rotation' in sd:
rotation = -int(float(sd['rotation']))
logging.debug(f"Using Display Matrix rotation (negated): {rotation}° for {file_path}")
except Exception as e:
logging.debug(f"Could not detect rotation via ffprobe: {e}")
# Try PyAV metadata fallback
if rotation == 0 and HAS_PYAV:
try:
container = av.open(str(file_path))
vstream = container.streams.video[0]
# Check stream metadata for rotate tag
if hasattr(vstream, 'metadata') and vstream.metadata:
rotate_val = vstream.metadata.get('rotate', '0')
rotation = int(rotate_val)
container.close()
except Exception as e:
logging.debug(f"Could not detect rotation via PyAV: {e}")
# Normalize to 0-359 range, handle negative values
rotation = rotation % 360
if rotation < 0:
rotation += 360
logging.debug(f"Detected rotation for {file_path}: {rotation}°")
return rotation
def check_ffmpeg():
import shutil
if shutil.which('ffmpeg'):
return True
# GUI apps on macOS are launched by LaunchServices/Finder, not a login shell,
# so they get a minimal default PATH that excludes Homebrew's install dirs
# (/opt/homebrew/bin on Apple Silicon, /usr/local/bin on Intel). ffmpeg can
# be installed and working fine in Terminal yet invisible to shutil.which
# here. Check the common install locations directly and, if found, patch
# this process's PATH so the bare 'ffmpeg'/'ffprobe' calls below resolve.
if sys.platform == 'darwin':
candidate_dirs = ['/opt/homebrew/bin', '/usr/local/bin', '/opt/local/bin']
elif sys.platform.startswith('linux'):
candidate_dirs = ['/usr/local/bin', '/snap/bin', '/var/lib/flatpak/exports/bin']
else:
candidate_dirs = []
for d in candidate_dirs:
if os.path.exists(os.path.join(d, 'ffmpeg')):
os.environ['PATH'] = d + os.pathsep + os.environ.get('PATH', '')
return True
return False
def check_vlc():
return HAS_VLC
def find_vlc_executable():
if sys.platform == 'win32':
vlc_paths = [
r"C:\Program Files\VideoLAN\VLC\vlc.exe",
r"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe",
]
elif sys.platform == 'darwin':
vlc_paths = [
"/Applications/VLC.app/Contents/MacOS/VLC",
os.path.expanduser("~/Applications/VLC.app/Contents/MacOS/VLC"),
]
else:
vlc_paths = [
"/usr/bin/vlc",
"/usr/local/bin/vlc",
"/snap/bin/vlc",
"/var/lib/flatpak/exports/bin/org.videolan.VLC",
]
vlc_in_path = shutil.which('vlc')
if vlc_in_path:
return vlc_in_path
for p in vlc_paths:
if os.path.exists(p):
return p
return None
def validate_video_file(file_path, min_duration=0.1, min_size=1000):
"""Validate video file using ffprobe or fallback to size check.
Args:
file_path: Path to video file
min_duration: Minimum duration in seconds (default 0.1)
min_size: Minimum file size in bytes (default 1000)
Returns:
Tuple of (is_valid: bool, info: dict)
info contains: duration, has_video, has_audio, codec, error
"""
file_path = sanitize_path(file_path)
info = {
'duration': None,
'has_video': False,
'has_audio': False,
'codec': None,
'error': None
}
# Basic checks
if not file_path.exists():
info['error'] = 'File does not exist'
return False, info
file_size = file_path.stat().st_size
if file_size < min_size:
info['error'] = f'File too small: {file_size} bytes'
return False, info
# Try ffprobe validation if available
if check_ffmpeg():
try:
# Get format info (duration)
cmd_format = [
'ffprobe', '-v', 'error',
'-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1',
str(file_path)
]
result = subprocess.run(cmd_format, capture_output=True, text=True, timeout=10, creationflags=CREATE_NO_WINDOW)
if result.returncode == 0 and result.stdout.strip():
try:
info['duration'] = float(result.stdout.strip())
except ValueError:
pass
# Get stream info
cmd_streams = [
'ffprobe', '-v', 'error',
'-show_entries', 'stream=codec_type,codec_name',
'-of', 'json',
str(file_path)
]
result = subprocess.run(cmd_streams, capture_output=True, text=True, timeout=10, creationflags=CREATE_NO_WINDOW)
if result.returncode == 0:
import json
data = json.loads(result.stdout)
for stream in data.get('streams', []):
codec_type = stream.get('codec_type')
if codec_type == 'video':
info['has_video'] = True
info['codec'] = stream.get('codec_name')
elif codec_type == 'audio':
info['has_audio'] = True
# Validation logic
if not info['has_video']:
info['error'] = 'No video stream found'
return False, info
if info['duration'] is not None and info['duration'] < min_duration:
info['error'] = f"Duration too short: {info['duration']}s"
return False, info
logging.debug(f"Video validation passed: {file_path} - duration={info['duration']}s, codec={info['codec']}")
return True, info
except subprocess.TimeoutExpired:
logging.warning(f"ffprobe validation timed out for {file_path}")
except Exception as e:
logging.debug(f"ffprobe validation error: {e}")
# Fallback: if ffprobe not available or failed, just check size
logging.debug(f"Video validation (size-only): {file_path} - {file_size} bytes")
return True, info
def convert_with_vlc(input_path, output_path=None):
"""Convert video using VLC (Python bindings or subprocess).
Returns:
Tuple of (success: bool, result: Path or error_message: str)
"""
input_path = sanitize_path(input_path)
if output_path is None:
base = input_path.stem
ext = input_path.suffix
output_path = input_path.parent / f"{base}_converted{ext}"
else:
output_path = sanitize_path(output_path)
if HAS_VLC:
try:
return convert_with_vlc_python(input_path, output_path)
except Exception as e:
logging.warning(f"python-vlc failed: {e}. Trying subprocess method...")
return convert_with_vlc_subprocess(input_path, output_path)
def convert_with_vlc_python(input_path, output_path):
"""Convert video using VLC Python bindings.
Returns:
Tuple of (success: bool, result: Path or error_message: str)
"""
input_path = sanitize_path(input_path)
output_path = sanitize_path(output_path)
try:
logging.info(f"Converting with VLC (Python bindings): {input_path}")
instance = vlc.Instance('--no-xlib')
player = instance.media_player_new()
media = instance.media_new(str(input_path))
# Use forward slashes for VLC compatibility
output_str = str(output_path).replace('\\', '/')
transcode_options = (
f"#transcode{{"
f"vcodec=h264,venc=x264{{preset=medium,profile=main}},acodec=mp3,ab=192,channels=2,samplerate=44100}}:"
f"standard{{access=file,mux=mp4,dst={output_str}}}"
)
media.add_option(f":sout={transcode_options}")
media.add_option(":sout-keep")
player.set_media(media)
player.play()
timeout = 300
start_time = time.time()
while time.time() - start_time < timeout:
state = player.get_state()
if state == vlc.State.Ended:
break
elif state == vlc.State.Error:
player.stop()
return False, "VLC conversion error"
time.sleep(0.5)
else:
player.stop()
if output_path.exists():
output_path.unlink()
return False, "VLC conversion timed out"
player.stop()
player.release()
media.release()
if output_path.exists() and output_path.stat().st_size > 1000:
logging.info(f"VLC Python conversion successful: {output_path}")
return True, output_path
else:
if output_path.exists():
output_path.unlink()
return False, "VLC conversion failed"
except Exception as e:
logging.error(f"VLC Python conversion error: {e}", exc_info=True)
if output_path.exists():
try:
output_path.unlink()
except Exception:
pass
raise
def convert_with_vlc_subprocess(input_path, output_path):
"""Convert video using VLC subprocess with proper path sanitization.
Returns:
Tuple of (success: bool, result: Path or error_message: str)
"""
vlc_path = find_vlc_executable()
if not vlc_path:
logging.error("VLC executable not found on system")
return False, "VLC not installed"
# Sanitize paths to prevent trailing brace issues
input_path = sanitize_path(input_path)
output_path = sanitize_path(output_path)
# CRITICAL: Use quotes around path in --sout to prevent issues with special chars
# Also escape the braces in the transcode options properly
output_str = str(output_path).replace('\\', '/') # VLC prefers forward slashes
cmd = [
vlc_path, "-I", "dummy", "--no-repeat", "--no-loop",
str(input_path),
"--sout",
(f"#transcode{{vcodec=h264,venc=x264{{preset=medium,profile=main}},acodec=mp3,ab=192,channels=2,samplerate=44100}}:"
f"standard{{access=file,mux=mp4,dst={output_str}}}"),
"vlc://quit"
]
logging.debug(f"VLC command: {' '.join(cmd)}")
logging.info(f"Converting with VLC subprocess: {input_path} -> {output_path}")
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300, creationflags=CREATE_NO_WINDOW)
# Log stderr for debugging
if result.stderr:
logging.debug(f"VLC stderr: {result.stderr[:500]}")
if output_path.exists() and output_path.stat().st_size > 1000:
logging.info(f"VLC subprocess conversion successful: {output_path}")
return True, output_path
else:
if output_path.exists():
output_path.unlink()
logging.error(f"VLC subprocess conversion failed - output not created or too small")
return False, "VLC subprocess conversion failed"
except subprocess.TimeoutExpired:
logging.error("VLC subprocess conversion timed out")
if output_path.exists():
try:
output_path.unlink()
except Exception:
pass
return False, "VLC subprocess timeout"
except Exception as e:
logging.error(f"VLC subprocess conversion error: {e}", exc_info=True)
if output_path.exists():
try:
output_path.unlink()
except Exception:
pass
return False, str(e)
def set_video_metadata(file_path, date_obj, latitude, longitude, timezone_offset=None):
"""Set video metadata using mutagen (MP4).
Args:
file_path: Path to the MP4 video file
date_obj: datetime object with timezone info (local time)
latitude: GPS latitude (or None)
longitude: GPS longitude (or None)
timezone_offset: Timezone offset string like '-05:00'
"""
if not HAS_MUTAGEN:
logging.debug("Skipping video metadata: mutagen not available")
return False
backup_path = f"{file_path}.backup"
try:
# Quick sanity check: file must exist and be reasonably sized
if not os.path.exists(file_path):
logging.error("Video file does not exist: %s", file_path)
return False
shutil.copy2(file_path, backup_path)
try:
video = MP4(file_path)
# Format with timezone offset for better app compatibility
if timezone_offset:
creation_time = date_obj.strftime("%Y-%m-%dT%H:%M:%S") + timezone_offset
else:
creation_time = date_obj.strftime("%Y-%m-%dT%H:%M:%SZ")
video["\xa9day"] = creation_time
try:
video["\xa9ART"] = date_obj.strftime("%Y")
except Exception:
pass
# CRITICAL for iCloud/Apple Photos: Set Apple-specific creation date
# iCloud reads 'com.apple.quicktime.creationdate' for the "date taken"
# Without this, iCloud may show videos with wrong dates (e.g., 01/08/1970)
try:
from mutagen.mp4 import MP4FreeForm, AtomDataType
video["----:com.apple.quicktime:creationdate"] = [
MP4FreeForm(creation_time.encode('utf-8'), dataformat=AtomDataType.UTF8)
]
logging.debug(f"Set Apple QuickTime creationdate: {creation_time}")
except (ImportError, AttributeError):
# Fallback for older mutagen versions
video["----:com.apple.quicktime:creationdate"] = [creation_time.encode('utf-8')]
logging.debug(f"Set Apple QuickTime creationdate (fallback): {creation_time}")
# Add GPS metadata if available
if latitude is not None and longitude is not None:
location_str = f"{latitude:+.6f}{longitude:+.6f}/"
try:
from mutagen.mp4 import MP4FreeForm, AtomDataType
video["----:com.apple.quicktime:location-ISO6709"] = [
MP4FreeForm(location_str.encode('utf-8'), dataformat=AtomDataType.UTF8)
]
video["----:com.apple.quicktime:latitude"] = [
MP4FreeForm(str(latitude).encode('utf-8'), dataformat=AtomDataType.UTF8)
]
video["----:com.apple.quicktime:longitude"] = [
MP4FreeForm(str(longitude).encode('utf-8'), dataformat=AtomDataType.UTF8)
]
except (ImportError, AttributeError):
video["----:com.apple.quicktime:location-ISO6709"] = [location_str.encode('utf-8')]
video["----:com.apple.quicktime:latitude"] = [str(latitude).encode('utf-8')]
video["----:com.apple.quicktime:longitude"] = [str(longitude).encode('utf-8')]
logging.info(f"Setting GPS metadata via mutagen: lat={latitude}, lon={longitude} for {file_path}")
# Freeform '----' atoms are iTunes-style app-private tags: Apple
# Photos/Finder/Spotlight only read GPS from mdta keys or udta
# (C)xyz/loci atoms, which mutagen cannot write.
logging.warning(
"GPS written as freeform ilst atoms only; Apple software will not "
"show a location for %s. Prefer the ffmpeg or PyAV writer.", file_path
)
else:
logging.info(f"No GPS data available for video (mutagen): {file_path}")
# write tags
video.save()
# verify by attempting to load the saved file with mutagen
try:
_ = MP4(file_path)
except Exception as e:
logging.error("Mutagen failed to re-open saved file, restoring backup: %s", e)
shutil.copy2(backup_path, file_path)
os.remove(backup_path)
return False
os.remove(backup_path)
logging.info("Successfully set video metadata using mutagen: %s", file_path)
return True
except Exception as e:
logging.exception("Error writing mutagen metadata, restoring backup if any: %s", file_path)
if os.path.exists(backup_path):
try:
shutil.copy2(backup_path, file_path)
os.remove(backup_path)
except Exception:
pass
return False
except Exception:
logging.exception("Unexpected error in set_video_metadata for %s", file_path)
if os.path.exists(backup_path):
try:
os.remove(backup_path)
except Exception:
pass
return False
def _iter_boxes(data, start, end):
"""Yield (name, box_start, box_end) for MP4 boxes in data[start:end]."""
pos = start
while pos + 8 <= end:
size = int.from_bytes(data[pos:pos + 4], 'big')
name = bytes(data[pos + 4:pos + 8])
if size == 0: # box extends to end of enclosing container
size = end - pos
elif size == 1: # 64-bit extended size
if pos + 16 > end:
return
size = int.from_bytes(data[pos + 8:pos + 16], 'big')
if size < 8 or pos + size > end:
return
yield name, pos, pos + size
pos += size
def _meta_children_offset(data, meta_start, meta_end):
"""Offset of a 'meta' box's first child, handling both layouts.
ISO BMFF meta is a full box (4-byte version/flags before children);
QTFF movie-level meta is a plain container (children start immediately).
Detect by checking where the 'hdlr' fourcc lands. Returns None if neither.
"""
if meta_end - meta_start >= 16 and bytes(data[meta_start + 12:meta_start + 16]) == b'hdlr':
return 8 # QTFF: no version/flags
if meta_end - meta_start >= 20 and bytes(data[meta_start + 16:meta_start + 20]) == b'hdlr':
return 12 # ISO: 4-byte version/flags first
return None
def _meta_has_mdta_handler(data, meta_start, meta_end):
"""True if a 'meta' box declares the mdta (QuickTime Keys) handler."""
offset = _meta_children_offset(data, meta_start, meta_end)
if offset is None:
return False
# hdlr handler type is at offset 16 within the hdlr box
# (8 header + 4 version/flags + 4 predefined)
for name, s, e in _iter_boxes(data, meta_start + offset, meta_end):
if name == b'hdlr' and e - s >= 20:
return bytes(data[s + 16:s + 20]) == b'mdta'
return False
def relocate_apple_metadata(file_path, latitude=None, longitude=None):
"""Move the mdta Keys 'meta' box from moov/udta up to moov itself.
ffmpeg's mov muxer (and therefore PyAV, which drives the same muxer) writes
movflags=use_metadata_tags Keys metadata inside moov/udta/meta. ffprobe and
exiftool read it there, but Apple Photos/Finder/Spotlight only read Keys
from a 'meta' box that is a DIRECT child of 'moov' — the placement iPhones
use and the QuickTime File Format spec defines. Without this relocation,
GPS tags exist in the file but macOS shows no location.
The moved box also has its ISO version/flags field stripped: QTFF defines
movie-level meta as a plain container, and Apple parsers read the 4 zero
bytes as a terminator atom, stopping before the GPS keys (confirmed via
exiftool and mdls on v10.0.5 output). All rewriting happens within moov;
when moov is not the final top-level box, any size change aborts the
rewrite so chunk offsets in stco/co64 can never be invalidated. When
coordinates are given and moov is last, an Android-style (C)xyz GPS atom
is also appended to udta for broader player compatibility.
Returns True if the file now has a moov-level meta box (moved or already
there), False if the structure could not be safely rewritten.
"""
file_path = str(file_path)
try:
with open(file_path, 'rb') as f:
data = f.read()
top = list(_iter_boxes(data, 0, len(data)))
moov_list = [(s, e) for name, s, e in top if name == b'moov']
if len(moov_list) != 1:
logging.warning("relocate_apple_metadata: expected 1 moov, found %d in %s",
len(moov_list), file_path)
return False
moov_start, moov_end = moov_list[0]
# Only handle plain 32-bit moov header (always the case for our writers)
if int.from_bytes(data[moov_start:moov_start + 4], 'big') in (0, 1):
return False
moov_is_last = moov_end == len(data)
moov_children = list(_iter_boxes(data, moov_start + 8, moov_end))
moov_meta = next(((s, e) for name, s, e in moov_children if name == b'meta'), None)
if moov_meta is not None:
s, e = moov_meta
if _meta_children_offset(data, s, e) == 8:
logging.debug("relocate_apple_metadata: QTFF moov-level meta already present in %s", file_path)
return True
if _meta_children_offset(data, s, e) == 12 and moov_is_last:
# Repair a meta box relocated with the ISO version/flags still in
# place (v10.0.5 output): Apple parsers read those 4 zero bytes
# as a terminator atom and never reach the GPS keys.
fixed_meta = (e - s - 4).to_bytes(4, 'big') + b'meta' + data[s + 12:e]
moov_payload = bytearray()
for name, cs, ce in moov_children:
moov_payload += fixed_meta if cs == s else data[cs:ce]
new_moov = (8 + len(moov_payload)).to_bytes(4, 'big') + b'moov' + moov_payload
temp_path = f"{file_path}.meta.tmp"
with open(temp_path, 'wb') as f:
f.write(data[:moov_start])
f.write(new_moov)
os.replace(temp_path, file_path)
logging.info("Stripped version/flags from moov-level meta for %s", file_path)
return True
return False
udta = next(((s, e) for name, s, e in moov_children if name == b'udta'), None)
if udta is None:
return False
udta_start, udta_end = udta
if int.from_bytes(data[udta_start:udta_start + 4], 'big') in (0, 1):
return False
udta_children = list(_iter_boxes(data, udta_start + 8, udta_end))
meta = next(((s, e) for name, s, e in udta_children
if name == b'meta' and _meta_has_mdta_handler(data, s, e)), None)
if meta is None:
return False
meta_start, meta_end = meta
meta_bytes = data[meta_start:meta_end]
# QTFF movie-level 'meta' is a plain container, unlike the ISO full-box
# layout ffmpeg writes inside udta. Apple's parsers (Spotlight, Photos,
# Finder) treat the 4 zero version/flags bytes as a zero-length
# terminator atom and stop reading before the GPS keys, so strip them
# while moving the box to moov level.
if _meta_children_offset(data, meta_start, meta_end) == 12:
meta_bytes = ((meta_end - meta_start - 4).to_bytes(4, 'big') + b'meta'
+ data[meta_start + 12:meta_end])
# Rebuild udta without the meta box (append (C)xyz if safe and wanted)
udta_payload = data[udta_start + 8:meta_start] + data[meta_end:udta_end]
has_xyz = any(name == b'\xa9xyz' for name, s, e in udta_children)
if (latitude is not None and longitude is not None
and not has_xyz and moov_is_last):
loc = f"{latitude:+.6f}{longitude:+.6f}/".encode('ascii')
xyz = ((12 + len(loc)).to_bytes(4, 'big') + b'\xa9xyz'
+ len(loc).to_bytes(2, 'big') + b'\x15\xc7' + loc)
udta_payload += xyz
new_udta = (8 + len(udta_payload)).to_bytes(4, 'big') + b'udta' + udta_payload
# Rebuild moov: original children with udta swapped, meta appended last
moov_payload = bytearray()
for name, s, e in moov_children:
if s == udta_start:
moov_payload += new_udta
else:
moov_payload += data[s:e]
moov_payload += meta_bytes
new_moov = (8 + len(moov_payload)).to_bytes(4, 'big') + b'moov' + moov_payload
# Unless moov is the final box, the rewrite must not change its size
# (anything after moov would shift and break chunk offsets)
if not moov_is_last and len(new_moov) != moov_end - moov_start:
logging.warning("relocate_apple_metadata: size change with trailing data in %s", file_path)
return False
temp_path = f"{file_path}.meta.tmp"
with open(temp_path, 'wb') as f:
f.write(data[:moov_start])
f.write(new_moov)
f.write(data[moov_end:])
os.replace(temp_path, file_path)
logging.info("Relocated Keys metadata to moov level for %s", file_path)
return True
except Exception:
logging.exception("relocate_apple_metadata failed for %s", file_path)
try:
temp_path = f"{file_path}.meta.tmp"
if os.path.exists(temp_path):
os.remove(temp_path)
except Exception:
pass
return False
def set_video_metadata_pyav(file_path, date_obj, latitude, longitude, timezone_offset=None):
"""Set video metadata by remuxing with PyAV (no ffmpeg CLI required).
Writes the same QuickTime 'mdta' key metadata as the ffmpeg CLI path
(movflags=use_metadata_tags), which is the format Apple Photos/Finder/
Spotlight read GPS coordinates from. The packaged app bundles PyAV but not
an ffmpeg binary, so on machines without ffmpeg this is the only writer
that produces Apple-readable location metadata (mutagen can only write
iTunes-style freeform atoms, which Apple software ignores).
Args:
file_path: Path to the MP4 video file
date_obj: datetime object with timezone info (local time)
latitude: GPS latitude (or None)
longitude: GPS longitude (or None)
timezone_offset: Timezone offset string like '-05:00'
"""
if not HAS_PYAV:
logging.debug("PyAV not available for metadata writing")
return False
file_path = str(file_path)
temp_output = f"{file_path}.temp.mp4"
input_container = None
output_container = None
try:
if timezone_offset:
creation_time_str = date_obj.strftime("%Y-%m-%dT%H:%M:%S") + timezone_offset
else:
creation_time_str = date_obj.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
metadata = {
'creation_time': creation_time_str,
'date': creation_time_str,
'com.apple.quicktime.creationdate': creation_time_str,
}
if latitude is not None and longitude is not None:
location_iso = f'{latitude:+.6f}{longitude:+.6f}/'
metadata.update({
'location': location_iso,
'location-eng': f'{latitude}, {longitude}',
'com.apple.quicktime.location.ISO6709': location_iso,
'com.apple.quicktime.GPS.latitude': str(latitude),
'com.apple.quicktime.GPS.longitude': str(longitude),
})
logging.info(f"Adding GPS metadata via PyAV: lat={latitude}, lon={longitude}")
else:
logging.info(f"No GPS data available for video (PyAV): {file_path}")
input_container = av.open(file_path)
output_container = av.open(
temp_output, 'w', format='mp4',
options={'movflags': 'use_metadata_tags'}
)
output_container.metadata.update(metadata)
# Stream-copy (no re-encode): map each A/V stream to an output twin
stream_map = {}
for stream in input_container.streams:
if stream.type in ('video', 'audio'):
try:
out_stream = output_container.add_stream_from_template(stream)
except AttributeError:
# PyAV < 12 spelling
out_stream = output_container.add_stream(template=stream)
stream_map[stream.index] = out_stream
for packet in input_container.demux():
if packet.dts is None:
continue # flush/probe packets can't be muxed
out_stream = stream_map.get(packet.stream.index)
if out_stream is None:
continue
packet.stream = out_stream
output_container.mux(packet)
input_container.close()
input_container = None
output_container.close()
output_container = None
if os.path.exists(temp_output) and os.path.getsize(temp_output) > 1000:
os.replace(temp_output, file_path)
# ffmpeg's muxer leaves the Keys meta box in moov/udta where Apple
# software never looks — move it to moov level (iPhone placement)
if not relocate_apple_metadata(file_path, latitude, longitude):
logging.warning("Keys metadata left in moov/udta for %s; "
"Apple Photos may not show location", file_path)
logging.info(f"Successfully set video metadata using PyAV: {file_path}")
return True
logging.error(f"PyAV metadata remux produced no/empty output for {file_path}")
if os.path.exists(temp_output):
os.remove(temp_output)
return False
except Exception:
logging.exception("PyAV metadata remux failed for %s", file_path)
for container in (input_container, output_container):
try:
if container:
container.close()
except Exception:
pass
if os.path.exists(temp_output):
try:
os.remove(temp_output)
except Exception:
pass
return False
def set_video_metadata_ffmpeg(file_path, date_obj, latitude, longitude, timezone_offset=None):
"""Set video metadata using ffmpeg.
Args:
file_path: Path to the video file
date_obj: datetime object with timezone info (local time)
latitude: GPS latitude (or None)
longitude: GPS longitude (or None)
timezone_offset: Timezone offset string like '-05:00'
"""
if not check_ffmpeg():
logging.debug("ffmpeg not available for metadata writing")
return False
temp_output = None
try:
temp_output = f"{file_path}.temp.mp4"
# Format with timezone offset
if timezone_offset:
creation_time_str = date_obj.strftime("%Y-%m-%dT%H:%M:%S") + timezone_offset
else:
creation_time_str = date_obj.strftime("%Y-%m-%dT%H:%M:%S")
# Also create a UTC version for the moov header (QuickTime standard)
# iCloud reads creation_time from moov.mvhd which expects UTC
utc_creation_str = date_obj.strftime("%Y-%m-%dT%H:%M:%S")
if timezone_offset:
utc_creation_str = creation_time_str # ffmpeg handles TZ conversion internally
else:
utc_creation_str = creation_time_str + "Z"
cmd = [
'ffmpeg', '-y', '-i', str(file_path), '-c', 'copy',
'-metadata', f'creation_time={utc_creation_str}',
'-metadata', f'date={creation_time_str}',
# Apple-specific metadata for iCloud/Apple Photos compatibility
# This is the primary tag iCloud uses for "date taken" on videos
'-metadata', f'com.apple.quicktime.creationdate={creation_time_str}',
'-movflags', '+use_metadata_tags',
]
# Add location metadata if available
if latitude is not None and longitude is not None:
location_iso = f'{latitude:+.6f}{longitude:+.6f}/'
cmd.extend([
'-metadata', f'location={location_iso}',
'-metadata', f'location-eng={latitude}, {longitude}',
'-metadata', f'com.apple.quicktime.location.ISO6709={location_iso}',
'-metadata', f'com.apple.quicktime.GPS.latitude={latitude}',
'-metadata', f'com.apple.quicktime.GPS.longitude={longitude}'
])
logging.info(f"Adding GPS metadata to video: lat={latitude}, lon={longitude}")
else:
logging.info(f"No GPS data available for video: {file_path}")
cmd.append(str(temp_output))
logging.debug(f"Setting video metadata with ffmpeg: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True, timeout=60, creationflags=CREATE_NO_WINDOW)
if result.returncode == 0 and os.path.exists(temp_output):
try:
os.remove(file_path)
os.rename(temp_output, file_path)
# ffmpeg writes the Keys meta box in moov/udta where Apple
# software never looks — move it to moov level (iPhone placement)
if not relocate_apple_metadata(file_path, latitude, longitude):
logging.warning("Keys metadata left in moov/udta for %s; "
"Apple Photos may not show location", file_path)
logging.info(f"Successfully set video metadata using ffmpeg: {file_path}")
return True
except Exception as e:
logging.error(f"Failed to replace file after metadata update: {e}")
if os.path.exists(temp_output):
os.remove(temp_output)
return False
else:
if os.path.exists(temp_output):
os.remove(temp_output)
return False
except subprocess.TimeoutExpired:
if temp_output and os.path.exists(temp_output):
try:
os.remove(temp_output)
except Exception:
pass
return False
except Exception:
if temp_output and os.path.exists(temp_output):
try:
os.remove(temp_output)
except Exception:
pass
return False
def enforce_portrait_video(file_path, timeout=300):
"""Apply rotation metadata to video frames so the file displays correctly.
Only rotates when explicit rotation metadata (rotate tag or display matrix)
is present. Does NOT blindly force landscape videos to portrait — genuinely
landscape content is left untouched.
Uses ffmpeg auto-rotation (default) which is the most reliable approach
across ffmpeg versions.
"""
if not os.path.exists(file_path):
return False, "File not found"
# Detect rotation from metadata
rotation = _get_video_rotation(file_path)
if rotation not in (90, 180, 270):
# No rotation metadata (or rotation=0). Leave the video as-is.
# Genuinely landscape content should NOT be forced to portrait.
return True, "No rotation needed"
# Try ffmpeg first — let it auto-rotate naturally
if check_ffmpeg():
try:
out_path = f"{file_path}.rotated{Path(file_path).suffix}"
# Let ffmpeg auto-rotate (default behaviour): it reads the display
# matrix / rotate tag, applies the rotation during decode, and produces
# output with correct orientation and no leftover rotation metadata.
ffmpeg_cmd = [
'ffmpeg', '-y',
'-i', file_path,
'-c:v', 'libx264', '-crf', '18', '-preset', 'veryfast',
'-c:a', 'copy',
'-metadata:s:v:0', 'rotate=0', # Strip any leftover rotate tag
out_path
]
logging.info(f"enforce_portrait: applying {rotation}° via ffmpeg auto-rotate")
proc = subprocess.run(ffmpeg_cmd, capture_output=True, text=True, timeout=timeout, creationflags=CREATE_NO_WINDOW)
if proc.returncode == 0 and os.path.exists(out_path) and os.path.getsize(out_path) > 1000:
try:
backup = f"{file_path}.backup"
shutil.move(file_path, backup)
shutil.move(out_path, file_path)
try:
os.remove(backup)
except Exception:
pass
return True, file_path
except Exception as e:
try:
if os.path.exists(backup) and not os.path.exists(file_path):
shutil.move(backup, file_path)
except Exception:
pass
if os.path.exists(out_path):
os.remove(out_path)
return False, f"Failed to replace original: {e}"
else:
if os.path.exists(out_path):
try:
os.remove(out_path)
except Exception:
pass
return False, f"ffmpeg failed: {proc.stderr}"
except Exception as e:
logging.debug(f"ffmpeg portrait enforcement error: {e}", exc_info=True)
# Fallback to PyAV if available — only rotate per metadata
if HAS_PYAV:
try:
input_container = av.open(file_path)