Python Forum
Echo call to VLC bash using subprocess.Popen on Linux
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Echo call to VLC bash using subprocess.Popen on Linux
#11
Hmm this also hangs... on most forums I see the issues I am having shouldn't be happening. And I had no problems making it work on my MacOSX, could it help to install another OS? Linux Mint for example?
Reply
#12
I don't think changing the OS is the correct way to handle this issue. The problem doesn't come from a bug in the OS, but from the fact that there is something that we don't see here. Did you try to execute the command out of python, that is to say by writing in a terminal
Output:
echo play | nc -U <socket location>
Reply
#13
I can confirm this opens a VLC instance from Python, which I can control with the echo calls from the Terminal. Note that I added a 'break' line to the python script.

import subprocess
import socket
import select
 
esock, echildsock = socket.socketpair()
osock, ochildsock = socket.socketpair()
p = subprocess.Popen('vlc -I oldrc --rc-unix=/home/user/Documents/Sockets/socket1.sock', shell = True, stderr = echildsock.fileno(),
    stdout = ochildsock.fileno())

while p.poll() is None:
    r, w, x = select.select([esock, osock],[],[], 1.0)
    if not r:
        continue # timed out
    for s in r:
        print('stdout first echo call ready' if s is osock else 'stderr ready')
        data = s.recv(1024)
        print('received', data.decode('utf8'))
    break
When I run exactly the same command from Python but with: echo enqueue /home/user/Documents/DualscreenVLC/videos/video01.mp4
Terminal returns this:
stdout first echo call ready
('received', u'trying to enqueue /home/user/Documents/DualscreenVLC/videos/video01.mp4 to playlist\r\nenqueue: returned 0 (no error)\r\n')
So it seems to work.

The echo play | nc -U /home/luukschroder/Documents/Sockets/socket1.sock command after this hangs both when executed from Python and from the terminal.

Also in the activity monitor I can see multiple nc and sh, one for each call I made (vlc, echo enqueue and echo play).

Hopefully this clarifies it a bit.
Reply
#14
Quote:this hangs both when executed from Python and from the terminal.
This seems to indicate that the issue has nothing to do with python. It could be a linux specific issue concerning permissions or the correct use of the commands that you call. The best thing to do is to try to do this completely outside of python by using only commands written in a terminal. Otherwise, the python part of it is only obfuscating the issue. Once you can do it without python, you can implement it with the subprocess module.
Reply
#15
Well, it only hangs both in python and terminal when the enqueue call is made with python.

When I open vlc through subprocess, then enqueue in terminal, then do the echo play call in python it works....

Basically this works perfectly in Terminal:

vlc -I oldrc --rc-unix=/home/user/Documents/Sockets/socket1.sock
echo play | nc -U /home/user/Documents/Sockets/socket1.sock
echo enqueue /home/user/Documents/DualscreenVLC/videos/MVI_2534.MP4 | nc -U /home/user/Documents/Sockets/socket1.sock
Reply
#16
I made it work partly but it seems like I am back to the beginning. I was able to use shlex.split to call a subprocess.popen without shell and I use shell=true for the echo calls. But again the echo calls are only executed after the whole script finishes. In this case I put a time.sleep(10) so it waits for 10 seconds. Maybe I need to do Threading but as far as I know the Popen command should already run independently from the main script.

Here is the current script:
#!/usr/bin/python2.7

import subprocess
import time
import signal
import shlex
import socket
import select

esock, echildsock = socket.socketpair()
osock, ochildsock = socket.socketpair()
omp_cmd = 'vlc -I oldrc --rc-unix=/home/user/Documents/Sockets/socket1.sock'
omp_cmd2 ='echo enqueue /home/user/Documents/DualscreenVLC/videos/video01.mp4 | nc -U /home/user/Documents/Sockets/socket1.sock' 
omp_cmd3 ='echo play | nc -U /home/user/Documents/Sockets/socket1.sock' 

p = subprocess.Popen(shlex.split(omp_cmd), 
    stderr=echildsock.fileno(),
    stdout=ochildsock.fileno())
 
