在没有及时得到答案(应该在一个星期前发布)之后,我最终潜入了自动VLC。我发现这个博客张贴有关使用Unix套接字控制VLC的宝石。要点是,如果正确配置了VLC,则可以通过命令行语法向其发送命令:
echo [VLC Command] | nc -U /Users/vlc.sock
其中[VLC Command]是VLC支持的任何命令(您可以通过发送命令“ longhelp ” 找到命令列表)。
我最终写了一个Python脚本来自动加载一个充满电影的目录,然后随机选择要显示的剪辑。该脚本首先将所有avis放入VLC播放列表。然后,它从播放列表中选择一个随机文件,然后在该视频中选择一个随机的起点进行播放。然后,脚本等待指定的时间并重复该过程。在这里,不是为了胆小的人:
import subprocess
import random
import time
import os
import sys
## Just seed if you want to get the same sequence after restarting the script
## random.seed()
SocketLocation = "/Users/vlc.sock"
## You can enter a directory as a command line argument; otherwise it will use the default
if(len(sys.argv) >= 2):
MoviesDir = sys.argv[1]
else:
MoviesDir = "/Users/Movies/Xmas"
## You can enter the interval in seconds as the second command line argument as well
if(len(sys.argv) >= 3):
IntervalInSeconds = int(sys.argv[2])
else:
IntervalInSeconds = 240
## Sends an arbitrary command to VLC
def RunVLCCommand(cmd):
p = subprocess.Popen("echo " + cmd + " | nc -U " + SocketLocation, shell = True, stdout = subprocess.PIPE)
errcode = p.wait()
retval = p.stdout.read()
print "returning: " + retval
return retval
## Clear the playlist
RunVLCCommand("clear")
RawMovieFiles = os.listdir(MoviesDir)
MovieFiles = []
FileLengths = []
## Loop through the directory listing and add each avi or divx file to the playlist
for MovieFile in RawMovieFiles:
if(MovieFile.endswith(".avi") or MovieFile.endswith(".divx")):
MovieFiles.append(MovieFile)
RunVLCCommand("add \"" + MoviesDir + "/" + MovieFile + "\"")
PlayListItemNum = 0
## Loop forever
while 1==1:
## Choose a random movie from the playlist
PlayListItemNum = random.randint(1, len(MovieFiles))
RunVLCCommand("goto " + str(PlayListItemNum))
FileLength = "notadigit"
tries = 0
## Sometimes get_length doesn't work right away so retry 50 times
while tries < 50 and FileLength .strip().isdigit() == False or FileLength.strip() == "0":
tries+=1
FileLength = RunVLCCommand("get_length")
## If get_length fails 50 times in a row, just choose another movie
if tries < 50:
## Choose a random start time
StartTimeCode = random.randint(30, int(FileLength) - IntervalInSeconds);
RunVLCCommand("seek " + str(StartTimeCode))
## Turn on fullscreen
RunVLCCommand("f on")
## Wait until the interval expires
time.sleep(IntervalInSeconds)
## Stop the movie
RunVLCCommand("stop")
tries = 0
## Wait until the video stops playing or 50 tries, whichever comes first
while tries < 50 and RunVLCCommand("is_playing").strip() == "1":
time.sleep(1)
tries+=1
哦,作为补充,我们让它在投影仪上运行,这很受派对欢迎。每个人都喜欢把秒值弄乱,并选择要添加的新视频。没让我被打倒,但是差点!
编辑:我删除了打开VLC的行,因为存在计时问题,其中当脚本开始将文件添加到播放列表时,VLC只会被加载一半。现在,我只是手动打开VLC,等待它完成加载,然后再启动脚本。