• Python help...

    From paulie420@1337:3/129 to All on Tue Sep 1 16:43:43 2020
    I am working on a Raspberry Pi Zero project. Using a 1941 Philco Predicta television, that I fully refurbished, I want to create some python code to
    play a random video with a digital touch sensor module.

    The digital touch sensory module is a bit different from a push button,
    because you can set it by some wood or small metal area and push a 'button' that doesn't appear to be there. Thats important, because I don't want to modify this antique in any way...

    Ok, so anyway... I have python randomizing the files... I have it using omxplayer to play a video... but then python/the linux box just sits there
    with the video player - not waiting for the next button press until the video is completely over.

    I thought I could use subprocess.Popen instead of os.system, but I haven't figured out if that it true - nor the correct syntax to doso.

    Anyone wanna look at my code and see if they can help?
    Again, the setup is Raspberry Pi Zero holds videos... I want the python
    script to wait for a button (digtal input device) press, play a random video and then play another random video if I press the button again. (I know, too, that I'll have to put some kill process code in there... but I figured I'd
    get to that after I figured out how to wait for next button press prior to video ending...)

    Heres my current code:
    -----
    from gpiozero import DigitalInputDevice
    from time import sleep
    import os
    import random
    import subprocess

    button = DigitalInputDevice(21)

    while True:
    # Setup Randon Videos
    videoPath = "/home/pi/Videos/"
    videoList = os.listdir(videoPath)
    random.shuffle(videoList)
    for video in videoList:
    target = os.path.join(videoPath, video)
    # Wait for button press
    print("Waiting for button press to start video.")
    button.wait_for_active()
    os.system('omxplayer --aspect-mode stretch "{}" > /dev/null'.format(target))

    #subprocess.Popen('omxplayer --acpect-mode stretch "{}" > /dev/null'.format(target))

    #The subprocess was my idea that would let python continue listening... but #didnt work YET. The os.system does play a video... just waits there before #listening again for a button press.
    -----



    |07p|15AULIE|1142|07o
    |08.........

    --- Mystic BBS v1.12 A45 2020/02/18 (Raspberry Pi/32)
    * Origin: 2o fOr beeRS bbs>>>20ForBeers.com:1337 (1337:3/129)
  • From Dr. What@1337:3/128 to paulie420 on Thu Sep 3 09:05:26 2020
    paulie420 wrote to All <=-

    # Setup Randon Videos
    videoPath = "/home/pi/Videos/"
    videoList = os.listdir(videoPath)
    random.shuffle(videoList)
    for video in videoList:
    target = os.path.join(videoPath, video)

    Not part of your question, but I don't see the reason to get the video list
    all the time, then go through the video list to get the last one to play.

    I suggest putting this code outside the While loop. Then when the button is pressed, just pick a random number and play the video at that location in
    the list of videos.

    # Wait for button press
    print("Waiting for button press to start video.")
    button.wait_for_active()
    os.system('omxplayer --aspect-mode stretch "{}" > /dev/null'.format(target))

    #subprocess.Popen('omxplayer --acpect-mode stretch "{}" > /dev/null'.format(target))

    os.system runs a command, then waits for it to complete before continuing.

    So, if you want the button to be deactivated while the video is playing, then os.system is the way to go.

    subprocess.Popen is used to execute a process, but makes it easier for you
    to communicate with the subprocess. But it also allows you to run the process in the "background" (i.e. once you run it, it returns immediately - but now you have to manage the process, poll it periodically, etc.)

    IHMO, unless you are already very familiar with Linux and subproceses, I suggest
    that you do not use this.

    but #didnt work YET. The os.system does play a video... just waits
    there before #listening again for a button press.

    Then the issue is with omxplayer. It's not exiting when it's done playing
    the video.


    ... You have two choices for dinner: Take it or Leave it.
    ___ MultiMail/Linux v0.52

    --- Mystic BBS/QWK v1.12 A45 2020/02/18 (Linux/32)
    * Origin: 1984 BBS (1337:3/128)
  • From paulie420@1337:3/129 to Dr. What on Sun Sep 6 13:48:27 2020
    Not part of your question, but I don't see the reason to get the video list all the time, then go through the video list to get the last one to play.

    I suggest putting this code outside the While loop. Then when the
    button is pressed, just pick a random number and play the video at that location in the list of videos.

    Thanks, that is good information and a suggestion that I can actually understand and implement. I get it.

    os.system runs a command, then waits for it to complete before
    continuing.

    So, if you want the button to be deactivated while the video is playing, then os.system is the way to go.

    subprocess.Popen is used to execute a process, but makes it easier for
    you to communicate with the subprocess. But it also allows you to run
    the process in the "background" (i.e. once you run it, it returns immediately - but now you have to manage the process, poll it periodically, etc.)

    IHMO, unless you are already very familiar with Linux and subproceses, I suggest
    that you do not use this.

    Well, ok... I might have to learn about subprocesses; because the subprocess.Popen sounds like the way to go. Also, sounds like by polling and whatnot I could pass further commands to omxplayer WHILE the video is
    running. (Like q, quit, fastforward, etcetc...)

    Anyway, the Popen sounds like it would fit me more - but is a higher
    knowledge function. I'm gonna have to learn sometime.. thanks for explaining
    it a BIT anyway.

    but #didnt work YET. The os.system does play a video... just waits there before #listening again for a button press.

    Then the issue is with omxplayer. It's not exiting when it's done
    playing the video.

    No, it exits when done... I just want to be able to push the button again before its done. I did figure it out, a different way which I'll try to post here shortly. :P



    Thanks, Dr What. I appreciate your post.



    |07p|15AULIE|1142|07o
    |08.........

    --- Mystic BBS v1.12 A45 2020/02/18 (Raspberry Pi/32)
    * Origin: 2o fOr beeRS bbs>>>20ForBeers.com:1337 (1337:3/129)
  • From Dr. What@1337:3/128 to paulie420 on Wed Sep 9 09:41:30 2020
    paulie420 wrote to Dr. What <=-

    Well, ok... I might have to learn about subprocesses; because the subprocess.Popen sounds like the way to go. Also, sounds like by
    polling and whatnot I could pass further commands to omxplayer WHILE
    the video is running. (Like q, quit, fastforward, etcetc...)

    Yes. Popen is more complicated, but allows you to do more. So if that fits your needs better, then it's probably worth while to spend the time to
    learn more about subprocesses.

    Subprocesses aren't hard. But it just more to learn to make your project
    work.


    ... My Body's here, but my Mind's on vacation.
    ___ MultiMail/Linux v0.52

    --- Mystic BBS/QWK v1.12 A45 2020/02/18 (Linux/32)
    * Origin: 1984 BBS (1337:3/128)