Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: Opgelost: Random video fragmenten tonen met VLC  (gelezen 1112 keer)

Offline Rutgerwlf

  • Lid
    • Rutger Wolf
Opgelost: Random video fragmenten tonen met VLC
« Gepost op: 2015/11/05, 15:37:59 »
Hay,

geen idee of dit mogelijk is, maar wie weet.
Begonnen als idee voor achtergrond video bij band optredens kwam ik dit tegen.

http://superuser.com/questions/81044/playback-random-section-from-multiple-videos-changing-every-5-minutes

De auteur heeft een python scriptje voor OSx geschreven waarmeee willekeurige fragmenten uit diverse video`s getoond kunnen worden. Leuk straks met kerst!

Ubuntu is echter (net) geen OSx, dus het werkt nog niet bij mij.
Ik heb het idee dat het probleem ligt bij de vlc socket die bij linux anders is, maar ook als ik die verander naar /tmp/vlc.sock werkt het niet.

Iemand een idee wat ik nog meer aan moet passen?

Het scriptje:

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 = "/rutger/video/lijst"

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

Met mijn wijzigingen:
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()

SOCK = "/tmp/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 = "/media/rutger/Documenten/video"

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

## 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
« Laatst bewerkt op: 2015/11/07, 12:26:47 door Rutgerwlf »

Re: 'Vertalen' OSx script naar Linux
« Reactie #1 Gepost op: 2015/11/06, 23:20:48 »
Er bestaan twee verschillende varianten van het programma netcat, BSD en GNU. OS X gebruikt standaard de BSD variant, terwijl Ubuntu de GNU variant gebruikt. De versie die bij Ubuntu zit ondersteunt de -U optie niet.

Om dit op te lossen, kan je het pakket netcat-openbsd installeren. Je moet dan wel nog het commando nc vervangen door nc.openbsd.
« Laatst bewerkt op: 2015/11/06, 23:22:40 door SeySayux »
I use a Unix-based system, that means I'll get laid as often as I have to reboot.
LibSylph
SeySayux.net

Offline Rutgerwlf

  • Lid
    • Rutger Wolf
Re: 'Vertalen' OSx script naar Linux
« Reactie #2 Gepost op: 2015/11/07, 12:25:00 »
Ha SeySayux,

dank voor je antwoord! Het netcat-openbsd pakket was bij mij al geinstalleerd. Ik heb het commando vervangen.

Ik moest nog een aantal dingen doen:

Vlc moet gestart worden met en correct gedefinieƫrde socket, (dezelfde als in het script). en het rc commando is vervangen door oldrc.

Als je dit werkend wil krijgen doe je dus:

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 = "/tmp/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 = "/media/rutger/Documenten/video"

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

## Sends an arbitrary command to VLC
def RunVLCCommand(cmd):
    p = subprocess.Popen("echo " + cmd + " | nc.openbsd -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
Dit opslaan als .py bestand (de movies.dir kun je uiteraard veranderen, de tijd in seconden ook)

vlc starten met vlc -I oldrc --rc-unix /tmp/vlc.sockhet scriptje starten met python scriptnaam.py
Het enige wat ik niet mooi vind is dat er steeds een nieuw vlc scherm geopend wordt.

Update:
Je kunt VLC gewoon starten en dan configureren voor afstandsbedieningtoegang. Kies extra opties, hoofdbediening en selecteer afstandsbediening. Oldrc zou nu in de regel moeten staan. Bij de extra opties kies je simuleer TYY en bij het bovenste regeltje voer je de socket locatie op. Het scriptje gewoon op de oude manier starten. De video`s laden nu netjes in het bestaande VLC scherm.
« Laatst bewerkt op: 2015/11/15, 00:31:42 door Rutgerwlf »