Skip to content

Instantly share code, notes, and snippets.

@odashi
Created March 9, 2014 18:37
Show Gist options
  • Save odashi/9452250 to your computer and use it in GitHub Desktop.
Save odashi/9452250 to your computer and use it in GitHub Desktop.
LAMEでWAVをまとめてMP3にするスクリプト(古いもの)
# lame.py
# Convert WAV files into MP3 format by using LAME.
# Copyright (C) 2010/06/11~ by Odashi.
import os
import sys
import subprocess
# Global settings
LAME_PATH = '"C:\\Program Files\\LAME\\lame.exe"'
DIR_SEPARATOR = '\\'
DEFAULT_SETTING = { 'quality': 2, 'bitrate': 128, 'del_infile': False, 'recursive': False }
# Divide arguments
def splitargv(argv):
setting = {}
i = 1
while i < len(argv):
if len(argv[i]) <= 2:
break
if argv[i][0:2] != '--':
break
keylist = argv[i].lstrip('-').split('=', 1)
if len(keylist) == 2:
setting[keylist[0]] = keylist[1]
else:
setting[keylist[0]] = True
i += 1
return setting, argv[i:len(argv)]
# Merge settings
def merge(user_setting):
setting = DEFAULT_SETTING
if isinstance(user_setting, dict):
for key in user_setting.keys():
setting[key] = user_setting[key]
return setting
# Convert an audio file into MP3 format
def lame(infile, outfile, setting = None):
# Make command
command = LAME_PATH
command += ' -q ' + str(setting['quality'])
command += ' -b ' + str(setting['bitrate'])
command += ' "' + infile + '"'
command += ' "' + outfile + '"'
# Show information
print('=' * 80)
print('inflie : ' + infile)
print('outfile : ' + outfile)
# Execute LAME
print('-' * 40)
print('Execute LAME ...')
print(command)
print('')
retcode = subprocess.call(command)
print('')
print('Lame Return Code: ' + str(retcode))
if retcode != 0:
print('LAME Error occered.')
print('')
return False
# Remove input file
print('-' * 40)
if setting['del_infile']:
print('Removing "' + infile + '" ...')
os.remove(infile)
else:
print('"' + infile + '" was kept.')
print('')
return True
# Convert all WAV files in a directory into MP3 format
def dirlame(directory, setting):
# Check if directory exists
if not os.path.isdir(directory):
print('"' + directory + '" is not such a directory.')
return False
# Search
for path, dirs, files in os.walk(directory):
# If "--recursive" is not selected, this program doesn't search sub directory.
if path != directory and not setting['recursive']:
break
# Find ".wav" extended file
for filename in files:
root, ext = os.path.splitext(filename)
if ext != '.wav':
continue
outfile = root + '.mp3'
# Convert
if not lame(path + DIR_SEPARATOR + filename, path + DIR_SEPARATOR + outfile, setting):
return False
return True
# Manual
def manual():
print('Usage : ' + sys.argv[0] + ' [options] <directories>')
print('Options :')
print(' --bitrate=<num> : Set MP3 bitrate[kbps]. default:' + str(DEFAULT_SETTING['bitrate']))
print(' --quality=<num> : Set MP3 quality(0-9). default:' + str(DEFAULT_SETTING['quality']))
print(' --del_infile : Delete input .wav file after convertion finished.')
print(' --recursive : Search all sub directories too.')
# Main routine
def main():
if len(sys.argv) == 1:
manual()
return
setting, argv = splitargv(sys.argv)
setting = merge(setting)
if argv == []:
print('No any target directories.')
return
# Run "dirlame" every directories
for directory in argv:
if not dirlame(directory, setting):
print('Some error occured.')
return
print('Convert finished completely.')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment