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?
If you're going to pass multiple arguments in a list, you have to separate all of them:
strFileName = "1.m4a"
strNewFileName = "1.mp3"
g_strVLCPath = "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe"
strCmd = [
g_strVLCPath,
"-I",
"dummy",
strFileName,
'--sout="#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=8000,scodec=none}:std{access=file{no-overwrite},mux=mp3,dst=' + strNewFileName + '}"',
"vlc://quit"
]
# Run VLC
objSubProcess.run(strCmd)
On Windows, shell=True is always implied because of how cmd.exe works. That means that python will assemble your arguments into a single command line and escape or quote things like spaces. This is critical towards understanding why both of your approaches did not work. If you want to pass in a single string you can do that on Windows directly, though I'd still recommend adding shell=True to make your intention clear:
strFileName = "1.m4a"
strNewFileName = "1.mp3"
g_strVLCPath = "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe"
strCmd = f'{g_strVLCPath} -I dummy {strFileName} --sout="#transcode{{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=8000,scodec=none}}:std{{access=file{{no-overwrite}},mux=mp3,dst={strNewFileName}}} vlc://quit'
# Run VLC
objSubProcess.run(strCmd, shell=True)
In your first attempt, you have a single list element containing "-I dummy 1.m4a". When you pass that to the shell, it's going to get turned into that string including the quotes, because you specified it as a single argument containing spaces, instead of three separate arguments.
In your second attempt, you concatenated the arguments more-or-less correctly, but didn't add the program name to the string. Since you're passing in a list, python will surround everything with double quotes and escape all the quotes inside.
In first use second argument of subprocess.run() method for passing arguments for vlc Subprocess management.
Two arguments of subprocess.run() method is usefull:
If capture_output is true, stdout and stderr will be captured.
Usefull for troubleshooting.
The timeout argument. If the timeout
expires, the child process will be killed and waited for.
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)
Related
Hi Guys im trying to excute a cmd line from VBA but it keeps giveing me errors like "c:\program" is not reconised. ive been stuck here for hours adding Chr(34) and other things but still no luck.
here is my code
Path = """C:\Users\Phill\Desktop\Mortgage Illustration 211206142739.pdf"""
scommand = """C:\Program Files (x86)\A-PDF Data Extractor\PRCMD.exe""" & _
Path
Shell "cmd.exe /k " & scommand, vbNormalFocus
Maybe something like this:
Dim exe, pth
exe = "C:\Program Files (x86)\A-PDF Data Extractor\PRCMD.exe"
pth = "C:\Users\Phill\Desktop\Mortgage Illustration 211206142739.pdf"
Shell "cmd.exe /k """"" & exe & """ """ & pth & """""", vbNormalFocus
EDIT: fixed - the whole command (exe plus path) also needs to be quoted...
I am trying to call a python script from another using subprocess.Popen(). It works perfectly when I debug the program line-by-line, however when I run the program normally I get the following error:
C:\Python38\python.exe: can't open file '"<filepath>\thumbs.py"': [Errno 22] Invalid argument
I'm stumped as to what the issue is as it works without fault when the program is being debugged line-by-line so not sure what changes exactly when the program is run normally.
I am trying to pass a set of arguments into the subprocesses, and am then parsing these using argparse in the child process.
This is how I am calling the process:
cmd = ['python', "\"" + pythonPath + "thumbs.py\"", '-d', "\"" + outputdb + "\"", '-c', "\"" + path + cache + "\""]
subprocess.Popen(cmd).wait()
And here is how I am parsing the arguments in the child process:
if __name__ == '__main__':
global session
args = None
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--database", action="store", dest="dbname", help="Database to output results to", required=True)
parser.add_argument("-c", "--cache", action="store", dest="cachefile", help="Cache file to scrape.", required=True)
while args is None:
args = parser.parse_args()
Can anyone spot what I'm missing?
I've managed to fix this issue by creating my command and arguments as a string first and then using shlex.Split() to split it into an arguments list.
cmdStr = "python \"" + pythonPath + "thumbs.py\" -d \"" + outputdb + "\" -c \"" + path + cache + "\""
cmd = shlex.split(cmdStr)
subprocess.Popen(cmd).wait()
Further info here: https://docs.python.org/3.4/library/subprocess.html#popen-constructor
I wrote a script in Matlab on windows system. Now I changed to linux system and I tried to use my script on linux in matlab also. But it does not work.
I got a problem with the data import part of my script:
data = {};
for i = 1:numel(filelist)
filename = filelist{i};
filename = [selpath '\' filename];
delimiter = ',';
startRow = 2;
formatSpec = '%*q%*q%*q%q%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID);
tmp_data = str2double(dataArray{1});
data{i} = tmp_data;
end
If I run my script I got the following error from matlab:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in justus_tem (line 21)
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError',
false, 'EndOfLine', '\r\n');
When I run the same script on windows I do not get the Error. In linux system the fileID is always -1
Has somebody a tip or knows what I do wrong? I tried different permissions for fopen but it does not work either.
Linux uses a forward slash (/) as the file separator, you have hard-coded the Windows compatible backward slash (\). Instead, consider either
Using filesep for a system-dependent file separator
filename = [selpath, filesep, filename];
Or use fullfile to build the path for you
filename = fullfile(selpath, filename);
I have a command that have the structure :
xrdcp "root://server/file?authz=ENVELOPE&Param1=Val1" local_file_path
The problem is that ENVELOPE in text that should be unquoted in command line
and it contains a lot of new-lines
I cannot use repr as it will replace new-line with \n
Moreover subprocess seems to automatically use repr on the items from the list arguments
In bash this command is usually run with
xrdcp "root://server/file?authz=$(<ENVELOPE)&Param1=Val1" local_file
So, is there a way to run a command while keeping the new lines in the arguments?
Thank you!
Later Edit:
my actual code is :
envelope = server['envelope']
complete_url = "\"" + server['url'] + "?" + "authz=" + "{}".format(server['envelope']) + xrdcp_args + "\""
xrd_copy_list = []
xrd_copy_list.extend(xrdcp_cmd_list)
xrd_copy_list.append(complete_url)
xrd_copy_list.append(dst_final_path_str)
xrd_job = subprocess.Popen(xrd_copy_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = xrd_job.communicate()
print(stdout)
print(stderr)
I have this part code which i don't understand the for loop part:
also what does file path holds and how it is different than all file path?
all_files_path = glob.glob("ADNI1_Screening_1.5T/ADNI/*/*/*/*/*.nii")
for file_path in all_file_path:
print(os.system("./runROBEX.sh " + file_path + " stripped/" +file_path.split("/")[-1]))