Search code examples
pythonaudioyoutube

My python script to download a youtube video's audio doesn't work as intended


I'm trying to make this python script work:

The script should download the youtube video link's Audio, the link is saved in a .txt file in the same folder as the script

when i run the script it doesn't do anything has anyone got a clue?

import pafy

# opening the text file which contains all the links
file = open('list.txt', 'r')

# loop through each link in the file
for line in file:

    # Assign link to "url" variable
    url = line

    try:
        # Passing link to pafy
        video = pafy.new(url)

        # extracting the best available audio
        bestaudio = video.getbestaudio()
        print(video.title)

        # downloading the extracted audio
        bestaudio.download()

    # move to next link if the video is removed in the youtube platform
    except:
        pass

# close the text file
file.close()

The youtube video i watched to get this code is: "https://www.youtube.com/watch?v=ob8pSr9Vxs0&ab_channel=AkshayKGowda"

The scrpit is not downloading any file even if the .txt contains valid youtube links


Solution

  • I made it!

    So basically i modified the pafy file "backend_youtube_dl.py" as was said in the github pull request that never made it into the merge at "https://github.com/mps-youtube/pafy/pull/288/files".

    changed the line 53,54

    self._likes = self._ydl_info['like_count']
    self._dislikes = self._ydl_info['dislike_count']
    

    into

    self._likes = self._ydl_info.get('like_count', 0)
    self._dislikes = self._ydl_info.get('dislike_count', 0)
    

    done this my code didn't give errors anymore and downloaded the audio files as expected.

    I suppose this problem is caused by youtube removing the ability to see dislikes in all videos idk tho just my hypothesis.

    Thanks to John Gordon for guiding me!