我在凌晨 3 点从树莓派上的 crontab 触发了这个脚本。这个想法是在一天的过程中拍摄延时视频,但仅限于太阳升起和日落之间。触发时的代码计算日落和日出时间,然后等待日出。然后它应该开始拍照,但由于某种原因它似乎没有在日出时触发。我认为这可能是因为“如果 home.date == 日出:”太准确了?我怎样才能让它忽略几分之一秒,甚至完全忽略几秒钟?
这是完整的代码-
import os
import time
import ephem
# find time of sun rise and sunset
sun = ephem.Sun()
home = ephem.Observer()
home.lat, home.lon = '52.8709', '-1.797' #your lat long
sun.compute(home)
sunrise, sunset = home.next_rising(sun)),
home.next_setting(sun))
daylightminutes = (sunset - sunrise) * 1440 # find howmany minutes of daylight there are
daylightminutes = (round(daylightminutes))
def dostuff() :
if home.date == sunrise:
import datetime
import sys
FRAMES = 60#daylightminutes -600 # number of images you want in timelapse video
FPS_IN = 4 # number of images per second you want in video
FPS_OUT = 4 # number of fps in finished video 24 is a good value
TIMEBETWEEN = 6 # number of seconds between pictures, 60 = 1 minute
#take the pictures needed for the time lapse video
frameCount = 0
while frameCount < FRAMES:
imageNumber = str(frameCount).zfill(7)
os.system("raspistill -rot 270 -o /home/pi/image%s.jpg"%(imageNumber))
frameCount += 1
time.sleep(TIMEBETWEEN - 6) #Takes roughly 6 seconds to take a picture
#record current time and date in variable datetime
datetime = time.strftime("%Y%m%d-%H%M")
# make the timelapse video out of the images taken
os.system("avconv -r %s -i /home/pi/image%s.jpg -r %s -vcodec libx264 -crf 20 -g 15 -vf crop=2592:1458,scale=1280:720 /home/pi/timelapse%s.mp4" %(FPS_IN,'%7d',FPS_OUT,datet$
#send the timelapse video to dropbox
from subprocess import call
photofile = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload /home/pi/timelapse%s.mp4 /home/pi/timelapse%s.mp4" %(datetime,datetime)
call ([photofile], shell=True)
#remove the timelapse video copy and all images it is made up of that are held localy on the Rpi
os.system("rm /home/pi/timelapse%s.mp4"%(datetime))
os.system("rm /home/pi/image*")
sys.exit()
while home.date <= sunrise:
dostuff()
如果您发现任何其他可能导致问题的问题,请告诉我。
干杯
史蒂夫