Search code examples
python-2.7ffmpegpygtk

Python subprocess: Know when a command has finished


I need a way to know when ffmpeg has finished its work. Here is my code:

def on_button_clicked(self, button, progress_bar, filename):
  self.execute(progress_bar, filename)
  progress_bar.set_text('Encoding')
  progress_bar.pulse()

def execute(self, progress_bar, filename):

  cmd = ['ffmpeg', '-y',
         '-i', filename,
         '-r', '30',
         '/tmp/test-encode.mkv']

  process = sp.Popen(cmd, stdin=open(os.devnull))

  progress_bar.set_text('Done')

The 'Done' never shows up. The job is done, though. I can see in the shell window that ffmpeg is done. How can I get the signal?


Solution

  • Here is how I ended up doing it:

        p = sp.Popen(command, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
    
        def mycallback(p):
            p.poll()
            if p.returncode is None:
                # Waiting for Godot
                return True
            else:
                # Yay, wait is over!
                return False
    
        GObject.timeout_add(1000, mycallback, p)