1

just extract audio from video

@override
  void initState() {
    // TODO: implement initState
    super.initState();
    getAudio();
  }

  getAudio() async {
    await FFmpegKit.execute(
            "ffmpeg -i D:/Dart and Flutter/Projects/Jmm/firebase_task/assets/videos/flutter.mp4 -q:a 0 -map a D:/Dart and Flutter/Projects/Jmm/firebase_task/assets/videos/flutter_audio.mp3")
        .then((value) async {
      var returnCode = await value.getReturnCode();
      if (ReturnCode.isSuccess(returnCode)) {
        print('succsses');
      } else {
        print('fail');
      }
    });
  }

I have no idea the how to extract an audio from video in flutter

Aqib Khan
  • 21
  • 6
  • Since you can just pass the command line to it (as the very first tutorial shows you), you can just do it like everybody else: – nvoigt Feb 07 '23 at 07:21
  • Does this answer your question? [How can I extract audio from video with ffmpeg?](https://stackoverflow.com/questions/9913032/how-can-i-extract-audio-from-video-with-ffmpeg) – nvoigt Feb 07 '23 at 07:21
  • No, I tried all the possibilities but nothing happened – Aqib Khan Feb 07 '23 at 10:30
  • What did you try, what happened? – nvoigt Feb 07 '23 at 10:31
  • I added the code snippet, please look at that – Aqib Khan Feb 07 '23 at 10:39
  • 1
    Drop the "ffmpeg" at the start, it knows it's ffmpeg. And your paths probable need to be in quotes, since they contain spaces. Can your application access your "D:\" paths? – nvoigt Feb 07 '23 at 10:47
  • I drop the "ffmpeg" and surround the path with quotes and change the path to project assets folder. But the result is same – Aqib Khan Feb 07 '23 at 10:56
  • What exactly *is* the result? For example, what is `returnCode`? And what is your latest code? – nvoigt Feb 07 '23 at 12:20

2 Answers2

1

As per the documentation of the mentioned plugin, you can extract audio by the following way.

import 'package:ffmpeg_kit_flutter/ffmpeg_kit.dart';

 FFmpegKit.execute('-i input.mp4 -q:a 0 -map a output.mp3').then((session) async {
   final returnCode = await session.getReturnCode();

   if (ReturnCode.isSuccess(returnCode)) {

     // SUCCESS

   } else if (ReturnCode.isCancel(returnCode)) {

     // CANCEL

   } else {

     // ERROR

   }
 });

or even you can do it with more simple command (in execute) like:

-i input.mp4 output.mp3

or whatever FFmpeg command you want to execute.

Tahir
  • 170
  • 10
  • Tahir i added code snippet, please look at that – Aqib Khan Feb 07 '23 at 10:40
  • seems your execution command and path have issues. Please remove the ffmpeg keyword as well. make a plain directory with single word name like "videosFolder" into your D drive and check again. – Tahir Feb 07 '23 at 10:55
1

ByteData _video = await rootBundle.load('assets/videos/flutter.mp4');
    var path = await getApplicationDocumentsDirectory();
    var sampleVideo = "${path.path}/sample_video.mp4";
    File _avFile = await File(sampleVideo).create();
    await _avFile.writeAsBytes(_video.buffer.asUint8List());
    var sampleAudio = "${path.path}/sample_audio.mp3";
    File audioFile = await _avFile.copy(sampleAudio);

I solve my problem with the above code snippet

Aqib Khan
  • 21
  • 6