Files
traccar_animation/test_video_creation.py
2025-07-10 13:46:05 +03:00

127 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""
Test script for video creation functionality
"""
import os
import sys
sys.path.append('/home/pi/Desktop/traccar_animation')
import cv2
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
def test_video_creation():
"""Test video creation with sample frames"""
print("Testing video creation functionality...")
# Create test directory
test_dir = "/tmp/video_test"
os.makedirs(test_dir, exist_ok=True)
try:
# Create sample frames
frame_files = []
for i in range(10):
# Create a simple test plot
fig, ax = plt.subplots(figsize=(8, 6))
# Simple animation - moving dot
x = np.linspace(0, 10, 100)
y = np.sin(x + i * 0.5)
ax.plot(x, y, 'b-', linewidth=2)
ax.scatter([i], [np.sin(i * 0.5)], c='red', s=100)
ax.set_xlim(0, 10)
ax.set_ylim(-2, 2)
ax.set_title(f'Test Frame {i+1}/10')
ax.grid(True)
# Save frame
frame_path = os.path.join(test_dir, f"frame_{i:03d}.png")
plt.savefig(frame_path, dpi=100, bbox_inches='tight')
plt.close(fig)
frame_files.append(frame_path)
print(f"Created frame {i+1}/10")
print(f"Created {len(frame_files)} test frames")
# Test video creation with different codecs
codecs_to_test = [
('mp4v', '.mp4'),
('XVID', '.avi'),
('MJPG', '.avi')
]
for codec, ext in codecs_to_test:
try:
output_path = os.path.join(test_dir, f"test_video_{codec}{ext}")
# Read first frame for dimensions
first_frame = cv2.imread(frame_files[0])
if first_frame is None:
print(f"❌ Could not read first frame")
continue
height, width, layers = first_frame.shape
print(f"Frame dimensions: {width}x{height}")
# Create video writer
fourcc = cv2.VideoWriter_fourcc(*codec)
video_writer = cv2.VideoWriter(output_path, fourcc, 5.0, (width, height))
if not video_writer.isOpened():
print(f"❌ Could not open video writer with {codec}")
continue
# Write frames
frames_written = 0
for frame_file in frame_files:
frame = cv2.imread(frame_file)
if frame is not None:
video_writer.write(frame)
frames_written += 1
video_writer.release()
# Check result
if os.path.exists(output_path):
file_size = os.path.getsize(output_path)
if file_size > 1024: # At least 1KB
print(f"{codec} video created: {output_path} ({file_size} bytes, {frames_written} frames)")
else:
print(f"{codec} video too small: {file_size} bytes")
else:
print(f"{codec} video not created")
except Exception as e:
print(f"❌ Error with {codec}: {e}")
# Check OpenCV version and capabilities
print(f"\nOpenCV version: {cv2.__version__}")
print(f"OpenCV build info available: {hasattr(cv2, 'getBuildInformation')}")
except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()
finally:
# Clean up
for frame_file in frame_files:
try:
os.remove(frame_file)
except:
pass
try:
os.rmdir(test_dir)
except:
pass
if __name__ == "__main__":
test_video_creation()