问题
I am making an API
for youtube-dl in tkinter
& python
and need to know:
- How to get the info dict from youtube-dl in realtime (speed, percentage finished, file size, etc.) ??
I have tried:
import subprocess
def execute(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
# Poll process for new output until finished
while True:
nextline = process.stdout.readline()
if nextline == '' and process.poll() != None:
break
sys.stdout.write(nextline.decode('utf-8'))
sys.stdout.flush()
output = process.communicate()[0]
exitCode = process.returncode
if (exitCode == 0):
return output
else:
raise ProcessException(command, exitCode, output)
execute("youtube-dl.exe www.youtube.com/watch?v=9bZkp7q19f0 -t")
from this Question
But it had to wait until finishing downloading to give me the info; maybe there is a way of getting the info from the youtube-dl source code.
回答1:
Try something like this:
from youtube_dl import YoutubeDL
video = "http://www.youtube.com/watch?v=BaW_jenozKc"
with YoutubeDL(youtube_dl_opts) as ydl:
info_dict = ydl.extract_info(video, download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
You may have figured this out by now, but it might help someone else.
回答2:
- Better to avoid using
subprocess
; you can use the module directly as usual python module; refer to this : use youtube-dl module this require downloading the source code not only installing the application to system. - To continue using
subprocess
; you should add the following arguments:
Verbosity / Simulation Options:
-q, --quiet activates quiet mode
-s, --simulate do not download the video and do not write anything to disk
--skip-download do not download the video
-g, --get-url simulate, quiet but print URL
-e, --get-title simulate, quiet but print title
--get-thumbnail simulate, quiet but print thumbnail URL
--get-description simulate, quiet but print video description
--get-filename simulate, quiet but print output filename
--get-format simulate, quiet but print output format
- for your code; Ithink the error in the line of return, you choose to return the last line of the
sys.output
, returned by communicate; I suggest this simple untested example:
def execute(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
#Poll process for new output until it is finished
while True:
nextline = process.stdout.readline()
if nextline == '' and process.poll() != None:
break
yield nextline
I have used it in :
for i in execute("sudo apt-get update"):
print i
In all conditions, don't foreget to update your version.
来源:https://stackoverflow.com/questions/23727943/how-to-get-information-from-youtube-dl-in-python