在 Python 中使用 FFMPEG 时出现此错误的原因是什么?

What causes an this error when using FFMPEG in Python?

我正在尝试将 MP3 转换为 OGG,但它不起作用。有什么问题? 音频文件的路径是正确的。 “ffmpeg.exe”在脚本目录中。

程序中的代码片段:

def ProcessAudio(audioPath, destPath):
   inp = ffmpeg.input(audioPath)
   au = inp.audio
   stream = ffmpeg.output(au, destPath)
   ffmpeg.run(stream)

def Convert(listofmusic, pathofmsc, pathofdest, append):
   count = 0
   if len(listofmusic) >= 100:
      for i in range(100):
         count += 1
         out = mscPath + "/" + pathofdest + "/" + "track" + str(count) + ".ogg"
         print(out)
         ProcessAudio(audioFolder + "/" + listofmusic[i], out)
         break
      count = 0
   elif len(listofmusic) < 100:
      for i in range(len(listofmusic)):
         count += 1
         mscP = mscPath.replace("/", "\")
         out = mscP + "\" + pathofdest + "\" + "track" + str(count) + ".ogg"
         print(out)
         audioProc = audioFolder + "\" + listofmusic[i]
         print(audioProc)
         ProcessAudio(audioProc, out)
         break
      count = 0

但是,这段代码工作正常:

import ffmpeg

inputfile = ffmpeg.input("example.mp3")
iAudio = inputfile.audio
stream = ffmpeg.output(iAudio, "example.ogg")
ffmpeg.run(stream)

错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Santila\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:\Users\Santila\Desktop\MSC_Audio_Converter.py", line 75, in pressed
    Convert(musicList, mscPath, oggFolder, cbVar.get())
  File "C:\Users\Santila\Desktop\MSC_Audio_Converter.py", line 52, in Convert
    ProcessAudio(audioProc, out)
  File "C:\Users\Santila\Desktop\MSC_Audio_Converter.py", line 32, in ProcessAudio
    ffmpeg.run(stream)
  File "C:\Users\Santila\AppData\Local\Programs\Python\Python310\lib\site-packages\ffmpeg\_run.py", line 325, in run
    raise Error('ffmpeg', out, err)
ffmpeg._run.Error: ffmpeg error (see stderr output for detail)

@Rotem

Add print(audioPath) and print(destPath) before inp = ffmpeg.input(audioPath) (or use the debugger to get the strings), and add the values of audioPath and destPath to your question. The error must be result of wrong values of audioPath and destPath

感谢您的提示!我查看了传递给函数的路径并发现了问题。

原来是变量的问题。一切都与范围有关。文件路径未分配给 audioFolder,因为我没有告诉 Python 该变量是全局变量。

之前:

audioFolder = selectedAudioFolder

之后:

global audioFolder
audioFolder = selectedAudioFolder

一切正常!