-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp3.py
More file actions
66 lines (49 loc) · 1.9 KB
/
mp3.py
File metadata and controls
66 lines (49 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from pytube import YouTube
import ffmpeg
def download_youtube_video(url, output_path):
# Create a YouTube object
yt = YouTube(url)
# Get the highest quality audio stream
audio_stream = yt.streams.filter(only_audio=True).first()
# Download the audio stream
audio_file = audio_stream.download(output_path=output_path)
return audio_file
def convert_to_mp3(input_file, output_file):
# Convert the audio file to MP3 using FFmpeg
ffmpeg.input(input_file).output(output_file, format='mp3').run(overwrite_output=True)
os.remove(input_file) # Remove the original audio file
def main():
# YouTube video URL
url = input("Enter the YouTube video URL: ")
# Output directory
output_path = "downloads"
os.makedirs(output_path, exist_ok=True)
# Download the video
audio_file = download_youtube_video(url, output_path)
# Convert to MP3
output_file = os.path.join(output_path, os.path.basename(audio_file).replace('.mp4', '.mp3'))
convert_to_mp3(audio_file, output_file)
print(f"MP3 file saved to: {output_file}")
if __name__ == "__main__":
main()
from pytube import YouTube
import urllib.parse
def download_youtube_video(url, output_path):
try:
# Clean or encode the URL if needed
encoded_url = urllib.parse.quote(url, safe='')
yt = YouTube(encoded_url)
audio_stream = yt.streams.filter(only_audio=True).first()
print("Downloading audio...")
audio_file = audio_stream.download(output_path=output_path)
print(f"Download complete: {audio_file}")
return audio_file
except Exception as e:
print(f"An error occurred: {e}")
def main():
url = input("Enter the YouTube video URL: ")
output_path = "./" # Change this to your preferred output path
download_youtube_video(url, output_path)
if __name__ == "__main__":
main()