Search code examples
python-3.xvlc

Python launching VLC with arguments


I am having trouble launching VLC with arguments from python script. I am using python 3.9.6 on Win10 21H1 build 1081.

Running this command works from cmd.exe which I have translated in the python code.

"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe" -I dummy 1.m4a --sout="#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=8000,scodec=none}:std{access=file{no-overwrite},mux=mp3,dst=1.mp3}" vlc://quit

I tried two methods. In method 1 I send arguments separately as below.

import subprocess as objSubProcess
strFileName = "1.m4a"
strNewFileName = "1.mp3"
g_strVLCPath = "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe"

strCmd = []
strCmd.append(g_strVLCPath)
strLine = "-I dummy " + strFileName
strCmd.append(strLine)
strLine = "--sout=\"#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=8000,scodec=none}:std{access=file{no-overwrite},mux=mp3,dst="
strLine = strLine + strNewFileName + "}\""
strCmd.append(strLine)
strCmd.append("vlc://quit")

# Run VLC
objSubProcess.run(strCmd)

This code just returns immediately. My guess is VLC launches and exits immediately.

In method 2 I combined all arguments into one as below.

import subprocess as objSubProcess
strFileName = "1.m4a"
strNewFileName = "1.mp3"
g_strVLCPath = "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe"

strCmd = []
strCmd.append(g_strVLCPath)

strLine = "-I dummy " + "\"" + strFileName + "\" "
strLine = strLine + "--sout=\"#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=8000,scodec=none}:std{access=file{no-overwrite},mux=mp3,dst="
strLine = strLine + strNewFileName + "}\"" + " vlc://quit"
strCmd.append(strLine)

# Run VLC
objSubProcess.run(strCmd)

Here VLC launches (as I can see in the task manager) but my code hangs and does not return even after a long time.

In both the cases I don't get the .mp3 that I desire.

What am I doing wrong?


Solution

  • Found the solution. I passed a single string with all arguments as a simple string instead of an array. Here is the code that worked for me.

    import subprocess as objSubProcess
    strFileName = "1.m4a"
    strNewFileName = "1.mp3"
    g_strVLCPath = "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe"
    
    strLine = "\"" + g_strVLCPath + "\" -I dummy " + strFileName
    strLine = strLine + " --sout=\"#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=8000,scodec=none}:std{access=file{no-overwrite},mux=mp3,dst="
    strLine = strLine + strNewFileName + "}\""
    strLine = strLine + " vlc://quit"
    
    # Run VLC
    objSubProcess.run(strLine)