Flutter record front facing camera at exact same time as playing video

Viewed 532

I've been playing around with Flutter and trying to get it so I can record the front facing camera (using the camera plugin [https://pub.dev/packages/camera]) as well as playing a video to the user (using the video_player plugin [https://pub.dev/packages/video_player]).

Next I use ffmpeg to horizontally stack the videos together. This all works fine but when I play back the final output there is a slight delay when listening to the audio. I'm calling Future.wait([cameraController.startVideoRecording(filePath), videoController.play()]); but there is a slight delay in these tasks actually starting. I don't even need them to fire at the exact same time (which I'm realising is probably impossible), instead if I knew exactly when each of the tasks begun then I can use the time difference to sync the audio using ffmpeg or similar.

I've tried adding a listener on the videoController to see when isPlaying first returns true, and also watching the output directory for when the recorded video appears on the filesystem:

      listener = () {
        if (videoController.value.isPlaying) {
          isPlaying = DateTime.now().microsecondsSinceEpoch;
          log('isPlaying '+isPlaying.toString());
        }
        videoController.removeListener(listener);
      };
      videoController.addListener(listener);

      var watcher = DirectoryWatcher('${extDir.path}/');
      watcher.events.listen((event) {
        if (event.type == ChangeType.ADD) {
          fileAdded = DateTime.now().microsecondsSinceEpoch;
          log('added '+fileAdded.toString());
        }
      });

Then likewise for checking if the camera is recording:

    var listener;
    listener = () {
      if (cameraController.value.isRecordingVideo) {
        log('isRecordingVideo '+DateTime.now().microsecondsSinceEpoch.toString());
        //cameraController.removeListener(listener);
      }
    };
    cameraController.addListener(listener);

This results in (for example) the following order and microseconds for each event:

is playing: 1606478994797247
is recording: 1606478995492889 (695642 microseconds later)
added: 1606478995839676 (346787 microseconds later)

However, when I play back the video the syncing is off by approx 0.152 seconds so doesn't marry up to the time differences reported above.

Does anyone have any idea how I could accomplish near perfect syncing when combining 2 videos? Thanks.

0 Answers
Related