2

I want to use this script to convert a mp3 file to a wav file.

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")

But the problem is my access is denied.

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] 拒绝访问。

How can I possibly fix the problem?

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
无名氏
  • 21
  • 1

2 Answers2

0

I tried out multiple solutions, which helped me, as follows:

  • Rebooting the computer and/or IDE,
  • Updating Python, IDE, computer,
  • pip install pydirectory (Command/Anaconda prompt, run as administrator)
  • Applying read permissions to the file,
  • Specifying the path to the file, and adding 'r' before the path, to indicate you want to 'read' the audio file, example at the bottom.

Source

Example, run in the same folder as the audio file:

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")
halfer
  • 19,824
  • 17
  • 99
  • 186
Nindui
  • 21
  • 3
0

I had the same problem, and resolved by referencing @artgue72's answer on Python Pydub Permission denied?:

I had the same issue but fixed using the GillHawk solution from this thread (same link as Jondiepdoop gave). I added f.close() in the _play_with_ffplay function of the playback.py file:

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])

In addition, please check the folder path is added to Window's Environmental Variables.

In my case, I also need no r string literal symbol before the file path, like:

sound = AudioSegment.from_file('C:\\temp\\audio1.mp3', format="mp3").
halfer
  • 19,824
  • 17
  • 99
  • 186
Mark K
  • 8,767
  • 14
  • 58
  • 118