使用 pydub 时访问被拒绝

问题描述 投票:0回答:2

我想用这个脚本将一个mp3文件转换成一个wav文件。

import pydub
from pydub import AudioSegment
pydub.AudioSegment.converter=r"C:\Users\sunha\ffmpeg-4.1-win64-static\bin"
sound = AudioSegment.from_mp3("筷子兄弟 - 小苹果.mp3")
sound.export("筷子兄弟 - 小苹果.wav", format="wav")

但问题是我的访问被拒绝了。

Traceback (most recent call last):

File "<ipython-input-1-5faa7bcb6b97>", line 1, in <module>
    runfile('C:/Users/sunha/project-001/untitled1.py', wdir='C:/Users/sunha/project-001')

File "E:\program\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

File "E:\program\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/sunha/project-001/untitled1.py", line 4, in <module>
    sound = AudioSegment.from_mp3("筷子兄弟 - 小苹果.mp3")

File "E:\program\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
    return cls.from_file(file, 'mp3', parameters=parameters)

File "E:\program\lib\site-packages\pydub\audio_segment.py", line 697, in from_file
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)

File "E:\program\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 143, in __init__
    super(SubprocessPopen, self).__init__(*args, **kwargs)

File "E:\program\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)

File "E:\program\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)

PermissionError: [WinError 5] 拒绝访问。

我怎样才能解决这个问题?

python python-3.x mp3 wav
2个回答
0
投票

我尝试了多种解决方案,对我有帮助,如下:

  • 重新启动计算机和/或 IDE,
  • 更新 Python,IDE,电脑,
  • pip install pydirectory(命令/Anaconda 提示符,以管理员身份运行)
  • 对文件应用读取权限,
  • 指定文件的路径,并在路径前添加'r',表示您要'读取'音频文件,示例在底部。

我希望这可以帮助您或至少其他遇到此错误的人。

祝你好运。

来源

示例,在与音频文件相同的文件夹中运行:

from pydub import AudioSegment
import os

os.chmod('audio2.mp3', 777)  #You might not need this

sound = AudioSegment.from_mp3(r"D:/audio.mp3")
#Put the 'r' in front of the filepath, to 'read' the audio file.
sound.export("audio.wav", format="wav")

0
投票

我遇到了同样的问题,并通过引用@artgue72 在Python Pydub 上的回答解决了权限被拒绝?

“我遇到了同样的问题,但使用此线程中的 GillHawk 解决方案修复了问题(与 Jondiepdoop 提供的链接相同)。我在 playback.py 文件的 _play_with_ffplay 函数中添加了 f.close() :”

def _play_with_ffplay(seg):
with NamedTemporaryFile("w+b", suffix=".wav") as f:
    f.close() # close the file stream
    seg.export(f.name, "wav")
    subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name])

另外,请检查文件夹路径添加到Window的环境变量中。

就我而言,我也不需要文件路径前的 r',例如:

sound = AudioSegment.from_file('C:\\temp\\audio1.mp3', format="mp3").
© www.soinside.com 2019 - 2024. All rights reserved.