MP4SVG - Video to SVG Converter
A Python package that converts MP4 video files into SVG containers using various encoding methods, with full roundtrip extraction capabilities.
Features
- Multiple Encoding Methods: ASCII85, Polyglot, Vector Animation, QR Code, and Hybrid
- XML-Safe Embedding: Proper handling of binary data in SVG/XML context
- Interactive SVG: Embedded JavaScript for in-browser video playback
- Thumbnail Previews: JPEG thumbnails for system/browser compatibility
- Data Integrity: Built-in validation and checksum verification
- CLI Interface: Command-line tools for conversion and extraction
- Extensible Architecture: Modular design for easy extension
Installation
From Source
git clone <repository-url>
cd mp4svg
pip install -e .
Dependencies
- Python 3.8+
- OpenCV (cv2)
- NumPy
- Pillow (PIL)
- qrcode
- lxml
Quick Start
Command Line Usage
# Convert MP4 to SVG using ASCII85 encoding
mp4svg input.mp4 output.svg --method ascii85
# Extract MP4 from SVG
mp4svg output.svg extracted.mp4 --extract
# Use polyglot method with custom chunk size
mp4svg input.mp4 output.svg --method polyglot --chunk-size 4096
# Convert to QR codes with error correction
mp4svg input.mp4 output.svg --method qrcode --error-correction M
# Try all methods and compare (hybrid)
mp4svg input.mp4 output.svg --method hybrid
Python API
from mp4svg import ASCII85SVGConverter, PolyglotSVGConverter
# ASCII85 encoding (recommended for most use cases)
converter = ASCII85SVGConverter()
svg_path = converter.convert('video.mp4', 'output.svg')
# Extract back to MP4
success = converter.extract('output.svg', 'extracted.mp4')
# Polyglot embedding (larger files, better compatibility)
polyglot = PolyglotSVGConverter()
svg_path = polyglot.convert('video.mp4', 'polyglot.svg')
Encoding Methods
1. ASCII85 (Recommended)
- Best for: General use, good compression ratio
- Overhead: ~25% size increase
- Features: Interactive playback, thumbnail preview, XML-safe
- Roundtrip: Full fidelity extraction
from mp4svg import ASCII85SVGConverter
converter = ASCII85SVGConverter()
2. Polyglot
- Best for: Maximum compatibility, embedded metadata
- Overhead: ~33% size increase
- Features: SVG + embedded binary data, summary statistics
- Roundtrip: Full fidelity extraction
from mp4svg import PolyglotSVGConverter
converter = PolyglotSVGConverter()
3. Vector Animation
- Best for: Stylized animation, artistic effects
- Overhead: Variable (depends on complexity)
- Features: True vector animation, scalable
- Roundtrip: Not supported (lossy conversion)
from mp4svg import SVGVectorFrameConverter
converter = SVGVectorFrameConverter()
4. QR Code
- Best for: Educational purposes, distributed storage
- Overhead: Very high (~10x size increase)
- Features: Multiple QR frames, chunk-based storage
- Roundtrip: Full fidelity with extraction script
from mp4svg import QRCodeSVGConverter
converter = QRCodeSVGConverter(chunk_size=1000)
5. Hybrid
- Best for: Comparing all methods, choosing optimal
- Features: Tests all methods, size comparison, format detection
- Output: Multiple files with performance report
from mp4svg import HybridSVGConverter
converter = HybridSVGConverter()
SVG Validation
The package includes comprehensive validation utilities:
from mp4svg.validators import SVGValidator, IntegrityValidator
# Validate SVG structure and detect format
svg_validator = SVGValidator()
result = svg_validator.validate_svg_file('output.svg')
# Check data integrity and extraction
integrity_validator = IntegrityValidator()
integrity_result = integrity_validator.validate_integrity('output.svg', 'original.mp4')
# Batch validate directory
batch_result = integrity_validator.batch_validate('/path/to/svg/files/')
Interactive SVG Features
Generated SVG files include:
- Thumbnail Preview: JPEG thumbnail for system compatibility
- Play Button: Click to decode and play video
- JavaScript Decoder: Built-in ASCII85/Base64 decoder
- Error Handling: Robust namespace and element selection
Project Structure
mp4svg/
├── src/mp4svg/
│ ├── __init__.py # Package exports
│ ├── base.py # Base converter class
│ ├── ascii85.py # ASCII85 converter
│ ├── polyglot.py # Polyglot converter
│ ├── vector.py # Vector animation converter
│ ├── qrcode.py # QR code converter
│ ├── hybrid.py # Hybrid converter
│ ├── cli.py # Command-line interface
│ └── validators/ # Validation utilities
│ ├── __init__.py
│ ├── svg_validator.py
│ └── integrity_validator.py
├── tests/ # Test suite
├── pyproject.toml # Poetry configuration
└── README.md # This file
Testing
Run the test suite:
# Install development dependencies
pip install -e ".[dev]"
# Run all tests
pytest tests/
# Run specific test file
pytest tests/test_converters.py -v
# Run with coverage
pytest --cov=src/mp4svg tests/
Advanced Usage
Custom Conversion Settings
# ASCII85 with custom settings
converter = ASCII85SVGConverter()
converter.thumbnail_quality = 85
converter.max_size_mb = 50
# Polyglot with custom boundary
polyglot = PolyglotSVGConverter()
polyglot.chunk_size = 8192
# Vector with animation settings
vector = SVGVectorFrameConverter()
vector.frame_duration = 0.1
vector.edge_threshold = 100
# QR with error correction
qr = QRCodeSVGConverter(
chunk_size=500,
error_correction='H', # High error correction
border=2
)
Batch Processing
import os
from mp4svg import ASCII85SVGConverter
converter = ASCII85SVGConverter()
# Convert all MP4 files in directory
input_dir = '/path/to/videos/'
output_dir = '/path/to/svg_output/'
for filename in os.listdir(input_dir):
if filename.endswith('.mp4'):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename.replace('.mp4', '.svg'))
try:
converter.convert(input_path, output_path)
print(f"Converted: {filename}")
except Exception as e:
print(f"Failed to convert {filename}: {e}")
Validation Pipeline
from mp4svg.validators import SVGValidator, IntegrityValidator
# Complete validation pipeline
def validate_conversion(original_mp4, generated_svg):
# Structure validation
svg_validator = SVGValidator()
svg_result = svg_validator.validate_svg_file(generated_svg)
if not svg_result['is_well_formed']:
print("SVG is not well-formed XML")
return False
# Integrity validation
integrity_validator = IntegrityValidator()
integrity_result = integrity_validator.validate_integrity(
generated_svg, original_mp4
)
if not integrity_result['data_integrity_valid']:
print("Data integrity check failed")
return False
print(f"Validation successful: {svg_result['detected_format']} format")
return True
Troubleshooting
Common Issues
-
XML Parsing Errors
- Ensure input MP4 is not corrupted
- Check file size limits (default: 100MB)
- Validate with
SVGValidator
-
JavaScript Not Working in Browser
- Check browser console for errors
- Ensure SVG is served with correct MIME type
- Modern browser required for ES6 features
-
Extraction Failures
- Verify SVG format detection
- Check embedded data integrity
- Use validation utilities
-
Large File Performance
- Use chunked processing for very large videos
- Consider polyglot method for better memory usage
- Set appropriate
max_size_mblimits
Debug Mode
import logging
logging.basicConfig(level=logging.DEBUG)
from mp4svg import ASCII85SVGConverter
converter = ASCII85SVGConverter()
# Detailed logging will be displayed
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
Apache 2.0 License - see LICENSE file for details.
Changelog
v1.0.0
- Initial release with ASCII85, Polyglot, Vector, QR Code converters
- Interactive SVG with JavaScript playback
- Comprehensive validation utilities
- CLI interface
- Full test coverage
Metadata
Release files for mp4svg 1.0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mp4svg-1.0.10.tar.gz | 55.6 kB | Without provenance | Details for mp4svg-1.0.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mp4svg-1.0.10-py3-none-any.whl | Python 3 | none | any | Details for mp4svg-1.0.10-py3-none-any.whl |
Total release size: 119.7 kB
Release files / mp4svg-1.0.10.tar.gz
| Download URL | mp4svg-1.0.10.tar.gz |
|---|---|
| Size | 55.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
967eedc23ce3b3faa93b680253fafc4c00f27bff3341b2a61a5d9e572d1a4952
|
|
BLAKE2b-256 checksum How to use checksums |
c6fe62362439c9ed40962751b3f871b4389b9991e40b164e7c10e6223356d1ea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.11.12 Linux/6.15.9-201.fc42.x86_64
|
Release files / mp4svg-1.0.10-py3-none-any.whl
| Download URL | mp4svg-1.0.10-py3-none-any.whl |
|---|---|
| Size | 64.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a9e5b9aa156cf6ad2599d2d014bb37dabf0eb23d282e875b2825e2f7c5fdfc74
|
|
BLAKE2b-256 checksum How to use checksums |
c74588f379d14d22fbb01fa3633f6ded87c897304c89099aae4c7a99549bc8e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.11.12 Linux/6.15.9-201.fc42.x86_64
|
Security for mp4svg 1.0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.