Ejemplo n.º 1
0
    def transcode(self, dest_path, file_descr, asset_id, target_mimetype,
                  **options):

        file_path = expand_path_template(target_mimetype.template,
                                         target_mimetype.mimetype, asset_id,
                                         **options)

        audio_mimetype = mimetypes.guess_type(file_descr.file.filename)[0]
        try:
            tmp = tempfile.NamedTemporaryFile()
            pro = subprocess.Popen([
                "sox", file_descr.file.filename, "-t", "wav", "-r",
                str(options['samplerate']), tmp.name
            ],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
            out, err = pro.communicate()
            if pro.returncode != 0:
                print(
                    "Sox failed %s with error code %d!" %
                    (file_decrs.file.filename, pro.returncode), out, err)
                return False
            else:
                toopen = tmp.name
        except OSError:
            print("Sox failed %s!" % (file_descr.file.filename), out, err)
            return False

        wavedata = WaveData()
        wavedata.extractData(toopen, options['precision'])
        channels = wavedata.getData()

        if wavedata.nchannels == options['channels']:
            if options['channels'] == 1:
                transcoded = json.dumps({"mono": channels[0]})
            elif options['channels'] == 2:
                transcoded = json.dumps({
                    "left": channels[0],
                    "right": channels[1]
                })

        else:
            if options['channels'] == 1:
                transcoded = json.dumps({"mono": channels[0]})
            else:
                transcoded = json.dumps({
                    "left": channels[0],
                    "right": channels[0]
                })

        full_path = os.path.join(dest_path, file_path)
        if not os.path.exists(os.path.dirname(full_path)):
            os.makedirs(os.path.dirname(full_path))

        output_file = open(full_path, 'w')
        output_file.write(transcoded)
        output_file.close()

        return [file_path]
Ejemplo n.º 2
0
    def transcode(self, dest_path, file_descr, asset_id, target_mimetype,
                  **options):

        file_path = expand_path_template(target_mimetype.template,
                                         target_mimetype.mimetype, asset_id,
                                         **options)

        audio_mimetype = mimetypes.guess_type(file_descr.file.filename)[0]
        try:
            tmp = tempfile.NamedTemporaryFile()
            pro = subprocess.Popen([
                "sox", file_descr.file.filename, "-t", "wav", "-r",
                str(options['samplerate']), tmp.name
            ],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
            out, err = pro.communicate()
            if pro.returncode != 0:
                print(
                    "Sox failed %s with error code %d!" %
                    (file_decrs.file.filename, pro.returncode), out, err)
                return False
            else:
                toopen = tmp.name
        except OSError:
            print("Sox failed %s!" % (file_descr.file.filename), out, err)
            return False

        wavedata = WaveData()
        wavedata.extractData(toopen, 2)
        channels = wavedata.getData()
        gcolor = options['color']

        plt.figure(1, figsize=options['size'])
        if wavedata.nchannels == 2:
            plt.subplot(211)
            plt.plot(channels[0], color=gcolor)
            plt.axis('off')
            plt.subplot(212)
            plt.plot(channels[1], color=gcolor)
        else:
            plt.plot(channels[0])

        plt.axis('off')

        full_path = os.path.join(dest_path, file_path)
        if not os.path.exists(os.path.dirname(full_path)):
            os.makedirs(os.path.dirname(full_path))

        plt.savefig(full_path, dpi=options['dpi'])

        return [file_path]
    def transcode(self, dest_path, file_descr, asset_id, target_mimetype,
            **options):

        file_path = expand_path_template(target_mimetype.template,
                target_mimetype.mimetype, asset_id, **options)
        
        audio_mimetype = mimetypes.guess_type(file_descr.file.filename)[0]
        try:
            tmp = tempfile.NamedTemporaryFile()
            pro = subprocess.Popen(["sox", file_descr.file.filename, "-t", "wav", "-r", str(options['samplerate']), tmp.name], 
                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = pro.communicate()
            if pro.returncode != 0:
                print("Sox failed %s with error code %d!" %(file_decrs.file.filename, pro.returncode), 
                        out, err)
                return False
            else:
                toopen = tmp.name
        except OSError:
            print("Sox failed %s!" %(file_descr.file.filename), out, err)
            return False

        wavedata = WaveData()
        wavedata.extractData(toopen, options['precision'])
        channels = wavedata.getData()

        if wavedata.nchannels == options['channels']:
            if options['channels'] == 1:
                transcoded = json.dumps({"mono": channels[0]})
            elif options['channels'] ==2:
                transcoded = json.dumps({"left": channels[0], "right": channels[1]})

        else:
            if options['channels'] == 1:
                transcoded = json.dumps({"mono": channels[0]})
            else :
                transcoded = json.dumps({"left": channels[0], "right": channels[0]})
        
        full_path = os.path.join(dest_path, file_path)
        if not os.path.exists(os.path.dirname(full_path)):
            os.makedirs(os.path.dirname(full_path))

        output_file = open(full_path, 'w')
        output_file.write(transcoded)
        output_file.close()

        return [file_path]
    def transcode(self, dest_path, file_descr, asset_id, target_mimetype,
            **options):

        file_path = expand_path_template(target_mimetype.template,
                target_mimetype.mimetype, asset_id, **options)
        
        audio_mimetype = mimetypes.guess_type(file_descr.file.filename)[0]
        try:
            tmp = tempfile.NamedTemporaryFile()
            pro = subprocess.Popen(["sox", file_descr.file.filename, "-t", "wav", "-r", str(options['samplerate']),  tmp.name], 
                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = pro.communicate()
            if pro.returncode != 0:
                print("Sox failed %s with error code %d!" %(file_decrs.file.filename, pro.returncode), 
                        out, err)
                return False
            else:
                toopen = tmp.name
        except OSError:
            print("Sox failed %s!" %(file_descr.file.filename), out, err)
            return False

        wavedata = WaveData()
        wavedata.extractData(toopen, 2)
        channels = wavedata.getData()
        gcolor = options['color']

        plt.figure(1, figsize = options['size'])
        if wavedata.nchannels == 2:
            plt.subplot(211)
            plt.plot(channels[0], color = gcolor)
            plt.axis('off')
            plt.subplot(212)
            plt.plot(channels[1], color = gcolor)
        else:
            plt.plot(channels[0])
        
        plt.axis('off')
        
        full_path = os.path.join(dest_path, file_path)
        if not os.path.exists(os.path.dirname(full_path)):
            os.makedirs(os.path.dirname(full_path))

        plt.savefig(full_path, dpi = options['dpi'])

        return [file_path]