Merge video with audio in flutter

2.3k views Asked by At

Hello folks need some suggestion for merging video with audio in flutter. I am able to achieve it using below FFmpeg command however output video quality is bad even though size is almost equal to the original video. Is there any other plugin which lighter and easier to use than FFmpeg. If not how can I make sure FFmpeg don't affect quality of video

flutterFFmpeg
              .execute("-y -i $recordingFilePath -i $selectedSoundPath -map 0:v -map 1:a  -shortest $mergedFilePath")
2

There are 2 answers

1
Nikhil M Jain On

You just have to fiddle with various ffmpeg options to find the one that suits your use-case. For me, the following options work.

final FlutterFFmpeg _flutterFFmpeg = FlutterFFmpeg();

_flutterFFmpeg.execute("-i video.mp4 -i audio.mp4 -c copy output.mp4")
              .then((return_code) => print("Return code $return_code"));

This retains the source quality.

0
krunal Gajera On
final FlutterFFmpeg flutterFFmpeg = FlutterFFmpeg();

// inputVideoPath => video path from local storage or record from camera
// backgroundMusicPath => music path from local storage
// time => output video time
// outputVideoPath => path for save to local storage

/// Merge audio with video
await flutterFFmpeg
    .execute("""-r 15 -f mp4 -i "$inputVideoPath" -f mp3 -i "$backgroundMusicPath" -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 -t $time -y $outputVideoPath""").then(
        (return_code) => print("0 Return code for success: $return_code"));

/// Merge audio with video
await flutterFFmpeg
    .execute("""-i $inputVideoPath -i $frameImage -filter_complex \"[0:v]scale=${imageHeight}:${imageWidth}[v];[1:v]scale=${imageHeight}:${imageWidth}[ov];[v][ov]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2\" $outputVideoPath""").then(
        (return_code) => print("0 Return code for success: $return_code"));