Ogg + Flac to MP3 Converter Script (Python)

Requires:

  • Python 2.x
  • TagPy (apt-get install python-tagpy)
  • apt-get install lame flac vorbis-tools
  • I might as well just say Linux, I guess

It will create a file called index in the cwd and use that to record which files it's processed, in case you need to stop it.

In terms of performance, it's *okay*. If you have two cores, it should make efficient use of them, because it runs the decoding and the encoding simultaneously (ish). If you have 4, bad luck, it'll only use two (you can use the other two to play Team Fortress 2 while it runs).

There are a few constants (paths) at the top. Tweak as desired.

You might want to change the lame options. I've set it to 192kbps CBR. Replace line 43's '-b', '192' with '-V2' if you want ~192kbps VBR (you probably do, but the version of lame I have seems to mess up the track length label when using VBR).

There are obviously caveats with transcoding Ogg to mp3, but if your source file is high bitrate, you won't hear the quality loss (prove me wrong with a double blind study).

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
#!/usr/bin/env python

import os
import subprocess
import tagpy

root = os.path.expanduser('~/Music')
out = os.path.expanduser('~/music-mp3')

indexfile = 'index'

exts = ('ogg', 'flac')

index = []
try:
    with open(indexfile) as f:
        index = f.read().splitlines()
except: pass

for directory, dirnames, filenames in os.walk(root):
    for name in filenames:
        fullname = os.path.join(directory, name)
        if not name.endswith(exts):
            continue
        if fullname in index: continue
        index += [fullname]

        fref = tagpy.FileRef(fullname)
        tag = fref.tag()
        command = None
        if name.endswith('ogg'):
            command = ['oggdec', '-Q', '-o-', fullname]
        elif name.endswith('flac'):
            command = ['flac', '-d', '-c', fullname]

        newfile = fullname.replace(root, out)
        newfile = newfile.replace('.ogg', '.mp3')
        newfile = newfile.replace('.flac', '.mp3')
        newdir = os.path.dirname(newfile)
        if not os.path.exists(newdir):
            os.makedirs(newdir)
        dec = subprocess.Popen(command, stdout=subprocess.PIPE)
        enc = subprocess.Popen(['lame', '-b', '192', '-h',
            '--ta', tag.artist,
            '--tt', tag.title,
            '--tl', tag.album,
            '--ty', str(tag.year),
            '--tn', str(tag.track),
            '-', '-'
        ], stdin=dec.stdout, stdout=subprocess.PIPE)
        with open(newfile, 'wb') as f:
            while True:
                d = enc.stdout.read(1024*100)
                if not d: break
                f.write(d)
        with open(indexfile, 'a') as f:
            f.write(fullname + '\n')

Talk is cheap

Doomups
I recommend Avdshare Audio Converter easy to convert OGG to FLAC.

– 03:33:42 14th June 2017

Leave a comment:

HTML is not valid. Use:
[url=http://www.google.com]Google[/url] [b]bold[/b] [i]italics[/i] [u]underline[/u] [code]code[/code]