Returning data in the encoding driver

The current implementation of the encoding driver returns the full path of the file that has been encoded. This made sense in the previous implementation because the encoding driver also wrote the transcoded file at its destination location. Now that the transcoded file is saved on a database, this must change. Instead, the encoding driver should return the data from the transcoded file, so that it can be written anywhere. For this, some evolutions are needed in the audio encoding function of the driver, as demonstrated in the following example:

def mp3_to_flac(data):
tmp_filename = os.path.join('/tmp/transcode-tmp.mp3')
tmp2_filename = os.path.join('/tmp/transcode-tmp.flac')
with open(tmp_filename, 'wb') as content_file:
size = content_file.write(data)
status = 0 if size == len(data) else -1
transformer = sox.Transformer()
transformer.convert(samplerate=16000, n_channels=2, bitdepth=16)
transformer.build(tmp_filename, tmp2_filename)

# retrieve data in a buffer
with open(tmp2_filename, mode='rb') as file:
flac_data = file.read()

os.remove(tmp_filename)
os.remove(tmp2_filename)
return flac_data

Now, this function only takes MP3 data as input, and it returns FLAC data as output. Also, the sox APIs only work with filename parameters. So, the destination filename of sox is also set to a file in /tmp. When the encoding is completed, then the content of the encoded file (located in tmp2_filename) is read to the flac_data variable. At the end of the function, the two temporary files are deleted, and the FLAC data is returned. A benefit of this change is that the transcoding function now only operates on files located in /tmp. This means that it operates on files located in a RAM filesystem, a filesystem with no access to I/O devices. In other words, the sox APIs can no longer block on read or write operations. So, this change fixed the issue where the encode driver blocked on I/O calls.

The EncodeResult item must also evolve so that the data is returned instead of the filename, as in the following example:

EncodeResult = namedtuple('EncodeResult', ['id', 'data', 'key'])

The file field is replaced with the data field, and the key provided in the request is also sent back in the response. These changes can be used in the driver function to return FLAC data in the response observable. The new code for the encode driver is available in the file present at audio-encode-server-2/audio_encode_server/encoder.py on the GitHub repository.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.80.34