Write a simple python script to convert all .wav files to a specific folder in .mp3 using lame

I would like to write a simple script to convert dozens of .wav files that I have in my folder for v0 mp3. It doesn't have to be complicated, enough to do the job and help me learn a little python in the process;)

I realized that I needed to use something like β€œfrom a subprocess import call” to make a β€œlame” call, but I was fixated on how I could write everything else. I wrote bash scripts to do this before, but in windows I don't really like them.

I understand basic python programming.

+1
source share
2 answers

Here is an example that works on Ubuntu Linux at least. If you are on Windows, you will need to change the direction of the slash.

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) 
+1
source

This is what I have come to 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") 
-1
source

Source: https://habr.com/ru/post/921306/


All Articles