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| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
VR180 Metadata Injector
|
| 4 |
+
Adds proper VR180 metadata to side-by-side stereoscopic videos for VR headset compatibility.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
import subprocess
|
| 10 |
+
import json
|
| 11 |
+
import tempfile
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
def check_spatial_media_installer():
|
| 15 |
+
"""Check if Google's Spatial Media Metadata Injector is installed."""
|
| 16 |
+
try:
|
| 17 |
+
# Try to find the spatial media installer
|
| 18 |
+
result = subprocess.run(['spatial-media', '--help'],
|
| 19 |
+
capture_output=True, text=True, timeout=10)
|
| 20 |
+
return True
|
| 21 |
+
except (subprocess.TimeoutExpired, FileNotFoundError, subprocess.CalledProcessError):
|
| 22 |
+
return False
|
| 23 |
+
|
| 24 |
+
def install_spatial_media():
|
| 25 |
+
"""Install Google's Spatial Media Metadata Injector."""
|
| 26 |
+
print("Installing Google's Spatial Media Metadata Injector...")
|
| 27 |
+
try:
|
| 28 |
+
# Install via pip
|
| 29 |
+
subprocess.run([sys.executable, '-m', 'pip', 'install', 'spatial-media'],
|
| 30 |
+
check=True)
|
| 31 |
+
print("β
Spatial Media Metadata Injector installed successfully!")
|
| 32 |
+
return True
|
| 33 |
+
except subprocess.CalledProcessError as e:
|
| 34 |
+
print(f"β Failed to install spatial-media: {e}")
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
def create_metadata_json():
|
| 38 |
+
"""Create VR180 metadata JSON configuration."""
|
| 39 |
+
metadata = {
|
| 40 |
+
"spatial_audio": False,
|
| 41 |
+
"stereo_mode": "left-right",
|
| 42 |
+
"projection": "180"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Create temporary JSON file
|
| 46 |
+
temp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False)
|
| 47 |
+
json.dump(metadata, temp_file, indent=2)
|
| 48 |
+
temp_file.close()
|
| 49 |
+
return temp_file.name
|
| 50 |
+
|
| 51 |
+
def inject_vr180_metadata(input_file, output_file):
|
| 52 |
+
"""Inject VR180 metadata into the video file."""
|
| 53 |
+
print(f"Processing: {input_file}")
|
| 54 |
+
print(f"Output: {output_file}")
|
| 55 |
+
|
| 56 |
+
# Create metadata JSON
|
| 57 |
+
metadata_file = create_metadata_json()
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
# Use spatial-media to inject metadata
|
| 61 |
+
cmd = [
|
| 62 |
+
'spatial-media',
|
| 63 |
+
'inject',
|
| 64 |
+
'--input', input_file,
|
| 65 |
+
'--output', output_file,
|
| 66 |
+
'--metadata', metadata_file
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
print("Injecting VR180 metadata...")
|
| 70 |
+
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
|
| 71 |
+
|
| 72 |
+
if result.returncode == 0:
|
| 73 |
+
print("β
VR180 metadata injected successfully!")
|
| 74 |
+
print(f"β
Output saved as: {output_file}")
|
| 75 |
+
return True
|
| 76 |
+
else:
|
| 77 |
+
print(f"β Error injecting metadata: {result.stderr}")
|
| 78 |
+
return False
|
| 79 |
+
|
| 80 |
+
except subprocess.TimeoutExpired:
|
| 81 |
+
print("β Process timed out. Video might be too large.")
|
| 82 |
+
return False
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"β Error: {e}")
|
| 85 |
+
return False
|
| 86 |
+
finally:
|
| 87 |
+
# Clean up temporary metadata file
|
| 88 |
+
try:
|
| 89 |
+
os.unlink(metadata_file)
|
| 90 |
+
except:
|
| 91 |
+
pass
|
| 92 |
+
|
| 93 |
+
def validate_input_file(input_file):
|
| 94 |
+
"""Validate that the input file exists and is a video."""
|
| 95 |
+
if not os.path.exists(input_file):
|
| 96 |
+
print(f"β Input file not found: {input_file}")
|
| 97 |
+
return False
|
| 98 |
+
|
| 99 |
+
# Check if it's a video file
|
| 100 |
+
video_extensions = ['.mp4', '.avi', '.mov', '.mkv', '.webm']
|
| 101 |
+
if not any(input_file.lower().endswith(ext) for ext in video_extensions):
|
| 102 |
+
print(f"β Input file doesn't appear to be a video: {input_file}")
|
| 103 |
+
return False
|
| 104 |
+
|
| 105 |
+
return True
|
| 106 |
+
|
| 107 |
+
def main():
|
| 108 |
+
"""Main function."""
|
| 109 |
+
if len(sys.argv) != 3:
|
| 110 |
+
print("Usage: python add_vr180_metadata.py <input_video> <output_vr180_video>")
|
| 111 |
+
print("\nExample:")
|
| 112 |
+
print("python add_vr180_metadata.py input.mp4 output_vr180.mp4")
|
| 113 |
+
sys.exit(1)
|
| 114 |
+
|
| 115 |
+
input_file = sys.argv[1]
|
| 116 |
+
output_file = sys.argv[2]
|
| 117 |
+
|
| 118 |
+
print("π₯½ VR180 Metadata Injector")
|
| 119 |
+
print("=" * 40)
|
| 120 |
+
|
| 121 |
+
# Validate input file
|
| 122 |
+
if not validate_input_file(input_file):
|
| 123 |
+
sys.exit(1)
|
| 124 |
+
|
| 125 |
+
# Check if spatial media installer is available
|
| 126 |
+
if not check_spatial_media_installer():
|
| 127 |
+
print("π¦ Spatial Media Metadata Injector not found.")
|
| 128 |
+
if not install_spatial_media():
|
| 129 |
+
print("β Cannot proceed without Spatial Media Metadata Injector.")
|
| 130 |
+
sys.exit(1)
|
| 131 |
+
|
| 132 |
+
# Create output directory if it doesn't exist
|
| 133 |
+
output_dir = os.path.dirname(output_file)
|
| 134 |
+
if output_dir and not os.path.exists(output_dir):
|
| 135 |
+
os.makedirs(output_dir)
|
| 136 |
+
|
| 137 |
+
# Inject VR180 metadata
|
| 138 |
+
if inject_vr180_metadata(input_file, output_file):
|
| 139 |
+
print("\nπ Success! Your video is now VR180-compliant!")
|
| 140 |
+
print(f"π± You can now play '{output_file}' in VR headsets like:")
|
| 141 |
+
print(" β’ Oculus Quest/Quest 2/Quest Pro")
|
| 142 |
+
print(" β’ HTC Vive")
|
| 143 |
+
print(" β’ PlayStation VR")
|
| 144 |
+
print(" β’ Windows Mixed Reality")
|
| 145 |
+
print("\nπ‘ Make sure to select 'Side-by-Side' or 'SBS' mode in your VR player.")
|
| 146 |
+
else:
|
| 147 |
+
print("\nβ Failed to inject VR180 metadata.")
|
| 148 |
+
sys.exit(1)
|
| 149 |
+
|
| 150 |
+
if __name__ == "__main__":
|
| 151 |
+
main()
|