3

.mp4 / .ogg / etc。などの特定のファイルからURIを取得するのに問題があります。問題は、Webサーバーが実行されているPythonでURIを取得する必要があるということです。

最初は、次のように進めます。

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)

しかし、この方法では、結果をここで示したものと比較すると、必要なものは得られません

aiに必要なのは、PythonでFileReaderクラス(上記のリンクのコードを参照)から関数readAsDataURL()を実装する方法です。

更新: @SeanVieiraによって提供されたソリューションは、URIの有効なデータフィールドを返します。

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

前のフィールドでURIを完成させるにはどうすればよいですか?このように。

例:data:video / mp4; base64、data

ありがとう!

4

2 に答える 2

0

問題は、バイナリエンコードされたデータをテキストデータとして扱っているため、コードが壊れていることです。

試す:

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)
于 2011-02-16T18:43:11.710 に答える
0

ファイルが非常に大きい(7MBを超える)場合、@SeanVieriaの回答は機能しません

この関数はすべての場合に機能します(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)
于 2018-03-01T07:29:20.893 に答える