【问题标题】:Error to upload entire video file into memory将整个视频文件上传到内存时出错
【发布时间】:2020-04-03 15:11:16
【问题描述】:

我正在尝试将整个视频文件上传到 RAM 以便快速访问。我希望文件保持原样,无需任何解码等。我只想指向 RAM 中的一个位置,而不是远程驱动程序。该文件只有 2 GB。我有 128 GB 内存。我需要逐帧分析,并且从服务器准备好需要很长时间。

我想我会做这样的事情

with open('my_file.txt', 'r') as f:
    file_content = f.read() # Read whole file in the file_content string
print(file_content)

但我可能会出错。还有另一种方法吗?喜欢使用 IO 库吗?

In [11]: u = open("/net/server/raw/2020.04.02/IMG_9261.MOV",'r')

In [12]: data = u.read()

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-12-eecbc439fbf0> in <module>
----> 1 data = u.read()

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py in decode(self, input, final)
    320         # decode input (taking the buffer into account)
    321         data = self.buffer + input
--> 322         (result, consumed) = self._buffer_decode(data, self.errors, final)
    323         # keep undecoded input until the next call
    324         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 31: invalid continuation byte

此示例使用 requests.get,但这仅适用于 HTTP 我有一个可以使用 NFS 挂载的本地服务器

import requests
from pygame import mixer
import io

r = requests.get("http://example.com/somesmallmp3file.mp3")
inmemoryfile = io.BytesIO(r.content)

mixer.music.init()
mixer.music.load(inmemoryfile)
mixer.music.play()

【问题讨论】:

  • 尝试以rb 模式而不是r 模式打开文件。前者用于从文件中读取字节,后者用于人类可读的文本。

标签: python file io


【解决方案1】:

为二进制模式添加一个“b”应该可以工作。

u = open("/net/server/raw/2020.04.02/IMG_9261.MOV",'rb')

【讨论】:

    猜你喜欢
    • 2015-07-19
    • 2014-01-26
    • 2014-11-02
    • 1970-01-01
    • 2016-01-29
    • 2013-01-25
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    相关资源
    最近更新 更多