Search code examples
python-3.xaudioffmpegconvertersogg

What causes an this error when using FFMPEG in Python?


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)

Solution

  • @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!