0

I'm trying to download my video playlist and convert it to mp3 extension using pytube,moviepy,re. Everything works but when it hits a number it stops working.

How can I fix this?

from pytube import YouTube
from pytube import Playlist
import os
import moviepy.editor as mp #to convert the mp4 to wav then mp3
import re

playlist = Playlist("https://www.youtube.com/playlist?list=PLb2p41g_hNVOeBy3OjGTdXKgscedya9f_")

for url in playlist:
    print(url)
for vid in playlist.videos:
    print(vid)
for url in playlist:
    YouTube(url).streams.filter(only_audio=True).first().download("./Downloads/Music_2")
folder = "./Downloads/Music_2"
for file in os.listdir(folder):
    if re.search('mp4', file):
        print("Converting: " + file)
        mp4_path = os.path.join(folder,file)
        mp3_path = os.path.join(folder,os.path.splitext(file)[0]+'.mp3')
        new_file = mp.AudioFileClip(mp4_path)
        new_file.write_audiofile(mp3_path)
        os.removed(mp4_path)

Error Code;

Traceback (most recent call last):
  File "C:\Users\user\Downloads\Projects\Python\main.py", line 14, in <module>
    YouTube(url).streams.filter(only_audio=True).first().download("./Downloads/Music_3")
    ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Downloads\Projects\Python\venv\Lib\site-packages\pytube\__main__.py", line 296, in streams
    return StreamQuery(self.fmt_streams)
                       ^^^^^^^^^^^^^^^^
  File "C:\Users\user\Downloads\Projects\Python\venv\Lib\site-packages\pytube\__main__.py", line 176, in fmt_streams
    stream_manifest = extract.apply_descrambler(self.streaming_data)
                                                ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Downloads\Projects\Python\venv\Lib\site-packages\pytube\__main__.py", line 161, in streaming_data
    return self.vid_info['streamingData']
           ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
KeyError: 'streamingData'

Process finished with exit code 1
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Mert K.
  • 3
  • 2

3 Answers3

0

I was able to reproduce your issue with the latest pytube version.

More precisely these video ids failed to be downloaded on first trial. However after another trial only the following video ids are still not available for download: bFfSn7RKgwE, pYEwoUgKhV8 and UtF6Jej8yb4. pYEwoUgKhV8 is a members-only video so you are obliged to pay to access it and bFfSn7RKgwE and UtF6Jej8yb4 are just age-restricted, you can access these videos with yt-dlp for instance.

So there is no pytube issue here.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
0

Just try this one - This will download the entire playlist as webm which basically can be played as mp3, therefor I'm setting the outtmpl as TITLE.mp3 (to the current folder)

enjoy :)

from yt_dlp import YoutubeDL

if __name__ == '__main__':
    video_url = input("Please insert the URL: ")
    video_info = YoutubeDL().extract_info(url=video_url, download=False)
    options = {
        'format': 'bestaudio/best',
        'keepvideo': False,
        'outtmpl': '%(title)s.mp3',
        # 'playlist_items': '2'
    }
    with YoutubeDL(options) as ydl:
        out = ydl.download([video_info['webpage_url']])
Ofir Nagadi
  • 181
  • 1
  • 5
-1

I tried. The script was fine, until the last line:

 os.removed(mp4_path)

It should be os.remove(mp4_path)

*Without the 'd'

  • 1
    In original code execute fix `YouTube(url).streams.filter(only_audio=True).first().download(output_path="./Downloads/Music_2")` And line `os.remove(mp4_path)` – sercheo_87 Feb 26 '23 at 02:59
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 27 '23 at 15:11