flutter_integrated_whb/lib/tools/VideoConverter.dart

48 lines
1.3 KiB
Dart
Raw Normal View History

2025-08-29 09:52:48 +08:00
import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:video_compress/video_compress.dart';
class VideoConverter {
/// 将视频转成 mp4 格式(如果本来就是 mp4 则直接返回原路径)
static Future<String> convertToMp4(String inputPath) async {
final ext = path.extension(inputPath).toLowerCase();
// 已经是 mp4直接返回
if (ext == '.mp4') {
return inputPath;
}
try {
print('开始转换: $inputPath');
// 压缩 + 转换格式(输出文件必然是 mp4
final MediaInfo? info = await VideoCompress.compressVideo(
inputPath,
quality: VideoQuality.DefaultQuality, // 可调: Low, Medium, High
deleteOrigin: false, // 是否删除原文件
includeAudio: true,
);
if (info == null || info.path == null) {
throw Exception('视频转换失败: $inputPath');
}
print('转换完成: ${info.path}');
return info.path!;
} catch (e) {
print('视频转换出错: $e');
rethrow;
}
}
/// 将多个视频批量转换为 mp4
static Future<List<String>> convertAllToMp4(List<String> videoPaths) async {
final results = <String>[];
for (final path in videoPaths) {
final newPath = await convertToMp4(path);
results.add(newPath);
}
return results;
}
}