ffmpeg kit flutter video compressor

3.4k views Asked by At

I am not experienced with ffmpeg. So I'm sorry if I'm asking the wrong question in the wrong place. i am trying to make a video compressor, i add the dependency exactly like : ffmpeg_kit_flutter_full_gpl: ^4.5.1 and The code I wrote is like:

compress(path) {
  FFmpegKit.executeAsync("-i $path -c:a copy -c:v libx264 -s 848x360 output.mp4", (session) async {
  final returnCode = await session.getReturnCode();
 if (ReturnCode.isSuccess(returnCode)){

     print("işlem başarılı");
  // SUCCESS

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

  print("iptal edildi");
 // CANCEL

 } else {

   print("hata oluştu");
   print(await session.getFailStackTrace());
   // ERROR

 }
});

}

and im calling this method like:

asset.originFile.then((fle) {
       var path = fle!.path;
       compress(path);
       }
);

it always returns error condition for some reason. and session.getFailStackTrace() is null.

My first question is what do you think is wrong here?

And secondly how can I detect what the error is in such cases?

thanks in advance :)

1

There are 1 answers

0
Kimse On BEST ANSWER

At the end, with advice of @kesh , i used this code for see the error;

FFmpegKitConfig.enableLogCallback((log) {
 final message = log.getMessage();
 print(message);
});

And as i figure out, if your videos name contains space or special character, FFmpeg kit gives error. So, i generate random names for input file and output file and rename them..

And if any body needs a ffmpeg video compressor, i'm leaving the code:

my depency at pubspec.yaml ;

ffmpeg_kit_flutter_full_gpl: ^4.5.1

and im generating new name like:

asset.originFile.then((fle) {
                              const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
                              Random _rnd = Random();
                              String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
                              length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
                              String newName = getRandomString(15);
                              String newInput = getRandomString(16);
                              var outputPath = fle!.path.toString().split('/');

                              outputPath.remove(outputPath[(outputPath.length)-1]);

                              var out = outputPath.join('/');
                              String output = out + "/" + newName + ".mp4";
                              String inputPath = out + "/" + newInput + ".mp4";

                              fle.rename(inputPath);
                              
                              compress(inputPath, output);
                                                                  
                                                              }

and my compress function is:

compress(path, output) {
                                   FFmpegKit.executeAsync("-y -i $path -vcodec libx264 -crf 22 $output", (Session session) async {
                                     final returnCode = await session.getReturnCode();
                                     if (ReturnCode.isSuccess(returnCode)){
                                       print("işlem başarılı");
                                       File lastVid = File(output);
                                       //SUCCESS
                                     } else if (ReturnCode.isCancel(returnCode)) {
                                            print("iptal edildi");
                                            // CANCEL

                                          } else {
                                            print("hata oluştu");
                                            FFmpegKitConfig.enableLogCallback((log) {
                                              final message = log.getMessage();
                                              print(message);
                                            });
                                            // ERROR

                                          }
                                   });
                                }