switch_camera
switch_camera
is a Dart library that provides a Dart FFI interface to interact with camera and audio recording functionalities from Rust. It supports video capturing, frame streaming, and video writing, as well as audio recording and merging video with audio. I used the switch_camera library to build a desktop application named Zed Camera, and you can watch a demonstration of it on YouTube. This library is particularly useful in IoT, CCTV, and industrial contexts where multiple camera inputs and reliable audio-visual recording are essential.
Features
- List available camera devices
- Open and release camera devices
- Capture video frames (with and without flip)
- Stream video frames (with and without flip)
- Start video writer
- Write video frames (with and without flip)
- Audio recording (start, stop, pause, resume)
- Merge audio and video
Platform Support
- Linux
Installation
To install switch_camera
, use flutter pub add
:
Flutter
flutter pub add switch_camera
Dependencies
Linux - Ubuntu:
sudo apt-get update
- Getting OpenCV:
sudo apt-get install libopencv-dev clang libclang-dev
- Install JACK development files:
sudo apt-get install libjack-jackd2-dev
- Install ALSA development files:
sudo apt-get install libasound2-dev
- Install FFmpeg:
sudo apt-get install ffmpeg
Usage
Camera
import 'package:switch_camera/switch_camera.dart';
void main() {
final camera = Camera();
// List available 10 devices
final devices = camera.getDevices(10);
print('Available devices: $devices');
// Open a camera device
camera.open(0);
// Capture a frame
final frame = camera.captureFrame();
print('Captured frame: ${frame.length} bytes');
// Capture a flipped frame
final flippedFrame = camera.captureFrameFlip();
print('Captured flipped frame: ${flippedFrame.length} bytes');
// Stream frames
camera.streamFrames();
// Stream flipped frames
camera.streamFramesFlip();
// Start video writer
camera.startVideoWriter('output.mp4', 30.0, 1920, 1080);
// Write a frame
camera.writeFrame();
// Write a flipped frame
camera.writeFrameFlip();
// Release the camera
camera.release();
// Dispose the camera
camera.dispose();
}
Audio Recorder
import 'package:switch_camera/switch_camera.dart';
void main() {
final recorder = RustAudioRecorder();
// Start recording
recorder.startRecording();
// Pause recording
recorder.pauseRecording();
// Resume recording
recorder.resumeRecording();
// Stop recording
recorder.stopRecording();
}
Merge Audio and Video
import 'package:switch_camera/switch_camera.dart';
void main() {
final result = mergeCamAudioVideo('output_with_audio.mp4');
print('Merge result: $result');
}
NB: Prepare Linux apps for distribution
To build flutter applciation as release run the following command:
flutter build linux --release
After that, go to build/linux/release/bundle/ and run the application using the following command:
./projectname
"By runnung the application in the directory, a file will be automatically copied to lib folder with the following directory src/rust_native/librust_camera.so. This file is responsible for this library to work in your application."
API Reference
Camera
-
List<int> getDevices(int limit)
- Lists available camera devices up to the specified limit.
-
void open(int index)
- Opens a camera device by index.
-
void release()
- Releases the currently opened camera device.
-
Uint8List captureFrame()
- Captures a single video frame.
-
Uint8List captureFrameFlip()
- Captures a single flipped video frame.
-
Stream<Uint8List> streamFrames()
- Streams video frames.
-
Stream<Uint8List> streamFramesFlip()
- Streams flipped video frames.
-
void startVideoWriter(String filename, double fps, int width, int height)
- Starts video writing to the specified file.
-
void startAVideoWriter(double fps, int width, int height)
- Starts a video writer without specifying a filename.
-
void writeFrame()
- Writes a video frame.
-
void writeFrameFlip()
- Writes a flipped video frame.
-
void dispose()
- Disposes the camera resources.
RustAudioRecorder
void startRecording()
- Starts audio recording.
void stopRecording()
- Stops audio recording.
void pauseRecording()
- Pauses audio recording.
void resumeRecording()
- Resumes audio recording.
Merge Functions
String mergeCamAudioVideo(String outputFilePath)
- Merges the recorded camera video and audio into the specified output file.
Author
Zacchaeus Oluwole
LinkedIn: www.linkedin.com/in/zacchaeus-oluwole/
Email: zacchaeusoluwole@gmail.com
Github: github.com/Zacchaeus-Oluwole