4

I get the error sometimes when downloading a video. For example, I can attempt to download the video and it will download, the next time I run the script that video will not download and the error will be thrown.

def search_youtube(song,randomString,playlist_name):
 results = YoutubeSearch(song, max_results=1).to_dict()
 for v in results:
    url = 'https://www.youtube.com' + v['link']
    print(url)

    _filename = song
    format_input = 'mp4'
    format_output = 'mp3'

    print("Downloading...." + ' ' + song)
    time.sleep(5)
    YouTube(url).streams.first().download(filename=randomString,output_path=playlist_name)
mruss24
  • 339
  • 1
  • 4
  • 13

3 Answers3

11

I have just fixed this issue. Follow these steps.

  1. Go to pytube directory in site-packages. If you're not sure where full location is use: pip show pytube3. You'll see location of site-packages.
  2. In site-packages/pytube folder opened from above location, you'll see a file called extract.py. Open that file in your IDE or text-editor.
  3. You'll see a function called apply_descrambler . Inside that function, in line 301 probably, you'll see something like parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
  4. Replace cipher in formats[i]["cipher"] with signatureCipher . Thus that line becomes, parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
  5. pytube3 should work fine now.
Saurav Pathak
  • 642
  • 5
  • 19
0

I have something similar to your code, I hope it helps:

import webbrowser
import pytube
from subprocess import call

key = input('Input what you want to download: ')
a_website = "https://www.youtube.com/results?search_query="+key
webbrowser.open_new(a_website)

open('url.txt', 'w')
call(['notepad', 'url.txt'])

start = input('Press enter when ready to download >>> ')
with open('url.txt','r') as f:
    urls = f.readlines()
number = len(urls)
num = 0
for url in urls:
    num += 1
    print('Downloading video',num,'of',number)
    yt = pytube.YouTube(url)
    stream = yt.streams.first()
    stream.download()

open('url.txt', 'w')
Ann Zen
  • 17,892
  • 6
  • 20
  • 39
0

I decided to skip the videos that couldn't be downloaded due to the error message.

except KeyError as e:
       print ('I got a KeyError - reason "%s"' % str(e))
       print ('skipping the song - : ' + song)
mruss24
  • 339
  • 1
  • 4
  • 13