1

I'd like to write a simple script to convert a few dozen .wav files I have in a folder to v0 mp3. It doesn't need to be complicated, just enough to do the job and help me learn a little bit of python in the process ;)

I've gathered that I'll need to use something like "from subprocess import call" to do the calling of "lame", but I'm stuck as to how I can write the rest. I've written bash scripts to do this before, but on windows they're not much good to me.

I understand basic python programming.

Jay
  • 29
  • 1
  • 2

2 Answers2

2

Here's a sample that works on Ubuntu Linux at least. If you're on Windows, you'll need to change the direction of the slashes.

import os
import os.path
import sys
from subprocess import call

def main():
    path = '/path/to/directory/'
    filenames = [
        filename
        for filename
        in os.listdir(path)
        if filename.endswith('.wav')
        ]
    for filename in filenames:
        call(['lame', '-V0',
              os.path.join(path, filename),
              os.path.join(path, '%s.mp3' % filename[:-4])
              ])
    return 0

if __name__ == '__main__':
    status = main()
    sys.exit(status)
Darren Yin
  • 628
  • 3
  • 10
-1

This is what i came up with so far

#!/usr/bin/env python
import os

lamedir = 'lame'
searchdir = "/var/test"
name = []

for f in os.listdir(searchdir):
    name.append(f)

for files in name:
    iswav = files.find('.wav')
    #print files, iswav
    if(iswav >0):
        print lamedir + ' -h -V 6 ' + searchdir + files + ' ' + searchdir + files[:iswav]+'.mp3'
        os.system(lamedir + ' -h -V 6 ' + searchdir + files + " " + searchdir +  files[:iswav]+".mp3")
GeneralZero
  • 290
  • 1
  • 3
  • 15