Home » Questions » Computers [ Ask a new question ]

Playback random section from multiple videos changing every 5 minutes

Playback random section from multiple videos changing every 5 minutes

I'm setting up the A/V for a holiday party this weekend and have an idea that I'm not sure how to implement.

Asked by: Guest | Views: 70
Total answers/comments: 1
Guest [Entry]

"After not getting an answer in time (should have posted this a week earlier), I ended up diving into automating VLC. I found this gem of a blog post about controlling VLC using UNIX sockets. The gist of it is that you can send commands to VLC via the command line syntax:

echo [VLC Command] | nc -U /Users/vlc.sock

where [VLC Command] is any command that VLC supports (you can find a list of commands by sending the command ""longhelp"").

I ended up writing a Python script to automatically load up a directory full of movies and then randomly choose clips to show. The script first puts all avis into a VLC playlist. It then chooses a random file from the playlist and chooses a random starting point in that video to play. The script then waits for the specified amount of time and repeats the process. Here it is, not for the faint of heart:

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]
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])
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

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"")):
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"":
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
## Stop the movie
tries = 0
## Wait until the video stops playing or 50 tries, whichever comes first
while tries < 50 and RunVLCCommand(""is_playing"").strip() == ""1"":

Oh and as a side note, we had this running on a projector and it was the hit of the party. Everyone loved messing around with the seconds values and picking new videos to add.

EDIT: I removed the line that opens VLC because there were timing issues where VLC would only be half loaded when the script started adding files to the playlist. Now I just manually open VLC and wait for it to finish loading before starting the script."