Reputation: 97
I'm trying to make a video out of two things:
I want to combine these two things and make a video of it.
Here is my code:
import 'dart:io';
import 'package:ffmpeg_kit_flutter/ffmpeg_kit.dart';
import 'package:ffmpeg_kit_flutter/return_code.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:image_picker/image_picker.dart';
import 'package:file_picker/file_picker.dart';
import 'package:path_provider/path_provider.dart';
Future<void> createVideoFromUserSelection() async {
final ImagePicker _picker = ImagePicker();
final FilePickerResult? audioResult = await FilePicker.platform.pickFiles(
type: FileType.audio,
allowMultiple: false,
);
final FilePickerResult? imageResult = await FilePicker.platform.pickFiles(
type: FileType.image,
allowMultiple: false,
);
if (audioResult != null && imageResult != null) {
final File audioFile = File(audioResult.files.single.path!);
final File imageFile = File(imageResult.files.single.path!);
final compressedImageBytes = await FlutterImageCompress.compressWithFile(
imageFile.path,
quality: 70, // Adjust the image quality as needed
);
final compressedImageFile =
File('${(await getTemporaryDirectory()).path}/temp_image.jpg');
await compressedImageFile.writeAsBytes(compressedImageBytes!.toList());
final outputDirectory = Directory(
'/storage/emulated/0/Download'); // Specify the desired output directory
final outputFileName =
'output_video.mp4'; // Specify the desired output file name
final outputFile = File('${outputDirectory.path}/$outputFileName');
if (audioResult != null && imageResult != null) {
final File audioFile = File(audioResult.files.single.path!);
final File imageFile = File(imageResult.files.single.path!);
// Verify audio file existence
if (!await audioFile.exists()) {
print("Audio file does not exist at path: ${audioFile.path}");
return;
}
// Verify image file existence
if (!await imageFile.exists()) {
print("Image file does not exist at path: ${imageFile.path}");
return;
}
}
final arguments = [
'-loop',
'1',
'-i',
compressedImageFile.path,
'-i',
audioFile.path,
'-c:v',
'libx264',
'-c:a',
'aac',
outputFile.path,
];
await FFmpegKit.executeWithArguments(arguments).then((session) async {
final output = await session.getLastReceivedStatistics();
print("FFmpeg session output:\n$output");
final returnCode = await session.getReturnCode();
print("Return code: $returnCode");
if (returnCode == ReturnCode.success) {
print("Video creation completed");
} else {
print("Video creation failed. FFmpeg exit code: $returnCode");
}
}, onError: (error) {
print("Video creation failed. Error: $error");
});
// Delete the temporary compressed image and audio files
compressedImageFile.delete();
audioFile.delete();
} else {
print("User canceled the selection.");
}
}
Each time I run this function inside my UI, it allows the user to pick image, audio as well but I get exit code 1 each time. Has anybody any solution to this problem?
I have added storage permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
Thank you
Upvotes: 0
Views: 1177
Reputation: 386
Error code 1 simply means an error occurred, and you need to check other logs to see what went wrong. I suggest search Logcat for ffmpeg
to look for other errors raised by the library
Upvotes: 0