|
import yt_dlp |
|
|
|
def download_audio_from_video(url, output_path='./'): |
|
"""Download audio from a YouTube video and save it as MP3.""" |
|
|
|
ydl_opts = { |
|
'format': 'bestaudio/best', |
|
'outtmpl': f'{output_path}/%(title)s.%(ext)s', |
|
'noplaylist': True, |
|
'postprocessors': [{ |
|
'key': 'FFmpegAudioConvertor', |
|
'preferredcodec': 'mp3', |
|
'preferredquality': '192', |
|
}], |
|
'quiet': False |
|
} |
|
|
|
try: |
|
|
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl: |
|
ydl.download([url]) |
|
print("Download completed successfully.") |
|
except Exception as e: |
|
print(f"Error: {e}") |
|
|
|
|
|
url = input("Enter YouTube video URL: ") |
|
output_path = input("Enter output directory (default is current directory): ") or './' |
|
download_audio_from_video(url, output_path) |
|
|