gpt4 book ai didi

python - pydub AudioSegment.export 正在锁定 smb 共享上的文件。无法删除该文件

转载 作者:行者123 更新时间:2023-12-04 23:19:13 29 4
gpt4 key购买 nike

尝试使用 pydub 从立体声文件中提取单声道音频时遇到问题。
这是代码:

import wave
import audioop
from pydub import AudioSegment

def cantDeleteLockedFile():

audiofile = "/Volumes/test/stereotest.wav"
audiostrip = AudioFileClip(audiofile)

if audiostrip.nchannels > 1:
with open(audiofile, "rb") as oaudiofile:
mono_audios = AudioSegment.from_file(oaudiofile, format="wav")

# List of AudioSegments, mono files, which can be accessed via [0] and [1]
mono_audios = mono_audios.split_to_mono()
audioChannelOne = str(audiofile.rsplit(".", 1)[0]) + "a.wav"

# This line is locking the stereo file
mono_left = mono_audios[0].export(audioChannelOne, format="wav")
# This extracts the mono left track from the stereo track
# On the same location a file will be created, in this example:
# "/Volumes/test/stereotesta.wav"

# This should unlock the file, but doesnt
mono_left.close()

# When trying to delete the file here, it will fail
# without exception raised
os.remove(audiofile)

if os.path.exists(audiofile):
return True

else:
return False

return False
执行此代码后,在我的例子中,它嵌入到 API 微服务系统中,不会退出代码。然后,只要该微服务正在运行,立体声音频文件就会被锁定。该文件不会被删除,函数返回值为“False”。如果您稍后在文件系统上手动导航到该文件并尝试手动删除它,它也会失败。它会先删除它,然后它会神奇地弹回来。
我知道这个问题之前在其他板上讨论过。但是,建议的解决方案不起作用。
引用: https://github.com/jiaaro/pydub/issues/305
要么我完全错过了一些东西。但是,也许有一种解决方法可以强制解锁文件,因此可以将其删除?我没有在网上找到引用资料。
基本上我知道,pydub 正在锁定资源,我无法让它解锁音频段后面的 wav 文件。
很高兴阅读您的反馈和建议。
谢谢!

最佳答案

用于检查立体声文件的 Audiosegment 也需要关闭。
这阻止了存储端的文件。
添加一个简单的:

audiostrip.close()
解决了这个问题。

关于python - pydub AudioSegment.export 正在锁定 smb 共享上的文件。无法删除该文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70202737/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com