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);
  }

提前致谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)