Using subprocess to capture a progress line

Tim Johnson tim at akwebsoft.com
Tue Nov 10 17:47:56 EST 2015


Using python 2.7.6 on ubuntu 14.04 
The application in question is run with bash and gnome-terminal :

I've written a command-line "wrapper" for youtube-dl, executing
youtube-dl as a subprocess.

------------------------------------------------------------------
youtube-dl reports download progress on one line. I.E. the line is
overwritten numerous times with no carriage return until the
downloading is finished.
------------------------------------------------------------------

The following code runs the youtube-dl command and reports each line
as output by youtube-dl
###########
    p = subprocess.Popen(list(args), stderr=subprocess.STDOUT,
                         stdout=subprocess.PIPE)
    while True:
        line = p.stdout.readline()
        if not line:
            break
        tmp = line.strip()
        print tmp
###########

However this method not does show the download progress _until_ the
download is complete. 

To clarify : follows is output from my app running youtube-dl.
I've annotated the line in question with '###'

[youtube] ZIgnHPqp0Dk: Downloading webpage
[youtube] ZIgnHPqp0Dk: Downloading video info webpage
[youtube] ZIgnHPqp0Dk: Extracting video information
[youtube] ZIgnHPqp0Dk: Downloading js player en_US-vfljDEtYP
[youtube] ZIgnHPqp0Dk: Downloading DASH manifest
[download] Destination: Someday Soon - Judy Collins 1969.avi.m4a
### the line below is not seen until download is finished.
[download] 100% of 7.58MiB in 00:12.85KiB/s ETA 00:00
[ffmpeg] Correcting container in "Someday Soon - Judy Collins 1969.avi.m4a"
[ffmpeg] Destination: Someday Soon - Judy Collins 1969.avi.mp3
Deleting original file Someday Soon - Judy Collins 1969.avi.m4a (pass -k to keep)

Is there a way to code subprocess so the the progress is being
shown? In my case lines being output aren't being shown unless a
newline is sent, or so I understand it.

FYI : the need for this function in this case is trivial, but the
solution will be enlightening for me and have other uses, I'll bet.

thanks

-- 
Tim 
http://www.akwebsoft.com, http://www.tj49.com



More information about the Python-list mailing list