我在编写python时相当新,所以请原谅我,如果我好像不知道自己在做什么。我想要实现的目标是自动视频播放器(这适用于我的公司)。上午9:30,任务调度程序将启动此python文件。这就是9:30的代码:
This script runs all day and plays a commercial using VLC media player every 30 minutes
# Notes:
# Python uses a 24 hour format, so 2:30 PM = 14:30
# All spaces in video files names must be replaced by an underscore i.e. no spaces in the file name
# ...or this will not work
# Example: "Example Video Name.wmv" would need to be changed to "Example_Video_Name.wmv"
import time
import os
import subprocess
import datetime
# start here at 9:30AM, windows task scheduler takes care of this
print "It is 9:30, playing first video"
# set our working directory
os.chdir("C:\Commercials")
# the commercial to be played is specified here
commName = "Robotow_Heavy_Duty.wmv"
# get the length in seconds of the commercial
commLength = subprocess.check_output('ffprobe -i ' + commName + ' -show_entries format=duration -v quiet -of csv="p=0"')
# start the file
p = subprocess.Popen(['C:\Program Files\VideoLAN\VLC\\vlc.exe', '-f', 'C:\Commercials\\' + commName])
# delay closing the file until it is finished playing
time.sleep(float(commLength))
# kill the VLC player
p.terminate()
print "finished playing " + commName
发生了很好的评论,视频运行,然后在它的持续时间过去后被杀死。之后,以下代码让它睡到10:
print "finished playing " + commName
# delay workflow until 10:00 AM
print "Waiting until 10:00 to run next commercial..."
for i in xrange(0, 365):
# sleep until 10:00 AM
t = datetime.datetime.today()
future = datetime.datetime(t.year, t.month, t.day, 10, 00)
if t.hour >= 10 and t.minute >= 00:
future += datetime.timedelta(days=1)
time.sleep((future-t).seconds)
然后用不同的视频名称重复开头的代码,它会一直睡到10:30。这有什么奇怪的,它会在75%的时间内起作用。它会自行运行并且一直都是好的,直到大约11:00(它不会播放11:00视频),其他时间它将一直持续到1:30等等。我正在使用的结构应该检查出来。例如,这就是我要做的就是让它睡到下午3点:
for i in xrange(0, 365):
# sleep until 3:00 PM
t = datetime.datetime.today()
future = datetime.datetime(t.year, t.month, t.day, 15, 00)
if t.hour >= 15 and t.minute >= 00:
future += datetime.timedelta(days=1)
time.sleep((future-t).seconds)
我知道这很多,我为此道歉。如果您对我如何做得更好或为什么这样做有任何意见,我将非常感激。