An open API service indexing awesome lists of open source software.

https://github.com/sctg-development/libwebm-swift

A Swift package providing bindings for libwebm, enabling WebM file parsing and creation on iOS and macOS platforms.
https://github.com/sctg-development/libwebm-swift

Last synced: 3 months ago
JSON representation

A Swift package providing bindings for libwebm, enabling WebM file parsing and creation on iOS and macOS platforms.

Awesome Lists containing this project

README

          

# LibWebMSwift

A Swift package providing bindings for libwebm, enabling WebM file parsing and creation on iOS and macOS platforms.

## Features

- **WebM Parsing**: Parse WebM files and extract metadata, track information, and media properties
- **WebM Muxing**: Create new WebM files with video and audio tracks
- **Swift Idiomatic API**: Clean, safe Swift interfaces with proper error handling
- **Memory Safe**: Automatic memory management with ARC
- **Cross-Platform**: Supports both iOS and macOS

## Installation

Add this package to your Swift project using Swift Package Manager:

```swift
dependencies: [
.package(url: "https://github.com/sctg-development/libwebm-swift.git", from: "1.0.0")
]
```

## Usage

### Parsing WebM Files

```swift
import LibWebMSwift

do {
let parser = try WebMParser(filePath: "/path/to/file.webm")

// Parse headers
try parser.parseHeaders()

// Get basic information
let duration = try parser.getDuration()
let trackCount = try parser.getTrackCount()

// Get track information
for i in 0..= 100 { break } // Limit for example
}
}

if let audioId = audioTrackId {
var frameCount = 0
while let frameData = try sourceParser.readNextAudioFrame(trackId: audioId) {
try muxer.writeAudioFrame(
trackId: audioId,
frameData: frameData.data,
timestampNs: frameData.timestampNs
)
frameCount += 1
if frameCount >= 200 { break } // Limit for example
}
}

try muxer.finalize()
print("Successfully created WebM file with \(sourceTrackCount) tracks")

} catch let error as WebMError {
print("WebM Error: \(error)")
}
```

### Video Codecs
- VP8 (`V_VP8`)
- VP9 (`V_VP9`)
- AV1 (`V_AV1`)

### Audio Codecs
- Opus (`A_OPUS`)
- Vorbis (`A_VORBIS`)

## Error Handling

The package uses Swift's error handling system with the `WebMError` enum:

```swift
enum WebMError: Error {
case invalidFile // File doesn't exist or is not a valid WebM file
case corruptedData // File data is corrupted
case unsupportedFormat // Codec or format not supported
case ioError // File I/O error
case outOfMemory // Memory allocation failed
case invalidArgument // Invalid parameter passed to function
}
```

### Error Handling Best Practices

```swift
do {
let parser = try WebMParser(filePath: filePath)
try parser.parseHeaders()

// Handle specific errors
let trackCount = try parser.getTrackCount()
guard trackCount > 0 else {
throw WebMError.invalidFile
}

} catch WebMError.invalidFile {
print("Invalid WebM file")
} catch WebMError.corruptedData {
print("File appears to be corrupted")
} catch WebMError.unsupportedFormat {
print("Unsupported codec or format")
} catch {
print("Unexpected error: \(error)")
}
```

## Advanced Features

### Video-Only Files

```swift
let muxer = try WebMMuxer(filePath: "/path/to/video-only.webm")
let videoTrackId = try muxer.addVideoTrack(width: 1920, height: 1080, codecId: "V_VP9")

// Write video frames only
for i in 0..