Created
May 2, 2015 05:17
-
-
Save dgrant/803a957ce86e535fb162 to your computer and use it in GitHub Desktop.
Convert .ogg files to .mp3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Convert ogg files to mp3""" | |
from optparse import OptionParser | |
import tempfile | |
import os | |
import subprocess | |
class CommandException(Exception): | |
"""Exception that is raised if calling an external command fails""" | |
def __init__(self, command): | |
self.command = command | |
def __str__(self): | |
return self.command | |
def ogg2wav(ogg_file, wav_file): | |
"""Run ogg123 to convert from an ogg file to a wav file""" | |
commands = ["ogg123", "-d", "wav", "-f", wav_file, ogg_file] | |
run_command(commands) | |
def wav2mp3(wav_file, mp3_file, lame_vbr_setting): | |
"""Convert wav file to mp3 file""" | |
commands = ["lame", | |
"-V", | |
lame_vbr_setting, | |
"--vbr-new", | |
"-h", | |
wav_file, | |
mp3_file] | |
run_command(commands) | |
def run_command(command): | |
"""Run a command, given an array of the command and arguments""" | |
proc = subprocess.Popen(command) | |
ret = proc.wait() | |
if ret != 0: | |
raise CommandException(" ".join(command)) | |
def ogg2mp3(input_files, outputdir, lame_V): | |
"""Convert ogg files to mp3""" | |
for ogg_file in input_files: | |
#Convert ogg file to wav | |
input_file_dir, input_file_name = os.path.split(ogg_file) | |
if outputdir == None: | |
mp3dir = input_file_dir | |
else: | |
mp3dir = outputdir | |
input_file_base_name, _ = os.path.splitext(input_file_name) | |
wav_file = os.path.join(tempfile.mkdtemp(), input_file_base_name+".wav") | |
ogg2wav(ogg_file, wav_file) | |
#Convert wav file to mp3 | |
mp3_file = os.path.join(mp3dir, input_file_base_name+".mp3") | |
wav2mp3(wav_file, mp3_file, lame_V) | |
def main(): | |
"""Main script. Get options and arguments""" | |
usage = "usage: %prog [options] <input file pattern>" | |
parser = OptionParser(usage) | |
parser.add_option("-o", "--outputdir", | |
action="store", type="string", dest="outputdir") | |
parser.add_option("-V", | |
action="store", type="string", dest="lameV", default="2") | |
options, args = parser.parse_args() | |
if len(args) < 1: | |
# need some files to process! | |
parser.error("incorrect number of arguments") | |
ogg2mp3(args, options.outputdir, options.lameV) | |
if __name__ == "__main__": | |
main() |
@Erkaboy0313 What have you tried?
@Erkaboy0313 What have you tried?
Just I have written this to console.
python ogg3mp3.py -o ex.ogg
I don't know is it right or not. And I tried more different ways. But failed. Becouse of not knowing option parsing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi ,
I have tried to run this program but I couldn't ,
It would be great if you could tell me how to run.
Thanks a lot.