VideoHelp Forum
+ Reply to Thread
Results 1 to 27 of 27
Thread
  1. I've been wanting to do this for a number of years, for projecting at parties, but am just not technical enough to have sorted it myself.

    I'd like to be able to point VLC at a folder, and for it to randomly play at full screen a random movie file (could be avi, mpg, vob, etc...), from a random point, for a random length of time (with a cap of, say, max 1 min and min of 5 secs) before it picks another random file to play. Ideally, as most of my video files are in subfolders in my main movie directory, it would ignore directory structure to pick the files. Also, there's often txt/nfo/jpg/png/mp3/flac files in such folders, which should be ignored, or at least not throw an error/stop the process if encountered.

    I found a post here:
    http://superuser.com/questions/81044/playback-random-section-from-multiple-videos-chan...very-5-minutes
    ...where a guy pretty much figured out something similar using a Python script - but I've no idea how to impliment such a thing? I'm on a MacBook, can it be done from Terminal and if so, how?

    Thanks for any help! Free invite to my party for anyone that cracks it.
    Last edited by Gibson's Squares; 22nd Sep 2015 at 21:46.
    Quote Quote  
  2. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    Okey. I'm testing the script on my macbook now...

    First try: VLC does not create any socket file.
    Second try: I saved the socket in /Users/Myusername/vlc.sock and now that works...now try the python script...
    Last edited by Baldrick; 23rd Sep 2015 at 03:58.
    Quote Quote  
  3. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    It works!

    Configure first VLC( I use 2.2.1) with the Remote control. And add the socks path and fake TTY, I use /Users/Yourusername/vlc.sock . (https://n0tablog.wordpress.com/2009/02/09/controlling-vlc-via-rc-remote-control-interf...o-programming/ step 2 and step 3)


    Download my vlc.py.zip . Extract it.

    Edit it with some basic texteditor...I don't think textedit will work as it only can save complex crappy formats. I hate MAC OS X ... .

    Replace the socketpath
    Code:
    SocketLocation = "/Users/Yourusername/vlc.sock"
    and the moviesdir folder for your movie folder,
    Code:
    MoviesDir = "/Volumes/movies"
    and what type of files
    Code:
     if(MovieFile.endswith(".mp4") or MovieFile.endswith(".mkv") or MovieFile.endswith(".avi") or MovieFile.endswith(".ts")):
    Save the file in /Users/Yourusername/vlc.py



    Start VLC. Do not open any video files.



    Start a Terminal and type
    Code:
    python /Users/Yourusername/vlc.py


    But I don't think it will work with subfolders. I will try that later.
    Image Attached Files
    Quote Quote  
  4. Hey - thanks for the instructions.

    I also got it to work.

    Thanks!
    Quote Quote  
  5. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    Does it work with subfolders?
    Quote Quote  
  6. No, it's only pulling in movie files sitting in the root of my specified directory. Is there a way to get it to look in folders?
    I saw there was a command in python to search through a directory and all its subfolders, but I don't know how you'd implement it into this script:http://www.tutorialspoint.com/python/os_walk.htm

    Also, I tried to make the duration of each clip random in length, by changing Line 22 of your script to:

    IntervalInSeconds = random.randint(5,30)

    ...but it gets ignored, and just plays for 15 seconds every clip?? What have I done wrong?

    Lastly, between each clip the video window closes and then a new video window opens. Is it possible to keep the video window open when it jumps to the next title in the playlist?
    Last edited by Gibson's Squares; 23rd Sep 2015 at 20:39.
    Quote Quote  
  7. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    I think you need to add
    Code:
    import random
    at the beginning for random support.

    I will see if I can find any python subfolders examples. I have never coded in python...


    Yep. I noticed that the windows closes and open again. Maybe can you change some setting in VLC to adjust that? Check the VLC forums.

    edit: Okey random was already added....so I have no idea why random wont work. Try google python random
    Last edited by Baldrick; 24th Sep 2015 at 04:07.
    Quote Quote  
  8. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    Try replace

    Code:
    ## Loop through the directory listing and add each avi or divx file to the playlist
    for MovieFile in RawMovieFiles:
        if(MovieFile.endswith(".mp4") or MovieFile.endswith(".mkv") or MovieFile.endswith(".mov") or MovieFile.endswith(".avi")):
            MovieFiles.append(MovieFile)
            RunVLCCommand("add \"" + MoviesDir + "/" + MovieFile + "\"")
    with

    Code:
    for root, dirs, files in os.walk(MoviesDir, topdown=False):
        for MovieFile in files:
            if(MovieFile.endswith(".mp4") or MovieFile.endswith(".mkv") or MovieFile.endswith(".mov") or MovieFile.endswith(".avi")):
    	      MovieFiles.append(MovieFile)
      	      RunVLCCommand("add \"" + MoviesDir + "/" + MovieFile + "\"")
    for looping subfolders
    Last edited by Baldrick; 24th Sep 2015 at 04:32.
    Quote Quote  
  9. Oooh... it's almost there. It's finding the files in subfolders, but then because it's ignoring the directory tree to find them, it's not passing the correct path for VLC to play it. eg. I'm getting the error:

    returning: Trying to add /Users/MyMac/Movies/MyMovie.avi to playlist.
    add: returned 0 (no error)


    In the above instance the file was gotten from some path like /Users/MyMac/Movies/MySubdirectory1/MySubdirectoy2/MyMovie.avi
    MySubdirectory1 and MySubdirectoy2 are missing from the filepath given to VLC.

    Regarding the random line not working, I'm wondering does it only encounter this line once, therefore it picks a random number but then plays all files for that length, rather than loop through each time it plays a file to generate a new number?
    Quote Quote  
  10. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    Okey, maybe something like this:

    Code:
    for root, dirs, files in os.walk(MoviesDir, topdown=False):
        for name in files:
            MovieFile = os.path.join(root, name)
            if(MovieFile.endswith(".mp4") or MovieFile.endswith(".mkv") or MovieFile.endswith(".mov") or MovieFile.endswith(".avi")):
    	      MovieFiles.append(MovieFile)
      	      RunVLCCommand("add \"" + MoviesDir + "/" + MovieFile + "\"")

    And then add
    Code:
    IntervalInSeconds = random.randint(5,30)
    below this line:
    Code:
    if tries < 50:
    Quote Quote  
  11. Code:
    for root, dirs, files in os.walk(MoviesDir, topdown=False):
        for name in files:
            MovieFile = os.path.join(root, name)
            if(MovieFile.endswith(".mp4") or MovieFile.endswith(".mkv") or MovieFile.endswith(".mov") or MovieFile.endswith(".avi")):
    	      MovieFiles.append(MovieFile)
      	      RunVLCCommand("add \"" + MoviesDir + "/" + MovieFile + "\"")
    This is appending the full path to the exsting root given in line 16, so the path ends up with a duplication at the start:
    /Users/MyMac/Movies//Users/MyMac/Movies/MySubdirectory1/MySubdirectoy2/MyMovie.avi


    The new random line seemed to cause a syntax error in the line below it, this is what I get:

    StartTimeCode = random.randint(30, int(FileLength) - IntervalInSeconds);

    IndentationError: unindent does not match any outer indentation level

    (and there's a little pointer to the semi colon at the end of the line it quoted. If I remove the semi colon it has the same error and points to the closed bracket.)
    Quote Quote  
  12. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    Remove the MoviesDir in RunVLCCommand
    Code:
    for root, dirs, files in os.walk(MoviesDir, topdown=False):
        for name in files:
            MovieFile = os.path.join(root, name)
            if(MovieFile.endswith(".mp4") or MovieFile.endswith(".mkv") or MovieFile.endswith(".mov") or MovieFile.endswith(".avi")):
    	      MovieFiles.append(MovieFile)
      	      RunVLCCommand("add \"" + MovieFile + "\"")

    IndentationError means that you must put the code exactly correct using tabs(not spaces). It should be at same position as StarttimeCode = ...

    I do not like Python...
    Quote Quote  
  13. Wow, strict!
    OK, got the indendation problem sorted, and the random length time is working great.

    I've looked for settings in VLC to keep the movie window active at all times (rather than closing and opening with each new movie), to no avail.

    However, I noticed that if I manually press Forward or Back while the script is running, it changes the movie no problem without an open/close. I wonder if, therefore, the new 'random duration' variable could be re-used to tell VLC to skip to next clip a second or so before it was due to end (eg 'at IntervalInSeconds - 2seconds go Next'), and that would stop the window from closing?
    Last edited by Gibson's Squares; 24th Sep 2015 at 07:47.
    Quote Quote  
  14. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    Instead of
    Code:
            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
    Try replace it with only
    Code:
    RunVLCCommand("next")
    and see what happens.
    Quote Quote  
  15. Woahh... think that's stopped it opening and closing! Thanks so much. So excited.

    Only thing still awry is the filepath error for the subfolders where it creates a path like this:

    /Users/MyMac/Movies//Users/MyMac/Movies/MySubdirectory1/MySubdirectoy2/MyMovie.avi


    Is it possible to make it ignore the initial root it was given at the start of the script when it's joining the root to the filename in this line:

    MovieFile = os.path.join(root, name)

    ?
    Quote Quote  
  16. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    Did you remove the MoviesDir from the RunVLCCommand? It should just look like RunVLCCommand("add \"" + MovieFile + "\"")

    It works on my mac with subfolders.
    Quote Quote  
  17. Ah yes, just removed it and it's picking up the correct filepaths now, however it's not finding all the files for some reason. Is it limited to how many folders deep it looks? It seems to be mainly .vob files missing (I added 'or MovieFile.endswith(".vob") or MovieFile.endswith(".mpg") or MovieFile.endswith(".flv")' to your list). The Vobs are usually in video_ts folders inside another folder, are they too deep I wonder?

    Also, i just left it running with all the files it did find, and it looks fab! Until one file needed its index rebuilding and paused the whole process with an error window:

    Broken or missing AVI Index
    Because this AVI file index is broken or missing, seeking will not work correctly.
    VLC won't repair your file but can temporary fix this problem by building an index in memory.
    This step might take a long time on a large file.
    What do you want to do?
    Play as is / Do not play / Build index then play
    I guess this is pushing it into another level, but is there a way to ignore files with errors? I guess it has to run a file first to find an error?
    Quote Quote  
  18. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    Can't you remove those files? Or do you have 394834893489 files so it would take years to find them all.

    Maybe it's missing files with uppercase filenames like VTS_01.VOB? I don't know if the .endswith is case sensitive.
    Quote Quote  
  19. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM
    You can try add .lower() (make the text lowercased) to .endswith and also replace all endswith with one comma separated:
    if(MovieFile.lower().endswith(".mpg",".mkv",".mp4" ,".vob",".m4v",".flv",".avi",".wmv")):
    instead of
    if(MovieFile.endswith(".mp4") or MovieFile.endswith(".mkv") or MovieFile.endswith(".mov") or MovieFile.endswith(".avi")):
    Last edited by Baldrick; 25th Sep 2015 at 03:32.
    Quote Quote  
  20. Hey, good spot - yes, it was just a case issue.

    It's now loading pretty much every file, of which yes, in over 300 folders there's almost 1000 movie files so hard to pick out the bad ones easily. I can cope though, thanks so much for sorting this - you definately get an invite to the next party!

    BTW, I tried the single commer seperated line but was informed that statement can only have max 3 things in it, not 8. I guess you hate Python even more now. ;D
    Quote Quote  
  21. I'm a MEGA Super Moderator Baldrick's Avatar
    Join Date
    Aug 2000
    Location
    Sweden
    Search Comp PM


    Can you post your final python code? It might help others who google this. I think it's best to attach it as a zip file.

    And this should work in windows also if you just install python.
    Quote Quote  
  22. Hey... here's the final python script that we ended up with.

    It's still got a few quirks which seem to happen inconsistantly, so it's hard for me to replicate them to figure out why they're happening.
    I'll list them just in case they're obvious to anyone:

    1. While the script is first populating the playlist, VLC attempts to play files but only does for around a second each time, and not in fullscreen. I have about 300 subfolders with around 1000 movie files to collate, and it takes my machine around 15s to complete the playlist. Maybe the script needs to wait until the playlist is complete, I'm not sure?
    2. (Possibly the glitch above then causes the following as the behavouir looks the same) Between each movie VLC plays a 1 second clip, sometimes it does it twice. I can't tell if it's a different part of the current movie, of the next movie, or a different file entirely. This all happens in fullscreen however, unlike in point 1.
    3. Sometimes it ends up just playing each movie from the beginning.
    4. Sometimes it seems to lose the duration value, and end up playing each movie in its entirety.
    5. If VLC encounters an error that generates a dialogue box (I often got 'file not indexed properly, would you like to fix it?') it will just pause the whole process until you click a button to dismiss the dialogue box.

    I'm totally happy to work around these, as I've been after a script like this for years, and I'm thrilled that someone helped me get this far. But if they're easy fixes, please let me know.
    Image Attached Files
    Last edited by Gibson's Squares; 28th Sep 2015 at 10:14.
    Quote Quote  
  23. Member
    Join Date
    Dec 2017
    Location
    Canada
    Search PM
    > And this should work in windows also if you just install python.

    Windows does not have these CLI tools like nc or pipes, nor does it have socket files. I have taken a few hours to modify the script to work via a TCP socket so it will work from Windows. To make it work, use the command line or set up a shortcut to run VLC like this:

    vlc -I rc --rc-host localhost:6969

    Warning: This will open a network connection on your computer to receive data from any computer connected to you via a network. I wouldn't worry about it though, you probably have a firewall to stop anything from the outside and the worst somebody could do with it is show you a video.

    The script is mostly the same.
    Image Attached Files
    Quote Quote  
  24. Member
    Join Date
    Dec 2017
    Location
    Canada
    Search PM
    I can address a few of the points that were raised.

    > 2. (Possibly the glitch above then causes the following as the behavouir looks the same) Between each movie VLC plays a 1 second clip, sometimes it does it twice. I can't tell if it's a different part of the current movie, of the next movie, or a different file entirely. This all happens in fullscreen however, unlike in point 1.
    > 3. Sometimes it ends up just playing each movie from the beginning.

    These are both caused by the change you made to play the "next" video before picking a new random video to play. That makes no sense. The video should be stopped each time or the seek command may be applied to a video other than the one that's about to show (#3). Another way would be to sleep briefly after giving it the command to load another random video so it will probably be loaded by the time the seek command is sent, but that won't be much better than seeing the thing close and reopen each time, as the video will appear to flicker.

    > 4. Sometimes it seems to lose the duration value, and end up playing each movie in its entirety.

    This means your python script has crashed and VLC is just playing files regularly and uninterrupted. This was happening to me due to some of the changes to the random function calls. Example: It was set so it could never start in the first 30 seconds of a video, but some of my videos were less than 30 seconds long, crashing the script.
    Quote Quote  
  25. Member
    Join Date
    Dec 2017
    Location
    Canada
    Search PM
    Stopping the video each time is necessary but not sufficient to get it working properly. Without stopping the video it can get the length of the wrong video when it queries for that, so it was causing some of the problems. Even with stopping the video each time though the seek command can still get sent before the next video is ready for it and it can be ignored. So I have an updated script which unlike the one I posted before will not crash due to file length issues, and it will (almost certainly) jump to a random point in each file. The 300 millisecond wait for that last part seems excessive and for a faster system it may be but for mine it is necessary to ensure the file is loaded and ready for the seek command.
    Image Attached Files
    Quote Quote  
  26. Convert all your videos to MPG and save the original as a backup...
    The auto catalog for golf video is play the complete clip.. Then at 3 seconds from the end play in slow motion.
    200+ clips from 2 camcorders were up and playing before the golfers finished their awards supper..

    Last edited by SpectateSwamp; 2nd Feb 2018 at 12:28. Reason: add video
    Quote Quote  
  27. Member
    Join Date
    Mar 2018
    Location
    Mexico
    Search Comp PM
    For Windows 7, I wrote this script and it's the best solution I have found:

    https://github.com/marq4/randomclipgenerator
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!