Skip to content

Instantly share code, notes, and snippets.

@remram44
Created July 8, 2012 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remram44/3072068 to your computer and use it in GitHub Desktop.
Save remram44/3072068 to your computer and use it in GitHub Desktop.
Conversion avec respect d'arborescence
#!/usr/bin/python
# convert_files.py -- simple script to convert multiple files in parallel
# July 8th, 2012
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
#
# Remi Rampin remirampin@gmail.com
#
import os
import shutil
import multiprocessing
import subprocess
import functools
inputFolder = "/home/grou/python/input"
outputFolder = "/home/grou/python/output"
def tr_copy(sourcedir, targetdir, name):
try:
shutil.copy(
os.path.join(sourcedir, name),
os.path.join(targetdir, name))
return True
except Exception:
return False
def tr_encode(sourcedir, targetdir, name):
pos = name.rfind('.')
target = name[:pos] + '.ogg'
r = execute_no_output([
"oggenc", "-q", "6",
os.path.join(
sourcedir,
name),
"-o",
os.path.join(
targetdir,
target)])
return r == 0
commands = {
'mp3': tr_copy,
'ogg': tr_copy,
'wav': tr_encode,
'flac': tr_encode,
}
def execute_no_output(args):
"""Execute a command without outputing anything on the console.
"""
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while p.poll() is None:
p.communicate()
return p.returncode
def dispatch(args):
sourcedir, targetdir, name = args
ext = name.split('.')[-1].lower()
try:
return commands[ext](
sourcedir,
targetdir,
name)
except KeyError:
print "No action for %s -- skipping" % (
os.path.join(sourcedir, name),)
return None
os.chdir(inputFolder)
tasks = []
for dirpath, dirnames, filenames in os.walk('.'):
print "Entering %s" % dirpath
target = os.path.join(outputFolder, dirpath)
if not os.path.isdir(target):
print "Creating %s" % target
os.makedirs(target)
for name in filenames:
tasks.append((
os.path.join(inputFolder, dirpath),
os.path.join(outputFolder, dirpath),
name))
done = 0
failed = 0
skipped = 0
total = len(tasks)
pool = multiprocessing.Pool(None)
for result in pool.imap_unordered(dispatch, tasks):
if result is True:
done += 1
elif result is False:
failed += 1
else:
skipped += 1
print "%3d%% (%d failed, %d skipped)" % (
int(100 * (done + failed + skipped) / total),
failed,
skipped)
print "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment