MhmD Yo
MhmD Yo

Reputation: 11

Applying audio to video background using ffmpeg_kit_flutter package not working in flutter

I am trying to merge video and audio and downloading the video in a flutter app using ffmpeg_kit_flutter package, it works fine with the videos that recorded by screen recorder app which records the screen of my phone, but it does not works with the videos that recorded by my phone camera or any video else and I do not know where the problem is. Here's my code:

Future<void> mergeRecordWithIntro({required String videoPath, required String audiPath, }) 
async 
  {
 final random = Random();
 final directory = await getTemporaryDirectory();

 String outputVideoPath = '${directory.path}/${random.nextInt(10000)}_merged_video.mp4';
 String ffmpegCommand = '-i $videoPath -i $audiPath -c:v copy -c:a aac $outputVideoPath';

 await FFmpegKit.execute(ffmpegCommand)
    .then((value) async {
  await Gal.putVideo(outputVideoPath);
  emit(MergeVideoAudioSuccessState());
  }).catchError((error){
  emit(MergeVideoAudioFailureState());
  });
}

Upvotes: 0

Views: 148

Answers (1)

Newbs play 45
Newbs play 45

Reputation: 11

Save the audio, but make sure to remove the special character empty spaces before saving. Use this regex:

String sanitizeFileName(String fileName) {
      // Remove special characters and replace spaces with underscores
      return fileName
          .replaceAll(RegExp(r'[^\w\s.]'), '') // Remove special characters
          .replaceAll(RegExp(r'\s+'), '_')     // Replace spaces with underscores
          .replaceAll(RegExp(r'\.+'), '.')     // Ensure only single dots
          .toLowerCase();                      // Convert to lowercase
    }

use command like this '-i "$outputPath" -i "$audioPath" -map 0:v -map 1:a -c:v copy -c:a aac "$finalOutputPath"';

Upvotes: 1

Related Questions