Refactor of generate waveform (crashing on larger mp3)

This commit is contained in:
Fergal Moran
2014-03-14 16:05:23 +00:00
parent 5d0612e037
commit 5ad348ef54
3 changed files with 64 additions and 55 deletions

View File

@@ -11,7 +11,6 @@ except ImportError:
from core.utils.waveform import generate_waveform
from dss import settings
from spa.signals import waveform_generated_signal, update_user_geoip_signal
from spa.models.userprofile import UserProfile
@task(time_limit=3600)
@@ -21,7 +20,7 @@ def create_waveform_task(in_file, uid):
generate_waveform(in_file, out_file)
if os.path.isfile(out_file):
print "Waveform generated successfully"
file, extension = os.path.splitext(in_file)
out_file, extension = os.path.splitext(in_file)
new_file = os.path.join(settings.MEDIA_ROOT, "mixes", "%s%s" % (uid, extension))
print "Moving cache audio clip from %s to %s" % (in_file, new_file)
shutil.move(in_file, new_file)

View File

@@ -6,39 +6,43 @@ from dss import settings
def generate_waveform(input_file, output_file):
print "Generating waveform"
working_file = "%s%s.png" % (settings.DSS_TEMP_PATH, uuid.uuid1())
try:
working_file = "%s%s.wav" % (settings.DSS_TEMP_PATH, uuid.uuid1())
try:
print "Starting decode : %s\nInput File: %s\nOutput File: %s" % \
(settings.DSS_LAME_PATH, input_file, working_file)
#sox f679a81a-ea14-4385-a677-c663559d1e4b.mp3 -c 1 -t wav -
#| /srv/dss/bin/wav2png -w 800 -h 120 -o song.png /dev/stdin
convert = subprocess.Popen("%s %s -c 1 -t wav" % (settings.DSS_LAME_PATH, input_file),
shell=True, stdout=subprocess.PIPE)
print "Starting decode : %s\n\tIn: %s\n\tOut: %s" % \
(settings.DSS_LAME_PATH, input_file, working_file)
#sox f679a81a-ea14-4385-a677-c663559d1e4b.mp3 -c 1 -t wav -
#| /srv/dss/bin/wav2png -w 800 -h 120 -o song.png /dev/stdin
convert_command = "%s %s -c 1 -t wav -" % (settings.DSS_LAME_PATH, input_file)
print "Convert command: %s" % convert_command
command_split = convert_command.split()
convert = subprocess.Popen(command_split,
stdout=subprocess.PIPE)
waveform = subprocess.Popen("%s -w 800 -h 120 -o %s" % (settings.DSS_WAVE_PATH, working_file),
shell=True, stdout=subprocess.PIPE)
print "Finished decode"
if os.path.exists(working_file):
print "Starting waveform generation"
ret = subprocess.call([
settings.DSS_WAVE_PATH, "-w", "800", "-h", "120", "-o",
output_file,
working_file], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if os.path.isfile(output_file):
os.remove(working_file)
print "Generated waveform"
return output_file
else:
print "Failed generating waveform: %s" % output_file
print "Subprocess returned: %s" % ret
waveform_command = "%s -w 800 -h 120 -o %s" % (settings.DSS_WAVE_PATH, working_file)
waveform = subprocess.Popen(waveform_command,
stdin=convert.stdout, stdout=subprocess.PIPE)
convert.stdout.close()
output = waveform.communicate()[0]
print "Finished decode\n%s" % output
if os.path.exists(working_file):
print "Starting waveform generation"
ret = subprocess.call([
settings.DSS_WAVE_PATH, "-w", "800", "-h", "120", "-o",
output_file,
working_file], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if os.path.isfile(output_file):
os.remove(working_file)
print "Generated waveform"
return output_file
else:
print "Unable to find working file, did LAME succeed?"
return ""
except Exception, ex:
print "Error generating waveform %s" % (ex)
print "Failed generating waveform: %s" % output_file
print "Subprocess returned: %s" % ret
else:
print "Unable to find working file, did LAME succeed?"
return ""
except Exception, ex:
print "Error generating waveform %s" % (ex)
except:
print "Error generating waveform"
traceback.print_exc()

View File

@@ -1,42 +1,48 @@
from optparse import make_option
import os
from django.core.management.base import NoArgsCommand
from django.core.management.base import NoArgsCommand, BaseCommand
from spa.models.mix import Mix
from core.tasks import create_waveform_task
class Command(NoArgsCommand):
class Command(BaseCommand):
help = "Generate all outstanding waveforms"
option_list = BaseCommand.option_list + (
make_option('--nocelery',
action='store_true',
dest='nocelery',
default=False,
help='Dispatch calls to celery broker'),
)
def _generateWaveform(self, mix):
@staticmethod
def _get_file(mix):
#Check for file in mix directory
processed_file = ""
try:
processed_file = mix.get_absolute_path()
if not os.path.isfile(processed_file):
cached_file = mix.get_cache_path()
if not os.path.isfile(cached_file):
print "File for [%s] not found tried\n\t%s\n\t%s" % \
(mix.title, processed_file, cached_file)
return
else:
print "File found, starting waveform task (%s)" % mix.uid
create_waveform_task.delay(
in_file=cached_file, uid=mix.uid)
print "Task submitted"
processed_file = mix.get_cache_path()
if not os.path.isfile(processed_file):
print "File for [%s] not found tried\n\t%s\n\t%s" % (mix.title, processed_file, processed_file)
return ""
except Exception, ex:
print "Error generating waveform: %s" % ex.message
return processed_file
def handle(self, *args, **options):
print "Scanning for missing waveforms"
unprocessed = Mix.objects.filter(waveform_generated=False)
for mix in unprocessed:
print "Generating waveform for mix %d" % mix.id
self._generateWaveform(mix)
print "Found %s" % mix.slug
mix_file = self._get_file(mix)
if mix_file is not "":
if options['nocelery']:
create_waveform_task(in_file=mix_file, uid=mix.uid)
else:
create_waveform_task.delay(in_file=mix_file, uid=mix.uid)
def handle_noargs(self, **options):
print "Scanning for missing waveforms"
unprocessed = Mix.objects.filter(waveform_generated=False)
for mix in unprocessed:
print "Generating waveform for mix %d" % mix.id
self._generateWaveform(mix.uid)