Amanwhocan
Amanwhocan

Reputation: 53

Saving video file the time and date in filename

Expected Behavior

  1. Automatically run a program to record a video for a short length of time.
  2. Save the video to a unique filename within a specific directory (to avoid overwriting). Ideally, this filename would include the date and time.

Actual Behavior

  1. Success
  2. Filename is always video.h264.

I have tried all sorts of things that I have found on the net, but they only result in the file name showing part of the code. Annoyingly it worked once, but saved it to somewhere I was not expecting and I changed the code before I realized it had worked!

Full File

    # Import Libraries

    import os       #Gives Python access to Linux commands
    import time         #Proves time related commands
    import RPi.GPIO as GPIO #Gives Python access to the GPIO pins


    GPIO.setmode(GPIO.BCM)  #Set the GPIO pin naming mode
    GPIO.setwarnings(False) #Supress warnings

    # Set GPIO pins 18 as output pin
    LEDReady = 18 #Red

    GPIO.setup(LEDReady,GPIO.OUT)

    GPIO.output (LEDReady,GPIO.HIGH)

    from subprocess import call
    call(["raspivid", "-o", "video.h264", "-t", "50000n"])
    time.sleep(10) #Sleep for 10 seconds

    GPIO.output (LEDReady,GPIO.LOW)

Adding DATE=$(date +"%Y-%m-%d_%H%M") and changing video.h264 to $DATE.h264 results in a syntax error for $DATE.

Tantalizingly, I have a file called 20180308_021941.h264 which is exactly what I am after, but I cannot tell you how I managed it!

P.S. the Red LED lighting up is so that I can tell if the Raspberry Pi has fired up properly and has run the Python script.

Thank you for taking the trouble to read this.

Upvotes: 5

Views: 901

Answers (1)

Steve
Steve

Reputation: 59

Try adding this

from datetime import datetime

date = datetime.now().strftime("%Y%m%d%H:%M:%S")

Then change your call to this

videoFile = date + ".h264"
call(["raspivid", "-o", videoFile, "-t", "50000n"])

Upvotes: 2

Related Questions