hi i'm learning python i discorver pytube to download on youtube the music.
my goal is to have the best audio quality. it seem that on youtube the best that you can have is 160 kbps. so i this:
from pytube import YouTube
url = input("Entre l'URL de la vidéo : ")
def on_download_progress(stream, chunk, bytes_remaining):
bytes_downloaded = stream.filesize - bytes_remaining
percent = bytes_downloaded * 100 / stream.filesize
print(f"Progression du téléchargement {int(percent)}%")
youtube_video = YouTube(url)
youtube_video.register_on_progress_callback(on_download_progress)
print("TITRE: " + youtube_video.title)
print("STREAMS")
streams = youtube_video.streams.filter(progressive=False, type="audio").order_by('abr').desc()
audio_stream = streams[0]
print("Téléchargement audio...")
audio_stream.download()
print("OK")
so i get a webm file sized: 3,76 Mo
-FIRST PROBLEM: when i look at the property i got this:property the total debit is at 126 kbps not at 160 like the abr in the itag. is it a different unit? did i do something wrong.
so i do music and i have a tool to check the true audio spectral. its a little exe call spek (its a pig) so i put my web in spek and i got this spek
so i have a 320 kbps result its realy good so it's cool even i don't know why.
BUT the extension webm is gross we can't work with i can read it in player but if i want to put in a musical i need to convert it. so i did this:
from pytube import YouTube import ffmpeg import os
url = input("Entre l'URL de la vidéo : ")
def on_download_progress(stream, chunk, bytes_remaining):
bytes_downloaded = stream.filesize - bytes_remaining
percent = bytes_downloaded * 100 / stream.filesize
print(f"Progression du téléchargement {int(percent)}%")
youtube_video = YouTube(url)
youtube_video.register_on_progress_callback(on_download_progress)
print("TITRE: " + youtube_video.title)
print("STREAMS")
streams = youtube_video.streams.filter(progressive=False, type="audio").order_by('abr').desc()
audio_stream = streams[0]
print("Téléchargement audio...")
audio_stream.download()
print("OK")
filename_without_ext, _ = os.path.splitext(audio_stream.default_filename)
output_filename = filename_without_ext + ".wav"
streamt = ffmpeg.input(audio_stream.default_filename)
audio = streamt.audio
streamt = ffmpeg.output(audio, output_filename)
ffmpeg.run(streamt)
os.remove(audio_stream.default_filename)
so i get a wav file of the same quality when i check in spek. but the weight is realy bigger the size is 45,9 Mo
SECOND QUESTION: is it possible to get the same size of the first file? it have same quality it can't be heavier. ( i looked around it seem to be impossible the WAV format is bigger thats all... maybe not?)
so i decide to convert it in mp3. the mp3 can go until 320 kbps. so i am in the range its technicaly possible to get the same quality so i did that:
from pytube import YouTube
import ffmpeg
import os
url = input("Entre l'URL de la vidéo : ")
def on_download_progress(stream, chunk, bytes_remaining):
bytes_downloaded = stream.filesize - bytes_remaining
percent = bytes_downloaded * 100 / stream.filesize
print(f"Progression du téléchargement {int(percent)}%")
youtube_video = YouTube(url)
youtube_video.register_on_progress_callback(on_download_progress)
print("TITRE: " + youtube_video.title)
print("STREAMS")
streams = youtube_video.streams.filter(progressive=False, type="audio").order_by('abr').desc()
audio_stream = streams[0]
print("Téléchargement audio...")
audio_stream.download()
print("OK")
filename_without_ext, _ = os.path.splitext(audio_stream.default_filename)
output_filename = filename_without_ext + ".mp3"
input_stream = ffmpeg.input(audio_stream.default_filename)
output_stream = ffmpeg.output(input_stream, output_filename)
ffmpeg.run(output_stream)
os.remove(audio_stream.default_filename)
so i get an mp3 file . but when i look in spek i have an hard drop rate drop
THIRD: how to get the same bitrate as the webm file?
i decide to try https://convertio.co/fr/webm-mp3/
and i gess what it work very weel the aduio quality of there mp3 is the same orf my webm. there mp3 file weight 7 Mo (heavier than mine). So it's technicaly possible to convert a webm into an mp3 of the same quality. but how to do it in python? ffmpeg just eat the quality of my file....
how to convert a webm file to mp3 with the same quality? and master the compression of wav and mp3 file ? all of that in python