python - 如何直接从 FTP 读取 WAV 文件的标题而不用 Python 下载整个文件?
问题描述
我想直接从 FTP 服务器读取 WAV 文件(在我的 FTP 服务器中),而不用 Python 将它下载到我的 PC 中。有可能吗?如果可以,怎么做?
我试过这个解决方案从 ftp python 读取缓冲区中的文件,但它没有用。我有 .wav 音频文件。我想读取该文件并从该 .wav 文件中获取详细信息,例如文件大小、字节速率等。
我能够在本地读取 WAV 文件的代码:
import struct
from ftplib import FTP
global ftp
ftp = FTP('****', user='user-****', passwd='********')
fin = open("C3.WAV", "rb")
chunkID = fin.read(4)
print("ChunkID=", chunkID)
chunkSizeString = fin.read(4) # Total Size of File in Bytes - 8 Bytes
chunkSize = struct.unpack('I', chunkSizeString) # 'I' Format is to to treat the 4 bytes as unsigned 32-bit inter
totalSize = chunkSize[0]+8 # The subscript is used because struct unpack returns everything as tuple
print("TotalSize=", totalSize)
解决方案
为了快速实现,您可以使用我的FtpFile
课程:
Get files names inside a zip file on FTP server without download entire archive
ftp = FTP(...)
fin = FtpFile(ftp, "C3.WAV")
# The rest of the code is the same
虽然代码有点低效,因为每个都fin.read
将打开一个新的下载数据连接。
为了更高效的实现,只需一次下载整个头文件(我不知道 WAV 头文件结构,我这里下载 10 KB 作为示例):
from io import BytesIO
ftp = FTP(...)
fin = BytesIO(FtpFile(ftp, "C3.WAV").read(10240))
# The rest of the code is the same
推荐阅读
- sharepoint - PowerApps - 带有按钮的 ifblank 不起作用
- javascript - 如何移动代码以设置 ut DB 并从我的文件中收集并要求它?
- angular - Angular 10 - 保护单元测试
- ruby - Ruby:使用括号和字符串键访问字符串中的字符
- sql - postgresql 查询性能慢
- javascript - javascript多重计算,百分比倍数,除以
- embedded - 如何从 efm32 控制器一次发送 32 位帧
- r - R:是否有相当于将 Excel 的锁定单元格引用应用于整个字段的计算?
- java - 即使在基本查询spring boot JPA中也能获得嵌套异常
- php - 首先是一个特定的 id,然后是 desc 顺序的所有 id