I'm writing a program that downloads videos from YouTube, using youtube-dl
.
I used to call youtube-dl with subprocess
:
import subprocess
p = subprocess.Popen([command], \
stdout=subprocess.PIPE, \
stderr=subprocess.STDOUT, \
universal_newlines = True)
Then, I would read the process' output by calling:
for line in iter(p.stdout.readline, ""):
hide_some_stuff_using_regex()
show_some_stuff_using_regex()
However, I prefer using youtube-dl
as a Python class. So I'm now doing this:
from youtube_dl import YoutubeDL as youtube_dl
options = {"restrictfilenames": True, \
"progress_with_newline": True}
ydl = youtube_dl(options)
ydl.download([url])
The code works, but I'm having a hard time finding out, how to pipe youtube-dl
's output. Note that I want to use parts of youtube-dl's output to print in real-time, so redirecting sys.stdout
to a custom output stream will not work, as I still need sys.stdout to print.
Can you help me?