Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python implementation of 'readAsDataURL

i'm having some troubles to get the URI from a certain file, like .mp4/.ogg/etc.. The thing is that i need to do it in python, where the webserver is running.

Initially, i proceed like this:

def __parse64(self, path_file):
    string_file = open(path_file, 'r').readlines()
    new_string_file = ''
    for line in string_file:
        striped_line = line.strip()
        separated_lines = striped_line.split('\n')
        new_line = ''
        for l in separated_lines:
            new_line += l
        new_string_file += new_line
    self.encoded_string_file = b64.b64encode(new_string_file)

But this way, doesn't give what i need, if you compare the result with given here.

What a i need is a way to implement the function readAsDataURL() from FileReader class (see the code of the link above), in python.

UPDATE: The solution given by @SeanVieira, returns a valid data field for the URI.

def __parse64(self, path_file):
    file_data = open(path_file, 'rb').read(-1) 
    self.encoded_string_file = b64.b64encode(file_data)

Now how can i complete the URI, with the previous fields? Like this.

For example: data:video/mp4;base64,data

Thanks!

like image 429
Nacho Avatar asked Mar 21 '26 09:03

Nacho


1 Answers

The problem is that you are treating binary-encoded data as text data, which is breaking your code.

Try:

def __parse64(self, path_file):
    file_data = open(path_file, 'rb').read(-1) 
    #This slurps the whole file as binary.
    self.encoded_string_file = b64.b64encode(file_data)
like image 67
Sean Vieira Avatar answered Mar 23 '26 02:03

Sean Vieira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!