Random VLC random clip player

Preamble

I needed a way to play random clips of random movies in a VLC playlist, in a random order (of a random length?)

See also

Useful Links

An initial Google search provided these links:

The meat and two veg

There are only two things to do in order to get this running:

  • VLC settings
  • Run the script

Prepare VLC

All you have to do, on the VLC side of things, is check two checkboxes and enter a path:

Images taken from Controlling VLC via RC (“Remote Control”) interface using a UNIX domain socket (and no programming)

Enable Remote Control Interface
Enable Remote Control Interface

and

Enable Fake TYY and specify path to socket
Enable Fake TYY and specify path to socket

Run a quick test, by playing a video in VLC, as you would normally do so. Then in the command line

sh-3.2$ /bin/echo -n  "pause" | nc -U /Users/vlc.sock

The video should pause. Run the command again and the video will start playing again. Wow!

The script

Then run this script  (original link: File Type: zip vlc.py.zip ), pointing it to your folder of movies (you can change the default on line 15 of the script), and optionally specifying a time (default is 10 seconds, line 21 of the script)

sh-32$ python vlc.py /Users/user/Movies/myloopmovies 15

Eh voila!

Notes

As a side note, the vlc binary is found here:

/Applications/VLC.app/Contents/MacOS/VLC

Note, your paths may differ depending on where you put VLC, for me it was (shown here which the option to open the rc interface):

~/Local_Apps/VLC302.app/Contents/MacOS/VLC -I rc

My sock I placed in:

/Volumes/Home/Users/user/vlc.sock

I had to manually open VLC and set the interfaces in the UI, and specified /Users/user/vlc.sock. Then I had to close and reopen and check the settings again, and save. But you may not have to.

The script

Here is the script:

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/username/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/username/Movies"

## 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 = 10 

## 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 all subfolders adding each movie file found to the playlist
for root, dirs, files in os.walk(MoviesDir, topdown=False):
    for name in files:
        MovieFile = os.path.join(root, name)
        if(MovieFile.lower().endswith(".mp4") or MovieFile.lower().endswith(".mkv") or MovieFile.lower().endswith(".avi") or MovieFile.lower().endswith(".ts") or MovieFile.lower().endswith(".vob") or MovieFile.lower().endswith(".mpg") or MovieFile.lower().endswith(".flv")):
	      MovieFiles.append(MovieFile)
  	      RunVLCCommand("add \"" + 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 duration between 20s and 45s
        IntervalInSeconds = random.randint(20,45)
        ## 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)   
        ## Skip to the next movie after playing for the given random duration
        RunVLCCommand("next")   
        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

 

That is it.

As stated above, makes changes to:

  • line 15 for the default folder
  • line 21 for the default time

Also,

RunVLCCommand("next")

could be changed to

RunVLCCommand("stop")

 

 

.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s