如何从FTP直接读取WAV文件的头文件,而不用Python下载整个文件?

2024-10-02 18:16:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想直接从FTP服务器读取WAV文件(它在我的FTP服务器中),而不用下载到Python中的PC中。有可能吗?如果有,怎么做?在

我试过这个解决方案,但没用。我有.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)

Tags: 文件import服务器readbytesftpstructprint
1条回答
网友
1楼 · 发布于 2024-10-02 18:16:07

为了快速实现,您可以使用我的FtpFile类:
Get files names inside a zip file on FTP server without downloading whole archive

ftp = FTP(...)
fin = FtpFile(ftp, "C3.WAV")
# The rest of the code is the same

但是代码效率有点低,因为每个fin.read都将打开一个新的下载数据连接。在


为了更有效地实现,只需一次下载整个头文件(我不知道WAV头的结构,我以下载10kb为例):

^{pr2}$

相关问题 更多 >