Python Script to run VLC media file python3-vlc

105 Views Asked by At

Trying to open mp3 file with VLC from within Python script. It opens VLC and the mp3 but no sound. VLC proves to show a Connect Refusal Error.

Have tried with sudo and has error of running as root. Unsure how to stop the error of VLC and PULSE AUDIO CONNECTION. Do I need to start vlc using different command?

thom@xeno:~$ sudo ./keyboard.py APPLYING COMMAND: Play Song >>> Worn_Out.Mp3 VLC media player 3.0.18 Vetinari (revision 3.0.13-8-g41878ff4f2) [00005641b6b7dd90] vlcpulse audio output error: PulseAudio server connection failure: Connection refused [00005641b6ac0550] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface. QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-thom'

    #!/usr/bin/python3

    #    This program is free software; you can redistribute it and/or modify
    #    it under the terms of the GNU General Public License as published by
    #    the Free Software Foundation; either version 2 of the License, or
    #    (at your option) any later version.

    #    This program is distributed in the hope that it will be useful,
    #    but WITHOUT ANY WARRANTY; without even the implied warranty of
    #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    #    GNU General Public License for more details.

    #    You should have received a copy of the GNU General Public License
    #    along with this program; if not, write to the Free Software
    #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA



    # Copyright 2023, superworm
    # Tested with Debian 13 "Trixie"
    # This is a "python.py" file used for people who want to trigger commands or programs/scripts      (or other things) with a secondary keyboard.
    # Save File As "keyboard.py" and "sudo chmod +x keyboard.py" then run file with "sudo   ./keyboard.py"

    import time 
    import vlc

    from subprocess import Popen 
    from evdev import InputDevice, categorize, ecodes

    dev = InputDevice('/dev/input/by-id/usb-413c_Dell_KB216_Wired_Keyboard-event-kbd') # You will     have to find your own secondary keyboard in "/dev/input/by-id/" By using "cd /dev/input/by-id/"   and then "ls". If you have trouble working out which keyboard is which unplug and plug back in and do "ls" each time. "/dev/input/by-id/usb-413c_Dell_KB216_Wired_Keyboard-event-kbd" should be similar to your keyboard code.
    dev.grab()                                                                         

    user = "thom" # Use your own username here.
                                                                               
    brave = f"sudo -u {user} brave-browser" # Variable for brave-browser; You can use a program of  your choice.                  
    hexchat = f"sudo -u {user} hexchat"     # And here's hexchat an IRC Client; You are dropping     out of sudo to start the program as a {user}.
    updateupgrade = "sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y" #   Update/Upgrade Command.

    thunderbird = f"sudo -u {user} thunderbird"
    reboot = "sudo reboot"

    wornout = f"sudo -u {user} vlc /home/thom/Desktop/Worn_Out.mp3"

    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
        key = categorize(event)
        if key.keystate == key.key_down:
            if key.keycode == 'KEY_ESC': # Enter your own Key Code here as 'KEY_ESC'  or 'KEY_F1'
                print("\033[31mAPPLYING COMMAND:\033[32m run >>>\033[33m sudo apt    update && sudo apt upgrade -y  && sudo apt autoremove -y\033[0m")
                Popen(updateupgrade, shell=True)
            if key.keycode == 'KEY_F1': # Copy these next three lines and paste under last line and change "key.keycode == 'KEY_(YOUR OWN KEY)'"
                print("\033[31mAPPLYING COMMAND:\033[32m open >>>\033[33m brave-browser\033[0m") # This is a print to terminal with colour coding.
                Popen(brave, shell=True) # This is where you add your command from "brave = "sudo..."" etc.
            if key.keycode == 'KEY_F2':
                print("\033[31mAPPLYING COMMAND:\033[32m open >>>\033[33m hexchat\033[0m")
                Popen(hexchat, shell=True)
            # ADD NEW KEY CODES UNDER THIS LINE AND BE MINDFUL OF TABS.
            # ---------------------------------------------------------------------------------------------------
            if key.keycode == 'KEY_F3':
                print("\033[31mAPPLYING COMMAND:\033[32m open >>>\033[33m thunderbird\033[0m")
                Popen(thunderbird, shell=True)
            if key.keycode == 'KEY_SPACE':
                print("\033[31mAPPLYING COMMAND:\033[32m >>>\033[33m REBOOT!!!\033[0m")
                Popen(reboot, shell=True)
            if key.keycode == 'KEY_1':
                print("\033[31mAPPLYING COMMAND:\033[32m Play Song >>>\033[33m Worn_Out.Mp3\033[0m")
                Popen(wornout, shell=True)
            

0

There are 0 best solutions below