flutter - Flutter video_compress 然后 ffmpeg 将视频修剪到 30 秒失败,日志无穷无尽
问题描述
我正在尝试在 Flutter 中制作一个简单的应用程序。用户可以拍摄或选择视频,然后上传。但是,我想压缩视频以在 firebase 存储上进行存储,并将其修剪为仅获得前 30 秒。
我面临一个非常令人费解的问题。我可以压缩视频,但是使用生成的文件,FFmpeg 无法修剪它,并且我得到无穷无尽的日志,这导致我不得不停止应用程序并重新运行。或者,我可以修剪视频,但使用生成的文件,我无法压缩它,出现错误: Failed to open file '/data/user/0/live.roots.roots/app_flutter/TRIMMED.mp4'. (No such file or directory) PlatformException(error, java.io.IOException: Failed to instantiate extractor., null, java.lang.RuntimeException: java.io.IOException: Failed to instantiate extractor.
这是我下面的代码:
//! function that controls file compression and trimming
static Future<File> compressFile(File file) async {
print('[COMPRESSING FILE]');
String mimeStr = lookupMimeType(file.path);
var fileType = mimeStr.split('/');
if (fileType.contains("image")) {
print('[COMPRESSING FILE] - file is image');
String tempPath = (await getTemporaryDirectory()).path;
String targetPath = '$tempPath/${DateTime.now().toIso8601String()}.jpg';
return await compressImageAndGetFile(file, targetPath);
} else {
print('[COMPRESSING FILE] - file is video');
final compressedVideoFile = await compressVideoAndGetFile(file);
print('[VIDEO FILE COMPRESSED]');
return await trimVideoGetFile(compressedVideoFile);
}
}
//! function to compress video
static Future<File> compressVideoAndGetFile(File file) async {
print('[COMPRESSING VIDEO]');
var result = await VideoCompress.compressVideo(
file.absolute.path,
quality: VideoQuality.DefaultQuality,
deleteOrigin: true,
);
print('[COMPRESSED VIDEO TO]: ${result.file.path}');
return result.file;
}
//! function to trim video
static Future<File> trimVideoGetFile(File file) async {
print('[TRIMMING VIDEO]');
Directory appDocumentDir = await getApplicationDocumentsDirectory();
String rawDocumentPath = appDocumentDir.path;
String outputPath = rawDocumentPath + "/TRIMMED.mp4";
final newFile = File(outputPath);
if (await newFile.exists()) {
await newFile.delete();
}
_flutterFFmpeg
.execute(
"-ss 00:00:00 -i ${file.path} -to 00:00:30 -c copy $outputPath")
.then((rt) async {
print('[TRIMMED VIDEO RESULT] : $rt');
if (rt == -1) {
throw Exception("Something went wrong when trimming the video");
}
});
return File(outputPath);
}
先感谢您
解决方案
Video_compress 包允许您在不需要 FFmpeg 的情况下修剪持续时间。
var result = await VideoCompress.compressVideo(
file.absolute.path,
quality: VideoQuality.DefaultQuality,
deleteOrigin: true,
startTime: 0, // customize start time
duration: 30, // customize the length
);
推荐阅读
- uwp - 在 C++/WinRT 项目中使用 CoreDispatcher::RunAsync 时获取“在定义之前无法使用返回‘auto’的函数”
- php - PHP将此时间戳转换为纪元/unix时间(int)
- c - 通过调用无意返回的函数来退出 switch 语句是一种好的编码习惯吗
- mysql - 触发器检查两个表上的多个值是否相等,然后更新表
- c# - 如何在 xamarin 表单中创建具有两种布局的页面
- c++ - 在 C++ 中使用取决于函数的大小初始化数组
- sql - 将数据从一个表迁移到另一个表
- java - 信件计数应用程序
- mongodb - 使用 mongodb 和 kafka 消费者的并发更新问题
- c - 如何执行简单的 buffer_overflow 攻击?