90 lines
2.7 KiB
Dart
90 lines
2.7 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
class G711 {
|
|
Future<Uint8List?> decodeG711ToPCM(List<int> g711Data) async {
|
|
try {
|
|
FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg();
|
|
|
|
// Save G.711 data to a temporary file
|
|
Directory appDocDir = await getApplicationDocumentsDirectory();
|
|
String g711FilePath = '${appDocDir.path}/input.g711';
|
|
await File(g711FilePath).writeAsBytes(g711Data);
|
|
|
|
// Replace with the desired output PCM file path
|
|
String pcmFilePath = '${appDocDir.path}/output.pcm';
|
|
|
|
// Run FFmpeg command to decode G.711 to PCM
|
|
String command =
|
|
'-y -f u8 -ar 8000 -ac 1 -i $g711FilePath -f s16le -ar 8000 -ac 1 $pcmFilePath';
|
|
int result = await flutterFFmpeg.execute(command);
|
|
|
|
if (result == 0) {
|
|
print('G.711 decoding successful! PCM file saved at: $pcmFilePath');
|
|
|
|
// Read PCM data from file
|
|
Uint8List pcmBytes = await File(pcmFilePath).readAsBytes();
|
|
|
|
// Delete the temporary G.711 file
|
|
await File(g711FilePath).delete();
|
|
|
|
// Delete the temporary PCM file
|
|
await File(pcmFilePath).delete();
|
|
|
|
return pcmBytes;
|
|
} else {
|
|
print('Error during G.711 decoding: $result');
|
|
return null;
|
|
}
|
|
} catch (e) {
|
|
print('Error: $e');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<Uint8List?> encodePCMToG711(Uint8List pcmData) async {
|
|
try {
|
|
FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg();
|
|
|
|
// Save PCM data to a temporary file
|
|
Directory appDocDir = await getApplicationDocumentsDirectory();
|
|
String pcmFilePath = '${appDocDir.path}/input.pcm';
|
|
await File(pcmFilePath).writeAsBytes(pcmData);
|
|
|
|
// Replace with the desired output G.711 file path
|
|
String g711FilePath = '${appDocDir.path}/output.g711';
|
|
|
|
// Run FFmpeg command to encode PCM to G.711
|
|
String command =
|
|
'-y -f s16le -ar 8000 -ac 1 -i $pcmFilePath -f g711 -ar 8000 -ac 1 $g711FilePath';
|
|
int result = await flutterFFmpeg.execute(command);
|
|
|
|
if (result == 0) {
|
|
print(
|
|
'PCM encoding to G.711 successful! G.711 file saved at: $g711FilePath');
|
|
|
|
// Read G.711 data from file
|
|
Uint8List g711Bytes = await File(g711FilePath).readAsBytes();
|
|
|
|
// Delete the temporary PCM file
|
|
await File(pcmFilePath).delete();
|
|
|
|
// Delete the temporary G.711 file
|
|
await File(g711FilePath).delete();
|
|
|
|
return g711Bytes;
|
|
} else {
|
|
print('Error during PCM encoding to G.711: $result');
|
|
return null;
|
|
}
|
|
} catch (e) {
|
|
print('Error: $e');
|
|
return null;
|
|
}
|
|
}
|
|
}
|