4

I'm trying to convert a file from .m4a to .mp3 using ffmpeg and I need to access to the music folder.

The path name of this folder is : C:\\Users\A B\Desktop\Music

I can't access it with subprocess.call() because only C:\\Users\A gets recognized. The white space is not processed.

Here's my python script :

import constants import os import subprocess path = 'C:\\Users\A B\Desktop\Music' def main(): files = sorted(os.listdir(path), key=lambda x: os.path.getctime(os.path.join(path, x))) if "Thumbs.db" in files: files.remove("Thumbs.db") for f in files: if f.lower()[-3:] == "m4a": process(f) def process(f): inFile = f outFile = f[:-3] + "mp3" subprocess.call('ffmpeg -i {} {} {}'.format('C:\\Users\A B\Desktop\Music', inFile, outFile)) main() 

When I run it I get an error that states :

C:\Users\A: No such file or directory

I wonder if someones knows how to put my full path name (C:\Users\A B\Desktop\Music) in subprocess.call() ?

1 Answer 1

5

Beforehand edit: spaces or not, the following command line -i <directory> <infilename> <outfilename> is not correct for ffmpeg since it expects the -i option, then input file and output file, not a directory first. So you have more than one problem here (which explains the "permission denied" message you had, because ffmpeg was trying to open a directory as a file!)

I suppose that you want to:

  • read all files from directory
  • convert them all to a file located in the same directory

In that case, you could add quotes to your both input & output absolute files like this:

subprocess.call('ffmpeg -i "{0}\{1}" "{0}\{2}"'.format('C:\\Users\A B\Desktop\Music', inFile, outFile)) 

That would work, but that's not the best thing to do: not very performant, using format when you already have all the arguments already, you may not have knowledge of other characters to escape, etc... don't reinvent the wheel.

The best way to do it is to pass the arguments in a list so subprocess module handles the quoting/escaping when necessary:

path = r'C:\Users\A B\Desktop\Music' # use raw prefix to avoid backslash escaping subprocess.call(['ffmpeg','-i',os.path.join(path,inFile), os.path.join(path,outFile)]) 

Aside: if you're the user in question, it's even better to do:

path = os.getenv("USERPROFILE"),'Desktop','Music' 

and you could even run the process in the path directory with cwd option:

subprocess.call(['ffmpeg','-i',inFile, outFile],cwd=path) 

and if you're not, be sure to run the script with elevated privileges or you won't get access to another user directory (read-protected)

Sign up to request clarification or add additional context in comments.

6 Comments

Do you know why I got C:\Users\A B\Desktop\Music: Permission denied ?
aaah that's another story: in windows you cannot visit another user profile unless you're an administator (opening an elevated shell would be the way if you have those privileges)
Strange, I am the administrator there is no other user profile
Run as administrator even if you are admin. That is an extra safety.
I opened the cmd with administrator authorization and launched my script from the command prompt. Didn't work, how do you run as an admin from PyCharm ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.