Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf8 -*-
- # This script converts all media files within the current directory which match the
- # search pattern into MP3 files.
- # Import required modules
- import os, re, multiprocessing
- # Init Queue for found media files
- q = multiprocessing.Queue()
- # Function to encode MP3 files
- def encodeAudio ():
- while not q.empty():
- # Get next entry out of file queue
- sourcefile = q.get()
- print sourcefile
- targetfile = sourcefile[:-3] + 'mp3'
- # Feel free to adjust the encoding tool used and its parameters :)
- commandline = "ffmpeg -i \"" + sourcefile + "\" -acodec libmp3lame -ac 2 -ab 192k -vn -y \""+ targetfile + "\""
- os.popen (commandline)
- # Find out how many CPU cores the host has
- # Beware: I am not shure how Intel CPUs with Hyperthreading react. They report each thread as logical CPU but spawning an
- # encoding thread for each "CPU" might apply too heavy load to the host. You should consider setting cores to a
- # constant value then.
- cores=multiprocessing.cpu_count()
- # Definition of extensions you want to search for. Only add lowercased extensions here as
- # the are automatically added in their uppercased version.
- extensions = ['avi','flv', 'mp4', 'wav', 'ogg', 'mpg', 'aac', 'flac' ]
- # Generate search pattern from extension list
- pattern = ''
- for n in extensions:
- if len(pattern) > 0: pattern += '|'
- pattern = pattern + n + '|' + n.upper()
- # Build regular expression for search pattern
- expr = re.compile (".*\.(" + pattern + ")$")
- # Read all files from current directory
- files = os.listdir('.')
- # Add media files matching the search pattern to the process queue
- for n in files:
- if expr.match (n): q.put(n)
- # Spawn a seperate audio encoding process for each core
- for i in range (cores):
- process = multiprocessing.Process(target=encodeAudio, args=())
- process.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement