Python subprocess: capture output of ffmpeg and run regular expression against it
Question: I have the following code

    
    
    import subprocess
    import re
    from itertools import *
    
    command = ['ffprobe', '-i', '/media/some_file.mp4']
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    text = p.stderr.read()
    retcode = p.wait()
    text = text.decode('utf-8')
    p = re.compile("Duration(.*)")
    
    num = 0 #for debugging
    for line in iter(text.splitlines()):
        print(str(num) + line) #for debugging
        m = p.match(str(line))
        if m != None:
            print(m.group(1))
    

When I look at the output there is a line that says "Duration" on it, however
it is not captured, print(m.group(1)) is never reached. If I change the text
variable to a hardcoded string of "Duration blahblah" I get " blahblah", which
is what I expect. It seems like the regex doesn't recognize the text coming
back from stderr. How can I get the text into a format that the regex will
recognize and match on?

* * *

I have come up with the following solution, should it help anyone else
attempting to capture duration from ffmpeg using python

    
    
    import subprocess
    import re
    
    command = ['ffprobe', '-i', '/media/some_file.mp4']
    p = subprocess.Popen(command, stderr=subprocess.PIPE)
    text = p.stderr.read()
    retcode = p.wait()
    text = text.decode('utf-8')
    p = re.compile(".*Duration:\s([0-9:\.]*),", re.MULTILINE|re.DOTALL)
    m = p.match(text)
    print(m.group(1))
    


Answer: 
    p = re.compile(r".*?Duration(.*)")
    

Try this.`match` starts from the begining while there may might be something
before `duration`.

