5

I'm using pytube and Python 3.5 to download videos from YouTube but I need to convert the videos to audio with an .avi extension.

Here is my code that I'm currently working with:

from pytube import YouTube

yt = YouTube(str(input("Enter the video link: ")))
videos = yt.get_videos()

s = 1
for v in videos:
    print(str(s)+". "+str(v))
    s += 1

n = int(input("Enter the number of the video: "))
vid = videos[n-1]

destination = str(input("Enter the destination: "))
vid.download(destination)

print(yt.filename+"\nHas been successfully downloaded")

In the code above I can download a video.

My question is: How can I directly download the audio of a video on YouTube with an extension of .avi?

Can YouTube Data API help me download exclusively the audio?

Phill Healey
  • 2,849
  • 2
  • 28
  • 61
omar harchich
  • 59
  • 1
  • 5
  • what is your question? – Argyll Mar 10 '18 at 00:40
  • in the code above I can download a video. my question is how can I directly download the audio of a video on youtube with the extension .avi ? – omar harchich Mar 10 '18 at 01:06
  • There are two issues here. One is that you need to clarify your current question and future questions. Actually asking a question is a good start. I can edit what you just wrote in this time as an example of what one can do. The other issue is that if you want to download audio exclusively from youtube, you would need youtube to supply audio and video data that are encoded separately. That's not impossible -- but it seems unlikely to me. Regardless of what interface you use, it would seem that you have to download the video file with audio and then extract the audio from there – Argyll Mar 10 '18 at 01:11
  • can Youtube Data API helps me to download exclusively the audio ? – omar harchich Mar 10 '18 at 01:18
  • I don't know. Let's add what you just asked to your original question and also add the relevant tags. Adding the right tags help you attract the attention of those who have experience with the specific topic. But of course, whenever you do add tags, you need to make sure your question is relevant to the tags and vice versa. In this case, you think it might be. So it's suitable to ask that specifically and then tag away – Argyll Mar 10 '18 at 01:21
  • yeah ! i understand – omar harchich Mar 10 '18 at 01:25
  • If tags and question don't relate, people may spend time examining your question, find it irrelevant, and recommend deletion. That won't help you solve your problem. Good luck! – Argyll Mar 10 '18 at 01:26
  • i think i find out the answer from the documentation of pytube library, if you are interested https://media.readthedocs.org/pdf/python-pytube/latest/python-pytube.pdf – omar harchich Mar 10 '18 at 01:28
  • Interesting. Indeed, on page 9, it mentions that some streams may have separate codex. It would seem that in such cases, you are able to download the audio separately. If it works for you, maybe answer the question yourself? That way, people in the future can refer to your question/answer and benefit from your knowledge! – Argyll Mar 12 '18 at 12:16
  • Yes, i did it. It is very simple. Thanks for your comments! – omar harchich Mar 15 '18 at 18:11
  • pytube is no longer maintained, consider using pytube3 instead – Martin Feb 08 '20 at 21:20

1 Answers1

14

Upgrade your pytube, as it seems that YouTube().get_videos() is deprecated. About downloading the video: you can filter the streams for audio with a command like YouTube().streams.filter(only_audio=True).all(). Printing that function can guide you to the type of audio you want to download. In order to give it .avi format, I think you can use any converter. I'm using python 3.6.

from pytube import YouTube
yt=YouTube(link)
t=yt.streams.filter(only_audio=True).all()
t[0].download(/path)
Thomas Dondorf
  • 15,689
  • 6
  • 46
  • 75
Sadeny Alpha
  • 176
  • 5
  • the problem that i found is i named my python file 'pytube.py' which makes python to create a folder called 'pytube_cache'. So, when i rename the file it works very good. Thanks for your help !! – omar harchich Aug 15 '18 at 14:23
  • Can you tell what is the difference between .first() & .all() used after filter? – Gourab Mar 25 '21 at 09:46
  • The code needs to be adapted to deal with `DeprecationWarning: Call to deprecated function all (This object can be treated as a list, all() is usele ss).`. To fix it, remove `.all()`. – Tiago Martins Peres 李大仁 May 18 '21 at 23:12