A python tool that let you download and convert to mp3 a YouTube video
Use the package manager pip to install youtube-dl.
pip install youtube-dl
Then you will need to install ffmpeg, for linux based machines, you will just need to use your console.
Example Ubuntu:
sudo apt update
sudo apt install ffmpeg
On windows ones instead, you will need to download the last build
At this point copy the 3 files you can find on bin folder, and paste them in the main Python's scripts folder
youtube-dl will be used to download the video from youtube
ffmpeg will convert them to mp3
In the first part of the code, we will need to import unicode_literals from future and import youtube_dl
We want to make the person using the tool to choose the youtube video's link directly in the console, and not in the code, we will make that by declaring a variable named link, which will take the user's input when the tool is runned.
from __future__ import unicode_literals
import youtube_dl
print("Insert the link")
link = input ("")
We need to specify all the options for the video, like the quality, the codec, etc...
We will download our video with the maximum quality and in a mp3 format
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([link])
So our code will look like:
from __future__ import unicode_literals
import youtube_dl
print("Insert the link")
link = input ("")
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([link])
For any issue contact me on discord: Silvano#8106