0

I'm having a similar issue on Python 3.8.7, and I can't find the solution.

In my project, I'm using pydub.AudioSegment to get audio from a file and then exporting that audio in a different format. My code works perfectly when I'm running the python file directly. However, when I convert it to an executable with pyinstaller, run the program and get to the point of importing the audio with pydub, it gives me the following error:

Traceback (most recent call last):
  File "main.py", line 269, in <module>
  File "main.py", line 213, in convertfile
  File "main.py", line 133, in cloud_upload
  File "pydub\audio_segment.py", line 728, in from_file
  File "pydub\utils.py", line 274, in mediainfo_json
  File "subprocess.py", line 804, in __init__
  File "subprocess.py", line 1142, in _get_handles
OSError: [WinError 6] The handle is invalid

pydub call in my program looks like this:

sound = AudioSegment.from_file(filepath)
sound.export(new_filepath, format="ogg",codec='libopus')

I've tried to add stdin=subprocess.DEVNULL and stdin=subprocess.PIPE in utils.py on line 274, but that didn't work either. Maybe I added them incorrectly, though, so suggestions like that are also highly appreciated.

SevaOle
  • 75
  • 1
  • 6
  • 1
    I assume its has something to do whenever the `export` method tries to call the ffmpeg via `subprocess`. Can you add manually the `shell=True` to this line: https://github.com/jiaaro/pydub/blob/master/pydub/audio_segment.py#L963 – Dimitar Apr 11 '21 at 20:42
  • @Dimitar, thanks for a quick response! I did it but sadly, I get the same error. – SevaOle Apr 11 '21 at 20:50
  • I don't know if it has something to do with it, but also I don't have ffmpeg in my PATH. I have the ffmpeg.exe, ffplay.exe and ffprobe.exe in my root directory along with the main script. I also add those exe files to my program in pyinstaller with --add-data. – SevaOle Apr 11 '21 at 20:57
  • 1
    That could be a reason too, could you add them to your PATH and try again? Also make sure they are there by invoking them beforehand and don't add them with --add-data – Dimitar Apr 11 '21 at 20:58
  • 1
    I just tried it on my own and I don't see any problems with it. I have `ffmpeg` in my path the only thing that is different is that I didn't specify the codec in the export. Using python 3.9.1, pydub 0.25.1 and pyinstaller 4.2 – Dimitar Apr 11 '21 at 21:10
  • It's weird but it started working without --onefile in pyinstaller. It works both with shell = True and without it. I've also added ffmpeg to PATH, but it's not working with --onefile anyways. – SevaOle Apr 11 '21 at 22:12

2 Answers2

2

I've managed to solve the problem only by removing --onefile option from pyinstaller and dropping ffmpeg.exe and ffprobe.exe into the resulted folder with the main.exe file.

That's not a good solution to the problem as I'd still prefer to use --onefile; but it works.

I'm still open to suggestions on how to make it work with --onefile or just generally why this is hapenning.

SevaOle
  • 75
  • 1
  • 6
1

Chances are is that PyInstaller cannot recognize the imported plugin. Although if you are trying to make a app, I suggest using a shortcut instead, it is better due to the fact that you can customize the icon for the shortcut. It is what many apps mainly use.

Dharman
  • 30,962
  • 25
  • 85
  • 135