簡體   English   中英

無法弄清楚為什么 PyDub 不起作用

[英]Can't figure out why PyDub is not working

我正在嘗試將 pydub 用於音樂項目,但是在嘗試使用這段代碼播放聲音時

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

我收到以下錯誤:

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'

有人明白為什么它不起作用嗎? 我對 python 相當陌生,所以我不知道。

python 腳本遇到Permission Error它正在嘗試讀取'C:\\Users\\vicen\\AppData\\Local\\Temp\\tmpvwotqts5.wav'文件,但沒有在目錄中寫入的權限。

更改上述臨時文件夾的權限應該可以解決問題。

或者您可以使用sudo命令運行 python 腳本。 由於您使用的是 windows 在這方面應該有所幫助。

從這里輕松修復:

pip install simpleaudio

Pydub廣泛使用臨時文件。如此處建議您可以添加TMPDIR環境變量。

問題出在臨時文件上,如此所述
所以文件playback.py ,它是這個pydub模塊的文件之一(你可以在Python\Python-version-\Lib\site-packages\pydub找到它),必須被修改。
有兩種推薦的方法來解決這個問題,

  1. 這里提到

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

'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM