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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

OperationMethodUse Case
Convertconvert(input, output)Format conversion, transcoding
Compresscompress(input, output, bitrate)File size reduction
Extract AudioextractAudio(video, audio)Audio separation
Screenshotscreenshot(input, folder, options)Thumbnail generation
Probeprobe(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.