Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
timestamp not updating
#1
I've got a script running from startup on my raspberry pi using
import time
and
timestamp = time.strftime("%Y-%m-%d_%H:%M:%S")
but when I grab a video and timestamps the filename it uses the same timestamp over and over. If I run it manually it puts the correct timestamp, so I think it grabs it once when the script is started but not each time it needs to timestamp, if you get what I mean??

so basically, how can I have it grab the correct timestamp instead of just once when the script is run at startup?

this is my full code:

#!/usr/bin/python
# 360 rotation and video
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor
import atexit
import RPi.GPIO as GPIO
from signal import pause
import picamera
import time
from time import sleep
from subprocess import call
import os

# Setup GPIO Pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#setup camera
camera = picamera.PiCamera()
#camera.framerate = 25
#camera.resolution = (1280, 720)

#Grab the current date & time
timestamp = time.strftime("%Y-%m-%d_%H:%M:%S")

# Setup Motor
# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT(addr = 0x60)

# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
        mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
        mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
        mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
        mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)
 
atexit.register(turnOffMotors)

myStepper = mh.getStepper(200, 1)       # 200 steps/rev, motor port #1
myStepper.setSpeed(10)                  # 10 RPM

while True:
    input_state = GPIO.input(18)
# filename = os.path.join(destination, dt.datetime.now().strftime('TFF_%Y-%m-%d_%H.%M.%S.h264 -t 10000 -w 1296 -h 730 -fps 25 > /dev/null'))
    if input_state == False:
        print('Button Pressed')
        sleep(.5)
        print("Camera recording for 28 seconds")
    # Setup the camera
    #with picamera.PiCamera() as camera:
    # Start recording
        camera.start_recording("/home/pi/Videos/360Video.h264" )
        sleep(2)
        print("turning motor 1 revolution, approx 26 seconds")
        myStepper.step(200, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.MICROSTEP)
        print("motor stopped")
        sleep(2)
        # Stop recording
        camera.stop_recording()
        # The camera is now closed.
        print("Camera finished recording")
        sleep(.5)
        print("Converting the video to mp4.")
        # Define the command we want to execute.
        command = "MP4Box -add /home/pi/Videos/360Video.h264 /home/pi/Videos/360Video_transfer/"+timestamp+"_360Video.mp4"
        # Execute our command
        call([command], shell=True)
        # Video converted.
        print("Video converted.")
        sleep(.5)
        print("deleting h264 file")
        os.unlink("/home/pi/Videos/360Video.h264")
        sleep(.5)
        print("h264 file deleted")
Reply
#2
try this:

import time
import datetime

def get_timestamp():
    t = time.time()
    return t, datetime.date.fromtimestamp(t)


t, ts = get_timestamp()
print('t: {}, timestamp: {}'.format(t, ts))
Reply
#3
(Aug-20-2017, 09:51 PM)Larz60+ Wrote: try this:

import time
import datetime

def get_timestamp():
    t = time.time()
    return t, datetime.date.fromtimestamp(t)


t, ts = get_timestamp()
print('t: {}, timestamp: {}'.format(t, ts))

No that didnt work, or i did it wrong being a newbie and all..hahaha...
i kept this bit in too, is that right?
#Grab the current date & time
timestamp = time.strftime("%Y-%m-%d_%H:%M:%S")
when mp4box converts to mp4 and adds the timestamp its just overwriting the original as i have it down to the seconds on the timestamp
Reply
#4
I think i have it. oddly just a matter of placement.

at the top i am defining the timestamp, so its called when the script starts. if its placed in the 'While true loop' so, when it's called from the button press it grabs the new time from each call rather than at the top of the script

cheers :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in timestamp Led_Zeppelin 3 3,197 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,003 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  Timestamp is undefined ErnestTBass 7 7,922 Feb-16-2019, 08:27 PM
Last Post: snippsat
  matplotlib timestamp zero_shubh0 2 6,803 Dec-02-2016, 02:12 PM
Last Post: zero_shubh0

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020