TS to MP4 Converter
A Rust library that converts MPEG-2 TS (Transport Stream) files to MP4 format. Can be compiled to WebAssembly for use in web browsers.
Before You Start
This project started as a personal implementation to understand TS (Transport Stream) and MP4 formats, and for this reason, the content may be somewhat rough around the edges.
Features
- H.264 video + AAC audio full support
- Pure Rust implementation (no SharedArrayBuffer required)
- WebAssembly support
- Safe for web environments with single-threaded operation
- Zero-copy optimization
- Compatible with major players like QuickTime, VLC, ffplay
Installation
Usage
As a Rust Library
If you wanna specific version, add to your Cargo.toml:
[]
= "0.1.0"
then...
use convert_ts_to_mp4;
use fs;
As a CLI Tool
For WebAssembly
Build from source:
# Install wasm-pack
# Build
This will generate the following files in the pkg/ directory:
ts2mp4.js- JavaScript bindingsts2mp4_bg.wasm- WebAssembly binaryts2mp4.d.ts- TypeScript type definitions
Using in Web Browsers
Why Not Use SharedArrayBuffer?
This library is intentionally designed not to use SharedArrayBuffer:
Advantages
- Browser Compatibility: SharedArrayBuffer requires COOP/COEP header configuration, making it difficult to use in many hosting environments
- Security: Restricted in many browsers to mitigate Spectre vulnerabilities
- Simplicity: Can be used immediately without complex server configuration
- Single-threaded: Simple and predictable memory management
Performance Optimization Techniques
Good performance can be achieved without SharedArrayBuffer:
- Streaming Processing: Process in chunks instead of loading entire file into memory
- Web Workers: Execute in workers to prevent main thread blocking
- Asynchronous Processing: Split work for large files
// Example using Web Worker
// worker.js
import init from './pkg/ts2mp4.js';
self ;
// main.js
const worker = ;
worker ;
// Start conversion
worker.;
Supported Features
Currently Supported
- Video Codec: H.264 (AVC) Main/High Profile
- Audio Codec: AAC-LC (Stereo, 48kHz)
- Container: MP4 (ISO/IEC 14496-12 compliant)
- Metadata: Complete moov/trak/stbl structure
- Timestamps: Accurate synchronization based on PTS/DTS
- Auto-detection:
- Resolution (SPS parsing)
- Frame rate
Future Plans
- Additional audio codecs (Opus, etc.)
- Multiple audio/subtitle tracks
- Variable frame rate support
- HDR metadata
- Timestamp processing improvements
Development Guide
For detailed development and debugging instructions, see DEV_GUIDE.md.
Quick Start
# Build
# Test
# Verify
Debugging Tools
The test-scripts/ directory contains Python scripts for analyzing MP4 file structure:
test-scripts/analyze_mp4.py- Analyze box structuretest-scripts/check_all_durations.py- Verify durationstest-scripts/check_stts.py- Check Time-to-Sampletest-scripts/verify_audio_data.py- Verify audio data positions
For detailed usage, refer to DEV_GUIDE.md.
Troubleshooting
For common issues and solutions, refer to the "Troubleshooting" section in DEV_GUIDE.md.
Additional Resources
| docs | description | lang |
|---|---|---|
| TS_STRUCTURE.md | TS packet generation and processing | ko, en |
| MP4_STRUCTURE.md | MP4 generation and structure | ko, en |
| 90kHZ_MAGIC.md | Why is 90kHz used for frame synchronization? | ko, en |
License
MIT
Contributing
Issues and PRs are always welcome!