Can't figure out why PyDub is not working

3.3k Views Asked by At

I'm trying to use pydub for a music project, but when trying to play sounds with this chunk of code

from pydub import AudioSegment
from pydub.playback import play
sound = AudioSegment.from_wav("s1.wav")
play(sound)

i get the following error:

RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
C:\Python\Python385\lib\site-packages\pydub\utils.py:184: RuntimeWarning: Couldn't find ffplay or avplay - defaulting to ffplay, but may not work
  warn("Couldn't find ffplay or avplay - defaulting to ffplay, but may not work", RuntimeWarning)
Traceback (most recent call last):
  File "C:/Users/vicen/Desktop/music project/mian.py", line 6, in <module>
    play(s1)
  File "C:\Python\Python385\lib\site-packages\pydub\playback.py", line 74, in play
    _play_with_ffplay(audio_segment)
  File "C:\Python\Python385\lib\site-packages\pydub\playback.py", line 18, in _play_with_ffplay
    seg.export(f.name, "wav")
  File "C:\Python\Python385\lib\site-packages\pydub\audio_segment.py", line 809, in export
    out_f, _ = _fd_or_path_or_tempfile(out_f, 'wb+')
  File "C:\Python\Python385\lib\site-packages\pydub\utils.py", line 60, in _fd_or_path_or_tempfile
    fd = open(fd, mode=mode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\vicen\\AppData\\Local\\Temp\\tmpvwotqts5.wav'

Does someone understand why it isn't working? I am fairly new to python so i don't.

3

There are 3 best solutions below

0
On

The python script encountered a Permission Error It is trying to read 'C:\\Users\\vicen\\AppData\\Local\\Temp\\tmpvwotqts5.wav' file but doesn't have permission to write in the directory.

Changing permissions on the above mentioned Temp folder should solve the problem.

Or you could run your python script using sudo command. Since you are using windows this should help in this regard.

0
On

Easy Fix from here:

pip install simpleaudio

Pydub uses tempfiles extensively .As suggested here you can add TMPDIR environment variable.

0
On

the problem is with the tempfile , as mentioned here
so the file playback.py , which is one of the files of this pydub module (you may find it at Python\Python-version-\Lib\site-packages\pydub ) , must be modified.
there are two recommended methods to solve this,

  1. mentioned here

  2. create a custom tempfile like below at playback.py

`

import subprocess
from tempfile import NamedTemporaryFile
from .utils import get_player_name, make_chunks
import random
import os
import tempfile


class CustomNamedTemporaryFile:
    """
    This custom implementation is needed because of the following limitation of tempfile.NamedTemporaryFile:

    > Whether the name can be used to open the file a second time, while the named temporary file is still open,
    > varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
    """
    def __init__(self, mode='wb', delete=True, suffix = ''):
        self._mode = mode
        self._delete = delete
        self.suffix = suffix

    def __enter__(self):
        # Generate a random temporary file name
        file_name = os.path.join(tempfile.gettempdir(), os.urandom(24).hex())
        # Ensure the file is created
        open(file_name+self.suffix, "x").close()
        # Open the file in the given mode
        self._tempFile = open(file_name+self.suffix, self._mode)
        return self._tempFile

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._tempFile.close()
        if self._delete:
            os.remove(self._tempFile.name)
            
def _play_with_ffplay(seg):
    PLAYER = get_player_name()
    # with NamedTemporaryFile("w+b", suffix=".wav") as f:
    with CustomNamedTemporaryFile(mode='wb', suffix = ".wav") as f:
        seg.export(f.name, "wav")
        subprocess.call([PLAYER, "-nodisp", "-autoexit", "-hide_banner", f.name])

'