mirror of
https://github.com/correl/dejavu.git
synced 2024-11-23 19:19:53 +00:00
Cleaned up convert.py and renamed it to decode.py
- 'Converter' class removed - 'ensure_folder' removed - 'find_files' changed into a generator and made it work with spaces - 'convert' renamed into 'read' - 'convert' now handles any supported file by pydub - 'convert' now returns the data instead of saving it to a file (same format as 'extract_channels') - generic cleanup of formatting in the file
This commit is contained in:
parent
3b72768f94
commit
7122e110d1
2 changed files with 27 additions and 54 deletions
|
@ -1,54 +0,0 @@
|
||||||
import os, fnmatch
|
|
||||||
from pydub import AudioSegment
|
|
||||||
|
|
||||||
class Converter():
|
|
||||||
|
|
||||||
WAV = "wav"
|
|
||||||
MP3 = "mp3"
|
|
||||||
FORMATS = [
|
|
||||||
WAV,
|
|
||||||
MP3]
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def ensure_folder(self, extension):
|
|
||||||
if not os.path.exists(extension):
|
|
||||||
os.makedirs(extension)
|
|
||||||
|
|
||||||
def find_files(self, path, extensions):
|
|
||||||
filepaths = []
|
|
||||||
extensions = [e.replace(".", "") for e in extensions if e.replace(".", "") in Converter.FORMATS]
|
|
||||||
print "Supported formats: %s" % extensions
|
|
||||||
for dirpath, dirnames, files in os.walk(path) :
|
|
||||||
for extension in extensions:
|
|
||||||
for f in fnmatch.filter(files, "*.%s" % extension):
|
|
||||||
p = os.path.join(dirpath, f)
|
|
||||||
renamed = p.replace(" ", "_")
|
|
||||||
os.rename(p, renamed)
|
|
||||||
#print "Found file: %s with extension %s" % (renamed, extension)
|
|
||||||
filepaths.append((renamed, extension))
|
|
||||||
return filepaths
|
|
||||||
|
|
||||||
def convert(self, orig_path, from_format, to_format, output_folder, song_name):
|
|
||||||
|
|
||||||
# start conversion
|
|
||||||
self.ensure_folder(output_folder)
|
|
||||||
print "-> Now converting: %s from %s format to %s format..." % (song_name, from_format, to_format)
|
|
||||||
|
|
||||||
# MP3 --> WAV
|
|
||||||
if from_format == Converter.MP3 and to_format == Converter.WAV:
|
|
||||||
|
|
||||||
newpath = os.path.join(output_folder, "%s.%s" % (song_name, Converter.WAV))
|
|
||||||
if os.path.isfile(newpath):
|
|
||||||
print "-> Already converted, skipping..."
|
|
||||||
else:
|
|
||||||
mp3file = AudioSegment.from_mp3(orig_path)
|
|
||||||
mp3file.export(newpath, format=Converter.WAV)
|
|
||||||
|
|
||||||
# unsupported
|
|
||||||
else:
|
|
||||||
print "CONVERSION ERROR:\nThe conversion from %s to %s is not supported!" % (from_format, to_format)
|
|
||||||
|
|
||||||
print "-> Conversion complete."
|
|
||||||
return newpath
|
|
27
dejavu/decode.py
Normal file
27
dejavu/decode.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import os
|
||||||
|
import fnmatch
|
||||||
|
import numpy as np
|
||||||
|
from pydub import AudioSegment
|
||||||
|
|
||||||
|
|
||||||
|
def find_files(path, extensions):
|
||||||
|
# Allow both with ".mp3" and without "mp3" to be used for extensions
|
||||||
|
extensions = [e.replace(".", "") for e in extensions]
|
||||||
|
|
||||||
|
for dirpath, dirnames, files in os.walk(path):
|
||||||
|
for extension in extensions:
|
||||||
|
for f in fnmatch.filter(files, "*.%s" % extension):
|
||||||
|
p = os.path.join(dirpath, f)
|
||||||
|
yield (p, extension)
|
||||||
|
|
||||||
|
|
||||||
|
def read(filename):
|
||||||
|
audiofile = AudioSegment.from_file(filename)
|
||||||
|
|
||||||
|
data = np.fromstring(audiofile._data, np.int16)
|
||||||
|
|
||||||
|
channels = []
|
||||||
|
for chn in xrange(audiofile.channels):
|
||||||
|
channels.append(data[chn::audiofile.channels])
|
||||||
|
|
||||||
|
return audiofile.frame_rate, channels
|
Loading…
Reference in a new issue