|
import pytube |
|
import os |
|
from pytube import YouTube |
|
|
|
def download_youtube_audio(youtube_url): |
|
"""Downloads the audio from a YouTube video, renames it to the first twelve characters, and returns the downloaded file path.""" |
|
|
|
try: |
|
|
|
yt = YouTube(youtube_url) |
|
|
|
|
|
audio = yt.streams.filter(only_audio=True).first() |
|
|
|
|
|
print("Downloading...") |
|
audio.download() |
|
|
|
|
|
original_filename = audio.default_filename |
|
|
|
|
|
new_filename = original_filename[:12] + '.mp3' |
|
|
|
|
|
os.rename(original_filename, new_filename) |
|
|
|
print("Download complete! Audio saved to:", new_filename) |
|
return new_filename |
|
|
|
except Exception as e: |
|
print("An error occurred:", e) |
|
return None |
|
|
|
|
|
youtube_url = "https://www.youtube.com/watch?v=your_video_id" |
|
download_youtube_audio(youtube_url) |
|
|