Video clip timestamp help

I have a camera that is not integrated to HA so to get motion I have the camera send a video-clip when there is motion via FTP to the HA www folder, then I use “Folder Watcher” to send me an event if I´m not home. All is working fine but I have one issue and that is that the camera always send the timestamp of the motion event so a file would be “IMG02172019133226.mp4” this makes it hard to use the video in a dynamic way like Lovelace iframe as the URL of the video change all the time.

Do anyone know a way to change the file automatic to maybe “capture.mp4” and also overwrite the old “capture.mp4” so it will only be one file in the folder?

Yes

You can create script.to watch folder for files with name “IMG*”. Qa

When found it should check for existing file capture.mp4 and rename it capture1.mp4

Once file is renamed, rename the IMG*.mp4 file as capture.mp4

Great do you have an example how to accomplish this?

I have something somewhat similar. In my case I have both a PIR motion sensor and camera integrated with HA. When the sensor detects motion, I have the camera take a snapshot. Then I invoke an external python script to update the snapshot folder. By this I mean I have it maintain a symbolic link that points to the most recent snapshot. It also keeps the latest N snapshots by removing any older ones beyond that many.

In your case you would use your existing trigger. Then you could use the same technique (possibly modified slightly for your particulars.) In any case, here is the “package” with the automation, etc.:

shell_command:
  update_snaps: python3 /home/homeassistant/.homeassistant/update_snaps.py

automation:
  - alias: Foyer Snapshot
    trigger:
      platform: state
      entity_id: sensor.foyer_burglar
    condition:
      condition: template
      value_template: >
        {{ trigger.to_state is not none and
           trigger.to_state.state|int > 0 and
           states('camera.lr_ipc') in ('streaming', 'recording') }}
    action:
      - service: camera.snapshot
        data:
          entity_id: camera.lr_ipc
          filename: >
            /home/homeassistant/.homeassistant/snapshots/lr_ipc_motion_{{
              now().strftime('%Y%m%d_%H%M%S')
            }}.jpg
      - service: shell_command.update_snaps

camera:
  - platform: local_file
    name: LR IPC Last Motion
    file_path: /home/homeassistant/.homeassistant/snapshots/lr_ipc_last_motion.jpg

And here is the external python script:

#!/usr/bin/env python3

from glob import glob
import os


MAX_SNAPS = 25
SNAPS_DIR = '/home/homeassistant/.homeassistant/snapshots'
SNAPS = [('lr_ipc_motion_*.jpg', 'lr_ipc_last_motion.jpg')]


for snap in SNAPS:
    snap_list = sorted(glob(os.path.join(SNAPS_DIR, snap[0])))
    if snap_list:
        snap_link = os.path.join(SNAPS_DIR, snap[1])
        try:
            os.remove(snap_link)
        except:
            pass
        os.symlink(snap_list[-1], snap_link)
        for f in snap_list[:-MAX_SNAPS]:
            os.remove(f)

If you think you might find this useful, and you have any questions, let me know.

3 Likes

Thanks! Will give it a try later this week or at the weekend:) I use Hassio, do you think there is any issue running this script in Hassio?

I don’t use Hassio, so I couldn’t tell you.

1 Like

I got this to work:) Thanks for you help!! Just needed to tweek it a bit, and the first time I manged to make a typo…

1 Like