0

I'm using Python 3.x on Windows 10. I'm using a subprocess to execute a command to use FFmpeg to create a slideshow from some images. It normally works fine except for when the output file already exists. When this happens I get a message on stderr prompting the user if it should overwrite. This message does not end in a newline. Here's the code I'm currently using

with subprocess.Popen([FFMPEG_PATH, 
'-i', 'img_%03d.png',
'-vf', vfCommand,
'-hide_banner',
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'{0}'.format(vidOutputPath)]
, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as outProcess:
    while outProcess.poll() is None:
        data = outProcess.stdout.read(1)
        data = data.decode()
        print(data, end='')

Using print(data, end='') on the last line I will see everything nicely formatted but it will not show the "File exists overwrite [y/N]" prompt. whereas if just do a print(data) then the prompt will show, just one character on a line.

My end goal is to show the command and allow a user to respond to it. I do not want to use shell=true as that opens up the program to injection issue and if possible I don't want to do anything where I have to install/download additional packages for python. I'd like to keep this where even a basic user could install python and immediately use the program.

Abishur
  • 189
  • 3
  • 13
  • I don't feel like this was a duplicate question even though they had the same answer as their question was specifically about print while mine was specifically about the subprocess function. It just turned out that the print function was my culprit. – Abishur Sep 07 '19 at 15:44
  • That said, Thank you @Aran-Fey for pointing me in the right direction. Code now reads `with subprocess.Popen([FFMPEG_PATH, '-i', 'img_%03d.png', '-vf', vfCommand, '-hide_banner', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '{0}'.format(vidOutputPath)] , stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as outProcess: while outProcess.poll() is None: data = outProcess.stdout.read(1) print (data.decode(), end='', flush=True)` – Abishur Sep 07 '19 at 15:45
  • If you want I can dig up a duplicate for "reading real-time output from a process", too... – Aran-Fey Sep 07 '19 at 15:55
  • yeah I looked at several of those. All the ones I found had solutions where they used shell=true or installed additional packages as a work around. Spent a lot of time over the past few days reading around stackoverflow and trying different solutions and feel like the question was unique even if I ultimately was asking the wrong question and the answer was not unique. – Abishur Sep 07 '19 at 16:24
  • 1
    I do not at all see how either of the links answer this question. I was so happy to see that someone else asked this question and then dismayed that the pedantic SO community closed it as a duplicate after linking some hard to interpret and only tangentially related questions. – rocksNwaves Oct 24 '20 at 18:52

0 Answers0