Reputation: 570
I was trying to make a really simple python code to get me the stream with the highest quality audio, so I first tried something like this
def get_highest_audio(url):
yt = YouTube(url)
best_audio_stream = yt.streams.filter(only_audio=True).all()[1]
return best_audio_stream
Which did return a stream, but it wasn't the stream with the highest quality audio, so I tried to find a function in the pytube library.
While there was a get_highest_resolution()
function, there was not a get_highest_audio_resolution()
function.
So I tried to get the audio resolution by hand, but there wasn't a function that got the audio resolution of a stream.
Is there any way to create this function?
Upvotes: 4
Views: 11779
Reputation: 11
You could .order_by('abr')
which would order the list from lowest audio bitrate to the hightest. Then you can use a .last()
to get the last stream - the one with best bitrate.
The new code would look like this:
def get_highest_audio(url):
yt = YouTube(url)
best_audio_stream = yt.streams.filter(only_audio=True).order_by('abr').last()
return best_audio_stream
Upvotes: 0
Reputation: 3616
Try This: My Github URL
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Created Syst: macOS Monterey 12.6 (21G115) Kernel: Darwin 21.6.0
# Created Plat: Python 3.10.8 ('v3.10.8:aaaf517424', 'Oct 11 2022 10:14:40')
# Created By : Jeromie Kirchoff (JayRizzo)
# Created Date: Tue Jul 13 20:52:32 2021 CST
# Last ModDate: Sat Nov 26 19:56:32 2022 CST
# =============================================================================
"""This Module Has Been Build For Downloading The Highest Quality Video & Audio Of A YouTube Video."""
# =============================================================================
from os import path as ospath
from pytube import YouTube
CURRENT_HOME = ospath.expanduser('~')
VIDEO_FILE_PATH = ospath.join(CURRENT_HOME, 'Videos', 'MusicVideos')
AUDIO_FILE_PATH = ospath.join(CURRENT_HOME, 'Music', 'Downloaded')
def getYTVid(URL):
"""Get Highest Quality Video from YT URL."""
YT = YouTube(URL)
try:
print(f"Downloading Video: {YT.title}")
YTVIDEO_FILE_PATH = YT.streams.filter(only_audio=False, progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(VIDEO_FILE_PATH)
print(f"Download Video Completed: {YTVIDEO_FILE_PATH}\n")
except Exception as e:
print(f"Error: {e}")
def getYTAudio(URL):
"""Get Highest Quality Audio from YT URL."""
YT = YouTube(URL)
try:
YTAUDIO_FILE_PATH = YT.streams.filter(only_audio=True, file_extension='mp4').order_by('abr').desc().first().download(AUDIO_FILE_PATH)
print(f"Download Video Completed: {YTAUDIO_FILE_PATH}\n")
except Exception as e:
print(f"Error: {e}")
if __name__ == '__main__':
print(f"Video Path: {VIDEO_FILE_PATH}")
print(f"Audio Path: {AUDIO_FILE_PATH}")
print("Downloading Audio: Tom MacDonald - I Wish.mp4")
getYTAudio('https://www.youtube.com/watch?v=8wNUjCcaGrM') # Tom MacDonald - I Wish.mp4
print("Downloading Audio: Tom MacDonald - \"Cancelled\".mp4")
getYTAudio('https://www.youtube.com/watch?v=EHBMbZdCpSk') # Tom MacDonald - "Cancelled".mp4
print("Downloading Audio: Tom MacDonald - Dont Look Down (uncensored).mp4")
getYTAudio('https://www.youtube.com/watch?v=Ex3zq_ADrNU') # Tom MacDonald - Dont Look Down (uncensored).mp4
print("Downloading Vid: Tom MacDonald - I Wish.mp4")
getYTVid('https://www.youtube.com/watch?v=8wNUjCcaGrM') # Tom MacDonald - I Wish.mp4
print("Downloading Vid: Tom MacDonald - \"Cancelled\".mp4")
getYTVid('https://www.youtube.com/watch?v=EHBMbZdCpSk') # Tom MacDonald - "Cancelled".mp4
print("Downloading Vid: Tom MacDonald - Dont Look Down (uncensored).mp4")
getYTVid('https://www.youtube.com/watch?v=Ex3zq_ADrNU') # Tom MacDonald - Dont Look Down (uncensored).mp4
Upvotes: 0
Reputation: 119
You can just use:
yt.streams.get_audio_only()
this gets the highest bitrate audio stream. It defaults to mp4.
Upvotes: 9
Reputation: 687
Why not remove the [1] and it will display all audio formats. From there you can select the highest one?
Upvotes: 0