I have recently made updates to my code, specifically in relation to the storage location of FFmpeg. In the revised code, I designate the storage path for the FFmpeg output as follows:
String outputPath = '/storage/emulated/0/Android/data/com.example.videocutter/files/MyFolder/$formattedDateTime.mp4';
This implementation functions seamlessly on a physical device running Android 10. However, during testing on an Android 11 emulator, an unexpected exception arises. Upon further investigation, I noticed a significant difference in behavior when the output is stored in external storage using the following path:
final String outputPath = '${directory.path}/download/$outputFileName';
Curiously, while this modification resolves the issue on Android 11, it introduces a similar error on Android 10. I am currently seeking clarification on the root cause of this discrepancy in behavior between the two Android versions. Your insights and assistance in diagnosing and resolving this matter would be highly valuable
here is my code
Future<void> trimVideos() async {
try {
final appDocDir = await getApplicationDocumentsDirectory();
String formattedDateTime = DateFormat('yyyyMMdd_HHmmss').format(DateTime.now());
//String outputPath = '${appDocDir.path}/$formattedDateTime.mp4';
String outputPath = '/storage/emulated/0/Android/data/com.example.videocutter/files/MyFolder/$formattedDateTime.mp4';
final String inputPath=widget.file.path;
final Duration start = _controller.startTrim;
final Duration end = _controller.endTrim;
// Build the FFmpeg command for trimming with keyframe alignment
final command = '-ss ${formatter(start)} -t ${formatter(end - start)} -noaccurate_seek -i $inputPath -codec copy -avoid_negative_ts 1 $outputPath';
// Execute FFmpeg command
await FFmpegKit.execute(command).then((session) async {
print('The command is executed: $command');
final returnCode = await session.getReturnCode();
if (ReturnCode.isSuccess(returnCode)) {
print('Command is executed successfully');
setState(() {
});
}
else if (ReturnCode.isCancel(returnCode)) {
print('Command is canceled');
} else {
print('Error $returnCode');
final logs = await session.getLogsAsString();
print('Error details: $logs');
}
});
} catch (e) {
print('Error trimming video: $e');
}
}
```
Loaded ffmpeg-kit-flutter-android-https-x86-5.1.0.
I/flutter (15789): The command is executed: -ss 00:00 -t 01:00 -noaccurate_seek -i /data/user/0/com.example.videocutter/cache/86eb14d4-041d-4c7a-a719-3394b318af56/Mercy - Badshah Feat. Lauren Gottlieb _ Official Music Video _ Latest Hit Song 2017.mp4 -codec copy -avoid_negative_ts 1 /storage/emulated/0/Android/data/com.example.videocutter/files/MyFolder/20231121_152801.mp4
I/flutter (15789): Error 1
I/flutter (15789): Error details: ffmpeg version n5.1.2 Copyright (c) 2000-2022 the FFmpeg developers
I/flutter (15789): built with Android (7155654, based on r399163b1) clang version 11.0.5 (https://android.googlesource.com/toolchain/llvm-project 87f1315dfbea7c137aa2e6d362dbb457e388158d)
I/flutter (15789): configuration: --cross-prefix=i686-linux-android- --sysroot=/files/android-sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/sysroot --prefix=/home/taner/Projects/ffmpeg-kit/prebuilt/android-x86/ffmpeg --pkg-config=/usr/bin/pkg-config --enable-version3 --arch=i686 --cpu=i686 --target-os=android --disable-neon --disable-asm --disable-inline-asm --ar=i686-linux-android-ar --cc=i686-linux-android24-clang --cxx=i686-linux-android24-clang++ --ranlib=i686-linux-android-ranlib --strip=i686-linux-android-strip --nm=i686-linux-android-nm --extra-libs='-L/home/taner/Projects/ffmpeg-kit/prebuilt/android-x86/cpu-features/lib -lndk_compat' --disable-autodetect --enable-cross-compile --enable-pic --enable-jni --enable-optimizations --enable-swscale --disable-sta
The issue you're experiencing might be due to the file system differences between the emulator and the real device. The path
'storage/emulated/0/download/new.mp4'
might not be accessible or might not represent the correct path on a real device.When working with file paths in Flutter for real devices, it's better to use platform-specific methods to get the correct path to the desired location. For example, you can use the
path_provider
package to access the device's storage directory.First you should add path provider package into you'r project.
you can refer more details by using this link.
Here's an example for how you can modify your code to use
path_provider
,try like this.