播放多个视频的随机部分,每 5 分钟更改一次

Bri*_*ian 10 video mac script macos

我正在为这个周末的假日派对设置 A/V,但我有一个想法,但我不知道如何实施。

我有一堆圣诞电影(圣诞假期、圣诞故事、美好生活等),我想在派对上连续播放。设置是连接到投影仪的 MacBook。我希望这是一个背景氛围的东西,所以它会安静地运行。

由于所有的电影都在 1 小时以上的范围内,而不是从头到尾播放它们,我想随机连续地显示小样本。理想情况下,它的工作方式是每五分钟从播放列表/目录中的随机电影中选择一个随机的 5 分钟样本并显示它。一旦该剪辑结束,它应该选择另一个剪辑并对所选剪辑进行某种淡入淡出/擦除。

我不确定从哪里开始,从我应该使用的播放器开始。VLC 可以做这样的事情吗?播放器?如果播放器具有广泛的脚本语言(支持 rand()、视频长度发现、随机视频访问)。如果是这样,我可能能够进行 RTFM 并使其正常工作;只是我没有时间从死胡同中回溯,所以我想从正确的轨道上开始。有什么建议?

Bri*_*ian 13

在没有及时得到答案(应该在一周前发布)之后,我最终潜入了自动化 VLC。我发现这个博客张贴有关使用Unix套接字控制VLC的宝石。它的要点是您可以通过命令行语法向 VLC 发送命令:

echo [VLC Command] | nc -U /Users/vlc.sock
Run Code Online (Sandbox Code Playgroud)

其中[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
Run Code Online (Sandbox Code Playgroud)

哦,顺便提一下,我们在投影仪上运行了这个,这是派对的热门话题。每个人都喜欢摆弄秒值并选择要添加的新视频。

编辑:我删除了打开 VLC 的行,因为存在计时问题,当脚本开始将文件添加到播放列表时,VLC 只会加载一半。现在我只是手动打开 VLC 并在启动脚本之前等待它完成加载。