0

I'm trying to make a video out of two things:

  1. image
  2. audio file

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

mortalis
  • 2,060
  • 24
  • 34
  • If you refer to `$returnCode` it doesn't give much info. If the code gets until that point, ffmpeg is doing something, so it's likely it leaves some logs. You have to post all those logs so others can check it and maybe help. In this old repo https://github.com/tanersener/mobile-ffmpeg there's a configuration for log level. Check something similar in the ffmpeg kit library, which supposedly comes from the mobile-ffmpeg. – mortalis Aug 17 '23 at 18:33

0 Answers0