0

I am using pydub to convert a mp3 file to wav. I switch from Atom to PyCharm and now is throws following error.

C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\site-packages\pydub\utils.py:193: RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work
  warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)
Traceback (most recent call last):
  File "speechrec.py", line 102, in <module>
    main()
  File "speechrec.py", line 36, in main
    recognize()
  File "speechrec.py", line 51, in recognize
    current = AudioSegment.from_mp3("./rec/ready.mp3")
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
    return cls.from_file(file, 'mp3', parameters=parameters)
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\site-packages\pydub\audio_segment.py", line 665, in from_file
    info = mediainfo_json(orig_file)
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\site-packages\pydub\utils.py", line 263, in mediainfo_json
    res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\BlakkM9\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

Before switching I just typed python start.py in the PowerShell when being in the root directory of the project. This still works without errors.

The corresponding code:

from pydub import AudioSegment

AudioSegment.converter = os.path.dirname(os.path.abspath(__file__)) + "\\ffmpeg\\bin\\ffmpeg"

current = AudioSegment.from_mp3("./rec/ready.mp3")
current.export("./rec/current.wav", format="wav")

Launch options in PyCharm enter image description here

Thanks

Blakk

EDIT:

Using an absolute path to the mp3 file is not working aswell (even if it works when I test it with open(...) two lines above.

printing AudioSegment.converter also is the same in PyCharm and PowerShell.

BlakkM9
  • 400
  • 4
  • 17
  • Where is the mp3 file in relation to the python file? It seems as if python cant find `./rec/ready.mp3`. It is looking for `E:\C\Users\BlakkM9\Documents\Dokumente\Python\rec\ready.mp3`. Can you verify that the file is there? – Alex Jul 13 '19 at 18:34
  • no its in the root directory but i tried ``open("./rec/ready.mp3")`` some lines above and this works. When i use an absolute path to the file it also opens with ``open`` but the error still occues. I think that this has something to do with ffmpeg because it also complains about not finding ffprobe. This is also not the case when started via powershell. But that also makes no sense because i use an absolute path for ffmpeg. – BlakkM9 Jul 13 '19 at 18:58
  • Maybe try pointing it at `"\\ffmpeg\\bin\\ffmpeg.exe"`? – Alex Jul 13 '19 at 19:05
  • yeah tried that aswell and it didn't fix it. i changed the path variable to ffmpeg and restarted my computer and not it's no longer working even in the powershell. this really messes me up. – BlakkM9 Jul 13 '19 at 19:30

1 Answers1

0

I solved my problem now by just implementing it myself. No idea what was the problem. Use the code on your own risk, because I'm pretty new to python.

import os
import subprocess

curr_path = os.path.abspath(".")

def ffmpeg(input_file, output_file):
    # convert to absolute paths if necessary
    if os.path.isabs(input_file):
        input_path = input_file
    else:
        input_path = curr_path + input_file

    if os.path.isabs(output_file):
        output_path = output_file
    else:
        output_path = curr_path + output_file

    subprocess.check_call(curr_path + "/ffmpeg/bin/ffmpeg.exe -i " +
                          input_path + " " +
                          output_path,
                          stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)

ffmpeg output is disabled and the ffmpeg folder needs to be placed in the same directory as the script that the function is in if not changed.

BlakkM9
  • 400
  • 4
  • 17