Advice on improving automation: camera motion -> animated gif -> push alert

Looking for advice on how to improve my security camera to animated gif + push process. This automation works well most of the time, but due to a dependency where the camera needs to finish writing a MKV clip, i sometimes end up with an old gif vs the latest. Here’s my setup, advice appreciated.

1.) Axis camera detects motion, saves a MKV via SFTP to a network location

2.) Axis camera makes a web service call to HA to run the ffmpeg/push automation. The MKV has to be converted to GIF because pushover does not support MKV files.

3.) The following automation runs:

Convert to Animated GIF & Send via Pushover:

- alias: 'Front Door Motion'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: frontdoor_motion
  action:
  - delay: '00:00:05'
  - service: shell_command.ffmpeg_gif
  - data:
      data:
        file:
          path: /config/tmp/front_door.gif
        priority: 0
        sound: incoming
      message: 'Front Door Line Crossing'
      title: Home Assistant - Front Door    
    service: notify.pushover

I have a 5 second delay to ensure the file is copied before running ffmpeg

FFMPEG Shell Command (Referenced in Automation):

  ffmpeg_gif: '/usr/bin/ffmpeg -i `/bin/ls -dtr1 /video_clips/*.mkv | tail -1` -vf fps=15,scale=320:-1:flags=lanczos -y /config/tmp/front_door.gif'

The script runs ffmpeg -> animated gif conversion on the latest MKV file in the video_clips directory

How do I ensure that the camera finishes writing the MKV to the server before running ffmpeg - the 5 second delay is not consistently working and the last image is picked up (not the latest). I’ve considered using FTP vs SFTP to improve transfer speeds at the camera and possibly switching to a watchdog process to monitor when files are done writing to a directory…

Thanks!