58 lines
1.5 KiB
Dart
58 lines
1.5 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:process_run/process_run.dart';
|
|
|
|
class ConverterService {
|
|
late Directory tempDir;
|
|
late String assetName;
|
|
late String ffmpegPath;
|
|
|
|
ConverterService._();
|
|
|
|
static Future<ConverterService> init() async {
|
|
debugPrint('Initializing DLServices');
|
|
var dlService = ConverterService._();
|
|
await dlService._init();
|
|
return dlService;
|
|
}
|
|
|
|
Future<void> _init() async {
|
|
tempDir = await getTemporaryDirectory();
|
|
assetName = 'ffmpeg.exe';
|
|
await copyExecutable();
|
|
}
|
|
|
|
Future<void> copyExecutable() async {
|
|
File tempFile = File('${tempDir.path}/$assetName');
|
|
|
|
ByteData data = await rootBundle.load('assets/executable/$assetName');
|
|
await tempFile.exists().then((value) {
|
|
if (!value) {
|
|
debugPrint('Copying $assetName to ${tempFile.path}');
|
|
tempFile.writeAsBytes(
|
|
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
|
|
}
|
|
});
|
|
|
|
ffmpegPath = tempFile.path;
|
|
}
|
|
|
|
Future<Stream> convertFile(String inputPath, String outputPath) async {
|
|
debugPrint('Converting $inputPath to $outputPath');
|
|
|
|
var command = ' -q --flat-playlist -J';
|
|
|
|
var shellLinesController = ShellLinesController();
|
|
var shell = Shell(stdout: shellLinesController.sink);
|
|
await shell.run('''
|
|
$ffmpegPath $command
|
|
''').then((result) {
|
|
debugPrint('Analyse result: $result');
|
|
});
|
|
|
|
return shellLinesController.stream;
|
|
}
|
|
}
|