我一直在嘗試在 python 腳本中運行 sox 但它不能輸出檔案并給了我 [errno2]
def AudioToSpectrogram(self, files, pixel_per_sec, height, width):
file_name = ("tmp_{}.png").format(random.randint(0, 100000))
command = "sox -V0 {} -n remix 1 rate 10k spectrogram -y {} -x {} -X {} -m -r -o {}".format(files, height, width, pixel_per_sex, file_name)
p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
Output, errors = p.communicate()
If errors:
Print(errors)
Image = Image.open(file_name)
Os.remove(file_name)
Return np.array(image)
這是它給出的錯誤
Exception: [errno2] No such file or Directory: 'tmp_47483.png'
我希望你能給我一些指點,因為我還是這個領域的新手,在此先感謝!
uj5u.com熱心網友回復:
假設tmp_47483.png實際上正在創建,問題很可能是命令將檔案放在不同的檔案夾中,而 Python 在當前作業目錄中找不到它。這
# manually set the full path (make sure the backslashes are escaped by putting two each)
file_name = f"C:\\Full\\Path\\To\\File\\tmp_{random.randint(0, 100000}.png"
# use the os module to join the path
base_dir = "C:\\Full\\Path\\To\\File"
file_name = os.path.join(base_dir, f"tmp_{random.randint(0, 100000}.png")
# if you want it to appear in the same folder as your script:
CWD = os.path.dirname(os.path.realpath(__file__)) # mostly fool-proof way of getting a script's Current Working Directory
file_name = os.path.join(CWD, f"tmp_{random.randint(0, 100000}.png")
試試這些,看看它們是否有幫助。如果沒有,請確保它command實際上正在作業并在某處輸出檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/487679.html
