You are not logged in.
Hi, Ive dabbled very little in actual coding, wrote one python app that these forums really helped me make, and Ive programmed in VBScript and SQL Server, but all academic, nothing for my own uses. I need a program to convert mp3's to .wav's that can then be burned in 'Recorder' to burn the audio disc. I know there are many scripts like this floating around, but I want to do my own, problem is, im lost. I seem to have 2 main problems, getting the file names into a list or tuple, and then actually running the commands to convert using a for loop using lame or mpg321.
this bash script uses mpg321 and sox,
#!/bin/bash
# allmp3wav
for i in *.mp3; do
echo "$i"
tgt=$(echo "$i" | sed -e "s/mp3/wav/")
mpg123 -b 10000 -s -r 44100 "$i" | sox -t raw -r 44100 -s -w -c2 - /var/cdrom/"$tgt"
done
This works fine and I understand the code. Its saying for every file that ends in .mp3, run these commands, which do the coding. The only problem is, this doesnt work in python, since its not pythong Can someone give me a little bit of help? Dont give me the program, or give me to much info, links would be great.
1 - Need to Get Directory
2 - Loop through all the .mp3 files and add them to an array.
3 - create a sub directory named "wav"
mkdir ~/wav
4 - copy all the mp3's to "wav"
cp ARRAy to wav/
5 - convert all .mp3's to wav
6 - delete the copied mp3's from .wav directory
for .mp3
rm
7 - tell me how many wavs were created.
for .wav
count + 1
That is just a rough flow chart of what I need to do, and as you can see its pretty bare as of now. Also, I imagine to run mpg321 or lame commands, I need some sort of lame or mpg321 module? Ive read the python wiki for for loops and lists and tuples, but I need more info. Thanks, Justin.
Offline
I think a lot of what you need to do could be done using the os module. Like making directories, running mpg321/lame commands, and deleting files. I'm not sure if there's a better way to do things, but it works.
As for getting the file names - it's a pretty ugly way of doing it (I'm still trying to find something better) - but you could use the commands module to get the output of the bash 'locate /path/to/music/dir/* ' and just split that string output into a list.
Offline
see http://docs.python.org/lib/os-file-dir.html
look at rename, renames, mkdir, listdir, remove
you can keep track of the wav files as you go along
for converting
http://docs.python.org/lib/module-subprocess.html
specifically, Popen
you can check the return code to see if the wav was created successfully (wait() == 0)
you can also keep track this way by incrementing a counter of sorts
or ... see below
as to getting the list of mp3s
there are two options, you could run ls or whatever in a subprocess
or (prolly better) using os.listdir
to extract the mp3s
you could utilize the re module(overkill)
a simple 'string'.lower().endswith('.mp3')
or os.path.splitext http://www.python.org/doc/current/lib/m … .path.html
that should cover it
Offline
just what I wanted thanks! ill take a look at this when i get off, 2 hrs!
Offline
Hi, I am still having problems. I only have a little bit so far, i am having trouble with the for loop and how to call popen.
I have a rough idea of how to do this now. I still cant find a way to get my for loop to examine each object in the list that os.listdir generates. once i get the string , i can split the last 4 letters off it, and compare it to '.mp3' if its in fact a .mp3 file, i can execute a ctr += 1 and run the mpg123 command, using the original file name, and using everything to the left of '.mp3' as the name of the outputted wav file, and just concatinate '.wav' to it. Can someone give me a primer on the for loop and using it with lists and tuples etc? That would really help me, thanks everyone. THe other question i have is how does one properly use the 'popen' its in the subprocess module, which i can import, but do i do subprocess.popen(args = blahblah) or do i need to call it as a 'class' which i will have to learn what that actually is lol thanks for helping a noobie.
Offline
the for loop is basically the foreach in other languages
so you do,
#for each_item in iterable:
# do something with each_item
for file in os.listdir("/dir"):
name, ext = os.path.splitext(file)
if ext == ".mp3":
do_whatever()
you can think of a tuple as just an immutable list i.e, you can initialise te tuple with values,
but you cannot re-assign or add to it
so you can do
T = (1, 2, 3)
T[1] = -2 # type error(i think lol)i
T[3] = 4 # same as above, this time u are trying to add to it
del T[2] # type error again
again tuples and lists are iterable so you can iter over them in the normal way
------
Popen(make a habit of spelling it case sensatively) is not hard to use you just need to get used to it
you can supply the command as either a list of a single string
p = subprocess.Popen("ls .", stdout=subprocess.PIPE, shell=True)
p.stdout.read()
p is the Popen object, and here we use its stdout attribute, as p.stdout.read(),
the stdout attr is filelike(a pipe) so you can do whatever you would do with a file opened with open()with
in the call to Popen we set, stdout to a pipe (subprocess.PIPE) if you wanted to capture stderr or stdin in the same way,
you do the same but with stdout replaced by stderr or stdin(subprocess.PIPE remains the same)
shell=True sets the shell active so that way you more or less instruct the shell to execute the command instead,
this brings such benefits like you can issue the command without using the full path(ls instead of /bin/ls)
and you can use the shell command pipes '|', (is that even the right name for them)
i think for piping commands, there is some wizardry using communicate but that's another story
i hope that helped.
Offline
Alright that helped alot, its almost working now, I am using Popen wrong still I assume, here is what I got.
import os
import subprocesshere = os.getcwd()
ctr = 0
for file in os.listdir(here):
name, ext = os.path.splitext(file)
if ext == ".mp3":
p = subprocess.Popen("mpg123 -w " + name + ".wav " + name + ext , stdout=subprocess.PIPE, shell=True)
ctr = ctr + 1
print ctr
That executes, but gives this error,
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
version 1.4.3; written and copyright by Michael Hipp and others
free software (LGPL/GPL) without any warranty but with best wishes
[readers.c:893] error: Cannot file [Beginning: No such file or directory
[mpg123.c:516] error: Cannot open [Beginning: File access error. (code 22)
[readers.c:893] error: Cannot file of: No such file or directory
[mpg123.c:516] error: Cannot open of: File access error. (code 22)
[readers.c:893] error: Cannot file Whats About to Happen] HWY 74.wav 01 [Beginni
ng of Whats: No such file or directory
[mpg123.c:516] error: Cannot open Whats About to Happen] HWY 74.wav 01 [Beginnin
g of Whats: File access error. (code 22)
[readers.c:893] error: Cannot file About: No such file or directory
[mpg123.c:516] error: Cannot open About: File access error. (code 22)
[readers.c:893] error: Cannot file to: No such file or directory
[mpg123.c:516] error: Cannot open to: File access error. (code 22)
[readers.c:893] error: Cannot file Happen]: No such file or directory
[mpg123.c:516] error: Cannot open Happen]: File access error. (code 22)
[readers.c:893] error: Cannot file HWY: No such file or directory
[mpg123.c:516] error: Cannot open HWY: File access error. (code 22)
[readers.c:893] error: Cannot file 74.mp3: No such file or directory
[mpg123.c:516] error: Cannot open 74.mp3: File access error. (code 22)
2
If i remove the subprocess popen, and just print name + ext and use the counter, it prints out both file name together, and how many mp3's it processed, 2. So i know the program is doing what I want, im just converting wrong. Thanks again.
here is whats in the directory, there is more then mp3's, so I know it is filtering the files properly thanks to your splittext.
justin ~/build/convert $ dir -ls
total 12104
20 -rw-r--r-- 1 justin justin 17138 2008-05-19 17:59 00\ -\ Wretch.txt
4 -rw-r--r-- 1 justin justin 44 2008-06-01 18:20 01
7588 -rw-r--r-- 1 justin justin 7768364 2008-05-19 17:59 01\ [Beginning\ of\ What's\ About\ to\ Happen]\ HWY\ 74.mp3
4 -rw-r--r-- 1 justin justin 44 2008-06-01 18:20 02
4484 -rw-r--r-- 1 justin justin 4588063 2008-05-19 17:59 02\ Love\ Has\ Passed\ Me\ By.mp3
4 -rw-r--r-- 1 justin justin 1284 2008-06-01 18:20 convert.py
justin ~/build/convert $
Last edited by axion419 (2008-06-01 22:23:32)
Offline
You may want to wait for kumico's reply on this; I'm rather new to python myself. But, the problem is that when you have
name, ext = os.path.splitext(file)
name is just going to be a basic string of the filename. So if you have the file "Random File.mp3", name is going to be "Random File". So now when we move on to the Popen, you're basically giving the following command:
mpg123 -w Random File.wav Random File.mp3
And any time using bash and a space, it needs to be escaped with a backslash...which is not happening in your script, which breaks things.
I think you could fix this by changing your Popen command and not using 'shell=True'. Not having that would allow you to have a list of commands instead of a string, which will hopefully solve your problem. (I think... I just quickly glanced over documentation).
Maybe try something like:
p = subprocess.Popen(["mpg123", "-w", name + ".wav", name + ext], stdout=subprocess.PIPE)
I dunno, sorry if this doesn't help. I'm lazy and just use os.popen, which is easier (for me)...but apparently depreciated.
Last edited by BetterLeftUnsaid (2008-06-02 00:58:40)
Offline