Examples
Basic Usage
Learn MediaX fundamentals with step-by-step examples covering all core operations. Each example includes complete event handling and realistic usage patterns.
What You'll Learn
Core Operations
- • Video format conversion and transcoding
- • File size compression with bitrate control
- • Audio extraction from video files
- • Screenshot and thumbnail generation
- • Media metadata inspection
Event Handling
- • Progress tracking with percentage updates
- • Success and completion handling
- • Error handling and recovery patterns
- • Asynchronous job management
- • Real-time feedback implementation
Complete Examples
1. Video Conversion
Convert video from one format to another with progress tracking
Implementation
video-conversion-example.ts
12345678910111213141516171819202122
typescript
How It Works
convert(input, output) spawns a job wrapping an FFmpeg command
The job emits progress → percentage of completion
done → when conversion finishes
error → if FFmpeg fails
Useful when changing containers or transcoding videos
2. Video Compression
Reduce file size by compressing video with lower bitrate
Implementation
video-compression-example.ts
123456789101112131415161718192021
typescript
How It Works
compress(input, output, bitrate) reduces file size
Bitrate must be passed (e.g. "1000k" for ~1Mbps)
Jobs emit the same lifecycle events as convert
Perfect for web optimization or storage savings
3. Extracting Audio
Extract audio track from video files in various formats
Implementation
extracting-audio-example.ts
123456789101112131415161718192021
typescript
How It Works
extractAudio(videoFile, audioFile) strips audio from a video
Output format depends on extension (e.g., .mp3, .wav)
Common for podcast creation or separating lectures
Preserves original audio quality by default
4. Taking Screenshots
Capture still frames from video at specific intervals
Implementation
taking-screenshots-example.ts
12345678910111213141516171819202122232425
typescript
How It Works
screenshot(input, folder, options) captures still frames
Options: count → how many frames to capture
folder → output directory
filename → naming convention with %i placeholder
Ideal for thumbnails, previews, or dataset creation
5. Probing Media Metadata
Inspect video/audio metadata and technical information
Implementation
probing-media metadata-example.ts
1234567891011121314151617
typescript
How It Works
probe(file) calls ffprobe to fetch structured media info
Returns codec, resolution, duration, audio/video streams, etc.
Useful for validating input files before running pipelines
No progress events - returns metadata directly on completion
Best Practices
Event Handling
- Always attach error listeners to prevent crashes
- Use progress events for user feedback
- Handle done events for cleanup tasks
File Management
- Validate input files exist before processing
- Use probe() to check file compatibility
- Ensure output directories exist
Quick Reference
Operation | Method | Use Case |
---|---|---|
Convert | convert(input, output) | Format conversion, transcoding |
Compress | compress(input, output, bitrate) | File size reduction |
Extract Audio | extractAudio(video, audio) | Audio separation |
Screenshot | screenshot(input, folder, options) | Thumbnail generation |
Probe | probe(input) | Metadata inspection |
Ready for Advanced Examples?
Now that you've mastered the basics, explore pipeline workflows, error handling patterns, and complex media processing scenarios.