1

I wrote this function to take the subprocess output line-by-line, which then yields each line and sends it to the user.

def invoke_process_popen_poll_live(command, shellType=False, stdoutType=subprocess.PIPE):
    """runs subprocess with Popen/poll so that live stdout is shown"""
    try:
        process = subprocess.Popen(command, shell=shellType, stdout=stdoutType, universal_newlines=True)
    except:
        print("ERROR {} while running {}".format(sys.exc_info()[1], command))
        return None
    while True:
        output = process.stdout.readline()
        yield output.strip()
        if process.poll() is not None:
            break
        if output:
            output.strip()
    rc = process.poll()
    return rc

The output is the youtube-dl download stats which is like this:

[youtube] knge_1uWbro: Downloading webpage
[download] Destination: ./DLVids/Kitty_says_Uh-Oh_very_clearly-knge_1uWbro-720.f398.mp4

[download]   0.1% of 963.52KiB at  3.82KiB/s ETA 04:11
[download]   0.3% of 963.52KiB at 11.46KiB/s ETA 01:23
[download]   0.7% of 963.52KiB at 26.61KiB/s ETA 00:35\n
.
.
.

Is there any way to take each line's output like this:

0.1% of 963.52KiB at  3.82KiB/s
0.3% of 963.52KiB at 11.46KiB/s
0.7% of 963.52KiB at 26.61KiB/s
Ali Abdi
  • 408
  • 7
  • 21
  • while this question still seems interesting, specifically for youtube-dl, it's written in Python and can be imported and controlled directly https://stackoverflow.com/questions/18054500/how-to-use-youtube-dl-from-a-python-program – ti7 Feb 08 '22 at 16:37
  • @ti7 thanks, I'm aware of it but I've written my whole app in the hard way (though for me, importing was hard too! there was a little doc) just don't know how to send the download stat to the user. – Ali Abdi Feb 08 '22 at 16:43

0 Answers0