在Python中使用FFMPEG时,是什么导致此错误?

发布于 2025-02-01 21:09:02 字数 2079 浏览 5 评论 0原文

我正在尝试将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)

I'm trying to convert MP3 to OGG but it doesn't work. What's the problem?
The paths to the audio files are correct. "ffmpeg.exe" is in the script directory.

Code snippet from the program:

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

However, this code works fine:

import ffmpeg

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

Error:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2025-02-08 21:09:02

@rotem

在INP = ffmpeg.input(AudioPath)之前添加Print(Audiopath)和Print(Destpath)(或使用调试器获取字符串),并将Audiopath和Destpath的值添加到您的问题中。错误必须是由于听力变态和Destpath的错误值

的结果

感谢您的提示!我查看了哪些路径传递到该功能并发现了问题。

事实证明,问题是一个变量。一切都与范围有关。该文件的路径未分配给AudioFolter,因为我没有告诉Python该变量是全局的。

之前:

audioFolder = selectedAudioFolder

之后:

global audioFolder
audioFolder = selectedAudioFolder

一切都起作用!

@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

Thanks for the tip! I looked at which paths are passed to the function and found the problem.

It turns out that the problem was a variable. It's all about scope. The path to the file was not assigned to audioFolder because I didn't tell Python that the variable was global.

Before:

audioFolder = selectedAudioFolder

After:

global audioFolder
audioFolder = selectedAudioFolder

And it all works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文