class BlenderTranscoder(ITranscoder):
    options = [
        IntVectorOption(name='size',
                        description='The target size of the image',
                        size=2,
                        min=1,
                        max=4096,
                        default=(128, 128)),
        IntOption(name='frames',
                  description='Total number of frames.',
                  min=1,
                  max=4096,
                  default=12),
        IntOption(name='footage',
                  description='Number of frames per line',
                  min=1,
                  max=4096,
                  default=4),
    ]
    convert_map = {
        "application/x-blender.object": {
            "image/jpg-reel": options,
            "image/png-reel": options
        },
        "application/x-blender.mesh": {
            "image/jpg-reel": options,
            "image/png-reel": options
        },
        "application/x-blender.group": {
            "image/jpg-reel": options,
            "image/png-reel": options
        },
    }

    def __init__(self):
        ITranscoder.__init__(self)

    def activate(self):
        pass

    def transcode(self, dest_path, file_descr, asset_id, target_mimetype,
                  **options):
        path_template = expand_path_template(target_mimetype.template,
                                             target_mimetype.mimetype,
                                             asset_id, **options)
        abs_file_path = os.path.join(dest_path, path_template)
        template = Template(path_template + '__${angles}')

        angles = [0.0, 0.1, 0.5]

        width = options['size'][0] * options['footage']
        height = options['size'][1] * (options['frames'] // options['footage'])

        angles = []
        file_paths = []
        for angle in range(0, 628, 628 // 12):
            angle = angle // 100.0
            file_path = template.safe_substitute(angles=angle)
            file_paths.append(file_path)
            angles.append(angle)

        if asset_id.mimetype == 'application/x-blender.mesh':
            datatype = 'mesh'
        elif asset_id.mimetype == 'application/x-blender.group':
            datatype = 'group'
        else:
            datatype = 'object'

        arguments = [
            '--', datatype, asset_id.subname,
            os.path.join(dest_path, template.safe_substitute())
        ]
        arguments.extend(list(map(str, angles)))
        arguments.append('--format=PNG')  # TODO
        arguments.append('--camera_type=PERSPECTIVE')
        arguments.append('--width=' + str(options['size'][0]))
        arguments.append('--height=' + str(options['size'][1]))

        script = os.path.join(os.path.dirname(__file__),
                              '../render/b-script-transcoderblenderrender.py')

        logger.debug(abs_file_path)

        stdoutdata, stderrdata, returncode = run_blender(
            file_descr.file.filename, script, arguments)

        logger.debug(stdoutdata)
        logger.debug(stderrdata)
        logger.debug(returncode)
        # print(returncode) # Todo: check return code

        sprite = Image.new('RGB', (width, height))
        for i, file_path in enumerate(file_paths):
            path = os.path.join(dest_path, file_path)
            tile = Image.open(path)
            x = (i % options['footage']) * options['size'][0]
            y = (i // options['footage']) * options['size'][1]
            sprite.paste(tile, (x, y))

        # sprite.show()
        sprite.save(abs_file_path)

        return [path_template]
Exemplo n.º 2
0
class Audio2ImageTranscoder(ITranscoder):
    options = [
        HexColorOption(name='color',
                       description='Color of the plot',
                       default='#0000ff'),
        IntOption(name='samplerate',
                  description='Sample Rate of the audio',
                  default=800),
        VectorOption(name='size',
                     type=int,
                     description='(width, height) tuple',
                     default=(8, 6),
                     min=0,
                     max=Sizes.maxint,
                     size=2),
        IntOption(name='dpi', description='DPI of image', default=80, min=0)
    ]

    convert_map = {
        "audio/x-wav": {
            "image/png": options,
            "image/jpeg": options
        },
        "audio/mpeg": {
            "image/png": options,
            "image/jpeg": options
        }
    }

    def __init__(self):
        ITranscoder.__init__(self)

    def activate(self):
        pass

    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]
Exemplo n.º 3
0
class Audio2JsonTranscoder(ITranscoder):
    options = [
        IntOption(name='channels',
                  description='Number of channels to output',
                  default=2,
                  min=1,
                  max=2),
        IntOption(name='samplerate',
                  description='Samples per second each channel',
                  default=800,
                  min=200),
        IntOption(name='precision',
                  description='Decimal Precision',
                  default=2,
                  min=1)
    ]

    convert_map = {
        "audio/x-wav": {
            "application/json": options
        },
        "audio/mpeg": {
            "application/json": options
        }
    }

    def __init__(self):
        ITranscoder.__init__(self)

    def activate(self):
        pass

    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_descr.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]
class BlenderTranscoder(ITranscoder):
    options = [
        IntVectorOption(name='size',
                        description='The target size of the image',
                        size=2,
                        min=1,
                        max=4096,
                        default=(128, 128)),
        IntOption(name='pages',
                  description='Total number of frames.',
                  min=1,
                  max=4096,
                  default=1),
    ]
    convert_map = {
        "application/x-blender.text": {
            "image/jpg": options,
            "image/png": options
        },
    }

    def __init__(self):
        ITranscoder.__init__(self)

    def activate(self):
        pass

    def transcode(self, dest_path, file_descr, asset_id, target_mimetype,
                  **options):
        path_template = expand_path_template(target_mimetype.template,
                                             target_mimetype.mimetype,
                                             asset_id, **options)
        abs_file_path = os.path.join(dest_path, path_template)
        abs_file_path_txt = abs_file_path + '.txt'

        arguments = [
            '--', asset_id.mimetype, asset_id.subname, abs_file_path_txt
        ]

        logger.debug(abs_file_path)

        stdoutdata, stderrdata, returncode = run_blender(
            file_descr.file.filename, script_path(__file__), arguments)

        logger.debug(stdoutdata)
        logger.debug(stderrdata)
        logger.debug(returncode)
        #print(returncode) #Todo: check return code

        arguments = [
            'convert', '-pointsize', '26', '-resize',
            str(options['size'][0]), abs_file_path_txt + '[0]', abs_file_path
        ]
        # print arguments
        pro = subprocess.Popen(arguments,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
        stdoutdata, stderrdata = pro.communicate()
        logger.debug(stdoutdata)
        logger.debug(stderrdata)
        logger.debug(pro.returncode)

        return [path_template]
class Video2ImageTranscoder(ITranscoder):
    """Generic Transcoder class for video2image"""
    options = [
        IntOption(name='second',
                  description='The second from which frame is to be extracted',
                  default=-1,
                  min=-1),
        IntVectorOption(name='size',
                        description='The size of output image in pixels',
                        size=2,
                        min=1,
                        max=4096,
                        default=(-1, -1))
    ]

    convert_map = {
        "video/mp4": {
            "image/png": options,
            "image/jpeg": options
        },
        "video/x-msvideo": {
            "image/png": options,
            "image/jpeg": options
        },
        "video/x-flv": {
            "image/png": options,
            "image/jpeg": options
        },
        "video/quicktime": {
            "image/png": options,
            "image/jpeg": options
        },
        "video/x-matroska": {
            "image/png": options,
            "image/jpeg": options
        },
        "video/mpeg": {
            "image/png": options,
            "image/jpeg": options
        },
    }

    def __init__(self):
        ITranscoder.__init__(self)

    def activate(self):
        pass

    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)

        time = file_descr.assets[0].metadata['duration'].string_value.split(
            ':')
        time = eval(time[0]) * 3600 + eval(time[1]) * 60 + eval(time[2])
        if time < options['second']:
            print("Not in range of video", file_descr.file.filename)
            return False

        if options['second'] == -1:
            second = time / 2
        else:
            second = options['second']
        try:
            tmp = tempfile.NamedTemporaryFile(suffix='.jpeg')
            pro = subprocess.Popen([
                'ffmpeg', '-ss',
                str(second), '-i', file_descr.file.filename, '-t', '1', '-r',
                '1', tmp.name, '-y'
            ])
            out, err = pro.communicate()
            if pro.returncode != 0:
                print(
                    'ffmpeg failed %s with error code %d' %
                    (file_descr.file.filename, pro.returncode), err)
                return False
        except OSError:
            print("Cannot open video", file_descr.file.filename)
            return False

        image = Image.open(tmp.name)

        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))

        if options['size'] != [-1, -1]:
            image.thumbnail(options['size'])
        image.save(full_path)

        return [file_path]