while p.poll() is None:
    r, w, x = select.select([esock, osock],[],[], 1.0)
    if not r:
        continue # timed out
    for s in r:
        print('stdout ready' if s is osock else 'stderr ready')
        data = s.recv(1024)
        print('received', data.decode('utf8'))
    break

time.sleep(2)

p = subprocess.Popen('echo enqueue /home/user/Documents/DualscreenVLC/videos/video01.mp4 | nc -U /home/user/Documents/Sockets/socket1.sock', shell = True, stderr = echildsock.fileno(),
    stdout = ochildsock.fileno())

while p.poll() is None:
    r, w, x = select.select([esock, osock],[],[], 1.0)
    if not r:
        continue # timed out
    for s in r:
        print('stdout first echo call ready' if s is osock else 'stderr ready')
        data = s.recv(1024)
        print('received', data.decode('utf8'))
    break

time.sleep(2)

raw_input("Press enter to start video...")


omp_cmd='echo play | nc -U /home/user/Documents/Sockets/socket1.sock'
p1 = subprocess.Popen(shlex.split(omp_cmd), 
    stderr=echildsock.fileno(),
    stdout=ochildsock.fileno())
 
while p1.poll() is None:
    r, w, x = select.select([esock, osock],[],[], 1.0)
    if not r:
        continue # timed out
    for s in r:
        print('stdout ready' if s is osock else 'stderr ready')
        data = s.recv(1024)
        print('received', data.decode('utf8'))
    break

time.sleep(10)

osock.shutdown(socket.SHUT_RDWR)
osock.close()
esock.shutdown(socket.SHUT_RDWR)
esock.close()
Reply
#17
You don't need all this code with the socketpair, etc. The way you're using it is meaningless. It's not what I meant when I wrote this trick. You need to simplify all this.
Reply
#18
Ok I figured it out. It seems like the issues were more with the VLC command I was using than with python or the echo calls. Instead of connecting to a unix socked I followed this suggestion to use a tcp socket instead. I used the '&' sign and the -q flag to close the netcat shell after each command.
I guess this is much cleaner than what I was trying to do to begin with.
Thank you very much for your help! (and patience)

p=subprocess.run('vlc -I rc --rc-host localhost:11337 video01.mp4 -d &',shell=True)
p=subprocess.run('vlc -I rc --rc-host localhost:11338 video02.mp4 -d &',shell=True)

time.sleep(10)

p=subprocess.run('echo pause | netcat -q 0 localhost 11337 &', shell=True)
p=subprocess.run('echo pause | netcat -q 0 localhost 11338 &', shell=True)

time.sleep(2)


p=subprocess.run('echo seek 1 | netcat -q 0 localhost 11337 &', shell=True)
p=subprocess.run('echo seek 1 | netcat -q 0 localhost 11338 &', shell=True)

time.sleep(2)

p=subprocess.run('echo pause | netcat -q 0 localhost 11337 &', shell=True)
p=subprocess.run('echo pause | netcat -q 0 localhost 11338 &', shell=True)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 634 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
Information subprocess.Popen() suddenly giving me grief? davecotter 3 532 Dec-13-2023, 10:49 PM
Last Post: davecotter
  I'm trying to make my Alexa/Echo dot 3 powered by ChatGPT.... mariozio 1 966 Apr-20-2023, 05:24 PM
Last Post: farshid
  Use subprocess.Popen and time.sleep chucky831 2 1,890 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  Call a bash script from within a Python programme Pedroski55 6 2,377 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  use subprocess on linux\pi wwith a "grep " command korenron 2 7,907 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  continue if 'subprocess.call' failes tester_V 11 5,011 Aug-26-2021, 12:16 AM
Last Post: tester_V
  printing out the contents aftre subprocess.call() Rakshan 3 2,701 Jul-30-2021, 08:27 AM
Last Post: DeaD_EyE
  Subprocess.Popen() not working when reading file path from csv file herwin 13 14,626 May-07-2021, 03:26 PM
Last Post: herwin
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,570 May-04-2021, 08:58 PM
Last Post: liudr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020