马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
一、配景
之前做一个音视频合成的工具类,须要将ffmpeg打包进Asset中,但是没有很好的办法运行Asset中的ffmpeg。
二、实现
思绪:获取体系暂时目次,将ffmpeg从Asset中读取,然后写入到体系暂时目次中再使用。- import 'dart:io';
- import 'package:flutter/services.dart';
- class FFmpegUtil {
- static final ffmpegExeFileName = Platform.isWindows ? 'ffmpeg.exe' : 'ffmpeg';
- static String? _ffmpegPath;
- static Future<void> init() async {
- final documentsDir = Directory.systemTemp;
- final ffmpegFile = File('${documentsDir.path}/$ffmpegExeFileName');
- final ffmpegBytes =
- await rootBundle.load('assets/ffmpeg/$ffmpegExeFileName');
- if (ffmpegFile.existsSync() &&
- ffmpegFile.lengthSync() == ffmpegBytes.buffer.lengthInBytes) {
- print('Will use existed ffmpeg file: $ffmpegFile');
- _ffmpegPath = ffmpegFile.path;
- return;
- }
- print('Will copy to ffmpeg file: $ffmpegFile');
- // 将 FFmpeg 可执行文件写入应用程序的文档目录
- await ffmpegFile.writeAsBytes(ffmpegBytes.buffer.asUint8List());
- // 设置文件权限为可执行
- if (Platform.isMacOS) {
- await Process.run('chmod', ['+x', ffmpegFile.path]);
- }
- _ffmpegPath = ffmpegFile.path;
- }
- }
复制代码 |