Skip to content

Instantly share code, notes, and snippets.

@crmccreary
Created June 9, 2011 23:25
Show Gist options
  • Save crmccreary/1017987 to your computer and use it in GitHub Desktop.
Save crmccreary/1017987 to your computer and use it in GitHub Desktop.
Parallel transcoding of ogg files to mp3 (not that it's a good idea, just that I had to)
import subprocess
import os
import argparse
import pp
'''
Parallel transcoding of ogg files to mp3. And yes, lossy to lossy is a bad idea.
Needed to do this since Google's Music Beta doesn't support ogg and that's the
format in which my music is encoded.
This works on linux (ffmpeg), and needs the uncrippled ffmpeg (mp3 support, http://ubuntuforums.org/archive/index.php/t-1117283.html)
'''
def transcode(target_ogg, target_mp3):
cmd = 'ffmpeg -ab 192k -i "%s" -map_meta_data 0:0,s0 "%s"' % (target_ogg, target_mp3)
print('Cmd: %s' % (cmd,))
proc = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
stdout_value = proc.communicate()[0]
return stdout_value
ppservers = ()
job_server = pp.Server(ppservers=ppservers)
print "Starting pp with", job_server.get_ncpus(), "workers"
jobs = []
parser = argparse.ArgumentParser(description='Convert ogg files to mp3 using the non-crippled ffmpeg')
parser.add_argument('path_to_walk')
args = parser.parse_args()
for root, dirs, files in os.walk(args.path_to_walk):
for fname in files:
base, ext = os.path.splitext(fname)
if ext[1:].lower() == 'ogg':
if os.path.exists(os.path.join(root,base + '.mp3')):
# Does an mp3 exist?
#print('mp3 exists! %s' % (os.path.join(root,base + '.mp3'),))
pass
else:
print('mp3 does not exist %s' % (os.path.join(root,base + '.mp3'),))
target_mp3 = os.path.join(root,base + '.mp3')
target_ogg = os.path.join(root,fname)
# Submit parallel transcoding jobs
# transcode - the function
# (target_mp3, target_ogg) - tuple with arguments for transcode
# () - tuple with functions on which function transcode depends
# ("subprocess",) - tuple with module names which must be imported before md5test execution
jobs.append(job_server.submit(transcode, (target_ogg, target_mp3), (), ("subprocess",)))
# Retrieve results of all submited jobs
for job in jobs:
result = job()
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment