Rishi Saraf
Rishi Saraf

Reputation: 1812

Merge video with audio in flutter

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")

Upvotes: 3

Views: 2619

Answers (2)

krunal Gajera
krunal Gajera

Reputation: 162

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"));

Upvotes: 0

Nikhil M Jain
Nikhil M Jain

Reputation: 41

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.

Upvotes: 0

Related Questions