1

Until recently I've been using python 3.63. When I need to use Pydub's audio_segment I do it like this to avoid a flash of the console when the app is frozen in a pyinstaller exe:

subprocess.STARTUPINFO.dwFlags |= subprocess.STARTF_USESHOWWINDOW
audio = AudioSegment.from_file('path_to_file')

Since moving to Python 3.10 this creates the error:

AttributeError: type object 'STARTUPINFO' has no attribute 'dwFlags'

I have tried adding options like:

creationflags=subprocess.CREATE_NO_WINDOW

and

startupinfo = subprocess.STARTUPINFO()  
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
p = subprocess.Popen(conversion_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo)

to pydub itself, but the flash of console is always there. I've Googled for hours and got nowhere...

Edgar
  • 35
  • 5
  • `subprocess.STARTUPINFO.dwFlags` is strange. You need an instance of the object to change dwFlags. Maybe it's become an instance variable, not a class variable. – Jean-François Fabre Nov 15 '22 at 15:54
  • maybe you should handle that when creating the app: https://stackoverflow.com/questions/17584698/getting-rid-of-console-output-when-freezing-python-programs-using-pyinstaller – Jean-François Fabre Nov 15 '22 at 15:55
  • --noconsole setting in Pyinstaller prevents the console appearing when I launch the script but it doesn't stop a flash of the console each time pydub is called. – Edgar Nov 15 '22 at 18:40
  • The suggestion of using the dwFlags line came from here: https://stackoverflow.com/a/49111811/10069936 – Edgar Nov 15 '22 at 18:45
  • .. which is an answer with 0 score. – Jean-François Fabre Nov 15 '22 at 21:02
  • but works perfectly under Python 36... whereas the scoring answers don't! – Edgar Nov 15 '22 at 21:34
  • you mean: when your code is frozen with pyinstaller, the window pops up right? What's the command you're running in `Popen` ? – Jean-François Fabre Nov 15 '22 at 21:55
  • Detailed in original post... – Edgar Nov 17 '22 at 07:45
  • no. For instance `conversion_command` is not defined. Would it be possible for you do create a [mcve] ? Else I fear that your issue will never be solved. – Jean-François Fabre Nov 17 '22 at 09:08
  • conversion_command is part of the popen line inside pydub. I am not calling popen directly, all I am doing is: 'AudioSegment.from_file("path_to_file")'. I could attempt a minimal reproducible example but it will be a little messy for you to set up! – Edgar Nov 17 '22 at 15:17
  • aah problem is probably on pydub side. Have you tried to package your file with a ".pyw" extension instead of ".py" ? – Jean-François Fabre Nov 17 '22 at 15:27
  • Yes, one of the first things I tried. All pydub does is launch ffmpeg via subprocess. If I launch ffmpeg myself via subprocess I don't get the problem. Grr! I really need the functionality that pydub gives. – Edgar Nov 18 '22 at 08:56

1 Answers1

1

I finally got to the bottom of this. A module of pydub called utils.py contains a couple of subprocess calls. I changed the calls from:

command = [prober] + command_args
output = Popen(command, stdout=PIPE).communicate([0].decode("utf-8")

To:

command = [prober] + command_args
startupinfo = subprocess.STARTUPINFO()  
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
res = subprocess.Popen(command, startupinfo=startupinfo, 
      stdout=subprocess.PIPE, stderr=subprocess.PIPE)

I did the same in the audio_segment.py module. Plus of course changed: from subprocess import popen, PIPE to import subprocess

Edgar
  • 35
  • 5