Python Forum

Full Version: Input Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,im trying to do a youtube video downloader script,so i did this:
import pytube
video_url = 'https://www.youtube.com/watch?v=4KcMdH8qiuk'
youtube = pytube.YouTube(video_url)
video = youtube.streams.first()
video.download('/home/dream/Downloads')


but i dont want to put the url in the code,I want the user to insert it when the program is started,so i did
import pytube
video_url = input()
youtube = pytube.YouTube(video_url)
video = youtube.streams.first()
video.download('/home/dream/Downloads')
but this return me error:
Error:
Traceback (most recent call last): File "/home/dream/.local/lib/python3.8/site-packages/pytube/extract.py", line 288, in apply_descrambler stream_data[key] = [ File "/home/dream/.local/lib/python3.8/site-packages/pytube/extract.py", line 290, in <listcomp> "url": format_item["url"], KeyError: 'url' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "youtube2.py", line 3, in <module> youtube = pytube.YouTube(video_url) File "/home/dream/.local/lib/python3.8/site-packages/pytube/__main__.py", line 92, in __init__ self.descramble() File "/home/dream/.local/lib/python3.8/site-packages/pytube/__main__.py", line 132, in descramble apply_descrambler(self.player_config_args, fmt) File "/home/dream/.local/lib/python3.8/site-packages/pytube/extract.py", line 300, in apply_descrambler cipher_url = [ File "/home/dream/.local/lib/python3.8/site-packages/pytube/extract.py", line 301, in <listcomp> parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats) KeyError: 'cipher'
It maybe be a bug with the library but I've never used it so I can't say for certain.

Have you thought about using other libraries? There's a very popular one called youtube-dl. It supports hundreds of sites including youtube. It has command line support as well. There is very many good python tutorials on it. https://www.bogotobogo.com/VideoStreamin...edding.php
The site provides this code:
# ydl1.py
from __future__ import unicode_literals
import youtube_dl

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=dP15zlyra3c'])
You would need to modify it to have user input but that should't be too hard. If you read up on it, there are also many options you can provide like whether you want audio, the output file type and so on.
Worked fine for me. I added a line to show what URL was read.

import pytube
video_url = input("Enter url: ")
print(f"Downloading {video_url}")
youtube = pytube.YouTube(video_url)
video = youtube.streams.first()
video.download('tube.out')
Output:
Enter url: https://www.youtube.com/watch?v=4KcMdH8qiuk Downloading https://www.youtube.com/watch?v=4KcMdH8qiuk
And it downloads...