Python implementation 'readAsDataURL

I'm having trouble getting the URI from a specific file, for example .mp4 / .ogg / etc .. The thing is, I need to do this in python, where the web server is running.

Initially, I do the following:

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 method does not give what I need if you compare the result with the data here.

What I need is a way to implement the readAsDataURL () function from the FileReader class (see link code 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 that .

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

Thank!

+3
source share
2 answers

, - , .

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

@SeanVieria , ( 7 )

( Python 3.4):

def __parse64(self, path_file):
        data = bytearray()
        with open(path_file, "rb") as f:
            b = f.read(1)
            while b != b"":
                data.append(int.from_bytes(b, byteorder='big'))
                b = f.read(1)
        self.encoded_string_file = base64.b64encode(data)
0

Source: https://habr.com/ru/post/1792605/


All Articles