I am using python to run Youtube-dl, a software used to download videos from YouTube and other sites. I am using it specifically to download the audio from YouTube videos. I am able to download the audio from a YouTube video using python by using check_output() to execute the youtube-dl.exe file saved on my computer.
Below is the method I wrote to execute the youtube-dl file and download a song. It returns a huge string of text that is the output from the youtube-dl program.
from subprocess import check_output
#downloads the audio from a song to wherever the python script is
def getSong(songUrl):
return check_output('C:\Users\Ben\Desktop\youtube-dl\youtube-dl.exe --max-quality FORMAT --extract-audio ' + songUrl, shell=True)
When I run this method in python, it will wait for the command to be completed, then return a huge response containing lots of information. This includes generic info outputted at the end of the download, but it also (if run through command prompt) will give out info about the download progress every few seconds. The problem is, when running it in python, I get all of this progress information after it is finished downloading.
Is there any way to get the output from the check_output() method while it is still running? that way I would be able to make a progress bar for the download.
EDIT:
update: below is the current code that I have gotten to work as I want to except for one small exception...
import subprocess
songUrl = "http://www.youtube.com/watch?v=uO3lN-hugaQ"
process = subprocess.Popen(["C:\Users\Ben\Desktop\youtube-dl\youtube-dl.exe", "--max quality", "FORMAT", "--extract-audio", songUrl], shell=True, stdout = subprocess.PIPE)
for line in iter(process.stdout.readline, b''):
print line,
process.communicate()
When I run this new above code, it will start to print out information line by line as it is generated in the executed command, but all of the information regarding percent downloaded is all printed out at the very end. When I tried running the same command in command prompt, I discovered that the text that gives information about percent downloaded is actually changed every second or so, rather than a new line being created with updated information, as I suspected was true based on the output in python. Do you think there is any way around this, so I can get the information necessary for a progress bar?
Use Popen instead. Then you can do something like the following:
import subprocess
process = subprocess.Popen(["C:\Users\Ben\Desktop\youtube-dl\youtube-dl.exe", "--max-quality", "FORMAT", "--extract-audio", "songUrl"], shell=True, stdout = subprocess.PIPE)
while process.poll() is None:
result = process.stdout.read()
# do some calculations for percentage done
Also as a general rule of thumb, you should avoid using shell=True to prevent potential security vulnerabilities.