我正在尝试将从 flutter 的文本到语音包“flutter_tts”生成的 .wav 音频文件转换为 mp3 文件,但每次都失败。 我编写了以下文件转换代码。我已经导入了包 ffmpeg_kit_flutter。它甚至没有显示转换失败的原因。 我在 stackoverflow 和其他网站上查找过,但找不到任何相关的解决方案。我使用 vscode 作为编辑器。我还在下面附上了颤振医生的输出。有人可以指导我吗?如果您需要更多信息,请告诉我。
List<String> command = [
'-i', '$filePath/998tts.wav',
'-c:a', 'mp3',
'$filePath/998.mp3'
];
await FFmpegKitConfig.enableLogs();
FFmpegKitConfig.enableLogCallback((log) =>print('FFmpeg log: $log'));
FFmpegSession result = await FFmpegKit.executeWithArguments(command);
dynamic resultcode = await result.getReturnCode();
dynamic resultlogs = await result.getLogsAsString();
// FFmpegKitConfig.setLogLevel(logLevel)
if(ReturnCode.isSuccess(resultcode)){
print("file saved after conversion at $filePath/998.mp3 and result : Success and logs : $resultlogs");
}
else{
print("Result : failure and logs : $resultlogs");
}
Flutter doctor output:
[√] Flutter (Channel stable, 3.19.6, on Microsoft Windows [Version 10.0.22631.3296], locale en-IN)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[!] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.9.5)
X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++"
workload, and include these components:
MSVC v142 - VS 2019 C++ x64/x86 build tools
- If there are multiple build tool versions available, install the latest
C++ CMake tools for Windows
Windows 10 SDK
[√] Android Studio (version 2023.2)
[√] VS Code (version 1.89.0)
[√] Connected device (3 available)
[√] Network resources
! Doctor found issues in 1 category.
编辑:附加错误日志:
I/flutter (25865): Loading ffmpeg-kit-flutter.
D/ffmpeg-kit-flutter(25865): FFmpegKitFlutterPlugin com.arthenica.ffmpegkit.flutter.FFmpegKitFlutterPlugin@a5d9788 started listening to events on io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler$EventSinkImplementation@4cfb5f2.
I/flutter (25865): Loaded ffmpeg-kit-flutter-android-audio-arm64-v8a-6.0.3.
I/flutter (25865): Result : failure and logs : ffmpeg version n6.0 Copyright (c) 2000-2023 the FFmpeg developers
I/flutter (25865): built with Android (7155654, based on r399163b1) clang version 11.0.5 (https://android.googlesource.com/toolchain/llvm-project 87f1315dfbea7c137aa2e6d362dbb457e388158d)
I/flutter (25865): configuration: --cross-prefix=aarch64-linux-android- --sysroot=/Users/sue/Library/Android/sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/darwin-x86_64/sysroot --prefix=/Users/sue/Projects/arthenica/ffmpeg-kit/prebuilt/android-arm64/ffmpeg --pkg-config=/opt/homebrew/bin/pkg-config --enable-version3 --arch=aarch64 --cpu=armv8-a --target-os=android --enable-neon --enable-asm --enable-inline-asm --ar=aarch64-linux-android-ar --cc=aarch64-linux-android24-clang --cxx=aarch64-linux-android24-clang++ --ranlib=aarch64-linux-android-ranlib --strip=aarch64-linux-android-strip --nm=aarch64-linux-android-nm --extra-libs='-L/Users/sue/Projects/arthenica/ffmpeg-kit/prebuilt/android-arm64/cpu-features/lib -lndk_compat' --disable-autodetect --enable-cross-compile
您可以使用 ffmpeg_kit_flutter_audio 而不是 ffmpeg_kit_flutter 将音频文件转换为 MP3。
ffmpeg_kit_flutter_audio 启用支持 MP3 的其他外部库。
https://github.com/arthenica/ffmpeg-kit?tab=readme-ov-file#9-packages
这是一个最小的代码片段。
import 'package:ffmpeg_kit_flutter_audio/ffmpeg_kit.dart';
import 'package:ffmpeg_kit_flutter_audio/return_code.dart';
Future<bool> ffmpeg({
required String inputPath,
required String outputPath,
}) async {
final session = await FFmpegKit.execute('-y -i $inputPath $outputPath');
final returnCode = await session.getReturnCode();
return ReturnCode.isSuccess(returnCode);
}
await ffmpeg(
inputPath: '/path/to/input.wav',
outputPath: '/path/to/output.mp3',
);