HOWTO: legally encode mp3s in freebsd

Here is a way to legally make mp3s on Freebsd

There is a discussion here about mp3 encoders - it is hard to tell if we should be using LAME or not, though.

So I decided to use the linux binary version from here, which the mp3 people say is certified.

Then I tried making up a python script to encode mp3s easily.
To use this script, you have to have vorbis-tools installed (package) and you have to get the mp3sEncoder program from the above website. I put it in ~/.local/bin/mp3sEncoder, and this script needs it there.

Feel free to use/change this if you find it useful.

edit: I know, I don't really know what I'm doing, but anyway... here is an update, that transfers metadata. Should I not be using subprocess here? Oh, I guess this isn't a python forum.

mp3enc.py
Code:
#!/usr/bin/env python

# needs py-id3
# needs py25-mutagen
# parse arguments

PIPE = False
DO_OGG = False

import sys, subprocess as sub, os, re, time

from mutagen.oggvorbis import OggVorbis
from ID3 import *


tags = ['artist', 'album', 'tracktotal', 'tracknumber', 'title']
metadata = {}

def show_usage():
    """Show Program Usage"""
    print 'Encode mp3s using mp3sEncoder from http://www.all4mp3.com/'
    print 'Usage: mp3enc [options] files files ...'
    print '--ogg        use oggdec to change to wav first'
    print '--pipe     try to pipe from oggdec to mp3sEncoder - much faster'
    print '--help       show this help'
    print

# If no arguments were given, print a helpful message
if '--help' in sys.argv or '-help' in sys.argv or len(sys.argv) == 1:
    show_usage()
    sys.exit(0)

home_dir = os.path.expanduser('~')

if '--ogg' in sys.argv: DO_OGG = True
if '--pipe' in sys.argv: PIPE = True

# for ogg step, I could figure out how to pipe it, but not for now.
# oggdec -R -o - 06*.oga | ~/.local/bin/mp3sEncoder -if - -of test.mp3 -br 128000 -res 16 -c 2 -sr 44100

for i in sys.argv[1:]:
    if i == '--ogg' or i == '--pipe': continue
    print 'converting ' + i
    if DO_OGG:
        print 'reading metadata...'
        audio = OggVorbis(i)
        for tag in tags:
            if audio.has_key(tag):
                metadata[tag] = audio[tag][0]
        ogg_file = i
        if not PIPE:
            wav_file = re.sub(r'(\.ogg|\.oga)$', '.wav', i)
        mp3_file = re.sub(r'(\.ogg|\.oga)$', '.mp3', ogg_file)
    else:
        wav_file = i
        mp3_file = re.sub(r'\.wav$', '.mp3', wav_file)
    
    if DO_OGG and PIPE:
        #PIPE
        p1 = sub.Popen(["oggdec", "-R", "-o", "-", ogg_file], stdout=sub.PIPE)
        #~/.local/bin/mp3sEncoder -if - -of test.mp3 -br 128000 -res 16 -c 2 -sr 44100
        p2 = sub.Popen([home_dir + "/.local/bin/mp3sEncoder", "-if", "-", "-br", "128000", "-res", "16", "-c", "2", "-sr", "44100", "-of", mp3_file], stdin=p1.stdout, stdout=sub.PIPE)
        sts = os.waitpid(p2.pid, 0)
        #output = p2.communicate()[0]
        
        print 'saving metadata...'
        audio = ID3(mp3_file)
        for tag in tags:
            if tag in metadata.keys():
                audio[str.upper(tag)] = metadata[tag]
                print 'Setting ' + tag + ' = ' + metadata[tag]
    elif not PIPE:
        if DO_OGG:
            output = sub.Popen(['oggdec', ogg_file], stdout=sub.PIPE).communicate()[0]
        output = sub.Popen([home_dir + '/.local/bin/mp3sEncoder', '-if', wav_file, '-of', mp3_file, '-br', '128000'], stdout=sub.PIPE).communicate()[0]
        print 'removing wav file...'
        os.remove(wav_file)
    print 'done...'
    print

print 'Done'
 
Back
Top