Flutter Audio Trim

Viewed 507

I trim a audio. Using following code.
I find code in https://pub.dev/packages/audiocutter package.
But can't generate output file.

import 'package:path_provider/path_provider.dart';
import 'package:ffmpeg_kit_flutter/ffmpeg_kit.dart';

static Future<String> cutAudio(String path, double start, double end) async {
    
    final Directory dir = await getTemporaryDirectory();
    final outPath = "${dir.path}/output.mp3";
    var cmd = "-y -i \"$path\" -vn -ss $start -to $end -ar 16k -ac 2 -b:a 96k -acodec libmp3lame $outPath";
    log(cmd);

    FFmpegKit.executeAsync(cmd, (session) async {
      final returnCode = await session.getReturnCode();

      print("returnCode $returnCode");
    });

    return outPath;
}

Please help me how to trim audio.

1 Answers

I getting this error because the output file is already exist.

So I solved this using delete existing file.
I deleted file using following code.

try {
  await File(outPath).delete();
} catch (e) {
  print("Delete Error");
}

Following is my working code.

static Future<String> cutAudio(String path, double start, double end) async {
        
        final Directory dir = await getTemporaryDirectory();
        final outPath = "${dir.path}/output.mp3";
    
        try {
          await File(outPath).delete();
        } catch (e) {
          print("Delete Error");
        }
    
        var cmd = "-y -i \"$path\" -vn -ss $start -to $end -ar 16k -ac 2 -b:a 96k -acodec libmp3lame $outPath";
        log(cmd);
    
        FFmpegKit.executeAsync(cmd, (session) async {
          final returnCode = await session.getReturnCode();
    
          print("returnCode $returnCode");
        });
    
        return outPath;
    }
Related