arthenica / ffmpeg-kit

FFmpeg Kit for applications. Supports Android, Flutter, iOS, Linux, macOS, React Native and tvOS. Supersedes MobileFFmpeg, flutter_ffmpeg and react-native-ffmpeg.
https://arthenica.github.io/ffmpeg-kit
GNU Lesser General Public License v3.0
4.52k stars 603 forks source link

PlatformException(SESSION_NOT_FOUND) when executing async. #1037

Closed mp051998 closed 2 months ago

mp051998 commented 2 months ago

So I am doing a video recording with rolling footage. I use ffmpeg to compress the video. When the rolling footage interval is small (say <30s) I have no issues, but when it's greater than 1 minute, after recording 2-3 videos (of 1 mins each - if i am lucky), the remaining crash with this error. Any idea what's wrong?

This is the snippet I use to invoke the compression:

    void callbackFunctionFFMpeg(session) async {
      debugPrint("Completed ffmpeg call async inside");
      bool wasSessionSuccessFul =
          await FFMpegCalls.wasSessionSuccessful(session);
      debugPrint("Was session successful: $wasSessionSuccessFul");
      if (wasSessionSuccessFul) {
        RootIsolateToken rootIsolateToken = RootIsolateToken.instance!;
        GalleryUtils(galleryLimitArg: galleryLimit).saveFileWithIsolate(
            rootIsolateToken, session, rawVideoPath, processedVideoPath);
      }
      videosBeingProcessed--;
      _progressStreamController.add(videosBeingProcessed);

      // Also update SharedPreferences
      prefs.setInt('videosBeingProcessed', videosBeingProcessed);

      // Also notify that gallery requires an update now
      // TODO: Make it a structured dict if that better fits the usecase in the future
      _pubSubService.publishEvent(PubSubEvents.VideoSaved);
    }

      static void runFfmpegCommand(
    RunFFmpegCommandArguments arguments,
  ) async {
    log("Starting runFfmpegCommand sequence for ${arguments.filePath}",
        name: "record_video");
    debugPrint("Starting runFfmpegCommand sequence for ${arguments.filePath}");

    // Session errors are to be checked within the callBackFuntion as the session
    // object is only returned when the execution completes
    // Use the FfmpegKit.wasSessionSuccessful(session) function as the first line
    // in the callback
    // TODO: Making this await didn't help either. Figure it out.
    await FFmpegKit.executeWithArgumentsAsync(
        arguments.commandArguments, arguments.callBackFunction);
  }

These are my command arguments:

    List<String> commandArguments = [
      "-y",
      "-i",
      inputPath,
      "-c:v",
      "libx264",
      "-crf",
      "26",
      "-preset",
      "ultrafast",
      "-vf",
      filterGraphStr,
      "-c:a",
      "copy",
      outputPath
    ];

This is the error I keep getting.

E/flutter (22553): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(SESSION_NOT_FOUND, Session not found., null, null)
E/flutter (22553): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
E/flutter (22553): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
E/flutter (22553): <asynchronous suspension>
E/flutter (22553): #2      FFmpegKitFactory.mapToNullableSession (package:ffmpeg_kit_flutter_full_gpl/src/ffmpeg_kit_factory.dart:76:3)
E/flutter (22553): <asynchronous suspension>
E/flutter (22553): #3      FFmpegKitInitializer._processCompleteCallbackEvent.<anonymous closure> (package:ffmpeg_kit_flutter_full_gpl/src/ffmpeg_kit_flutter_initializer.dart:213:48)  
E/flutter (22553): <asynchronous suspension>

I've been breaking my head over this for quite a lot of time now, hope someone can help me out. Thanks!

mp051998 commented 2 months ago

ffmpeg_kit_flutter_full_gpl: ^6.0.3

This is my flutter version by the way.

mp051998 commented 2 months ago

Fixed this

My issue was that other than compressing, I was also using ffmpeg to extract images from the video and sometimes they would overlap. By keeping them one after the other, I stopped getting this error.