Gokul14 commited on
Commit
4a85fef
1 Parent(s): d6aca98

Update youtube_video.py

Browse files
Files changed (1) hide show
  1. youtube_video.py +15 -8
youtube_video.py CHANGED
@@ -1,9 +1,10 @@
1
  import pytube
2
  import os
3
  from pytube import YouTube
 
4
 
5
- def download_youtube_audio(youtube_url):
6
- """Downloads the audio from a YouTube video, renames it to the first twelve characters, and returns the downloaded file path."""
7
 
8
  try:
9
  # Create a YouTube object
@@ -20,13 +21,19 @@ def download_youtube_audio(youtube_url):
20
  original_filename = audio.default_filename
21
 
22
  # Extract the first twelve characters and change the file extension to .mp3
23
- new_filename = original_filename[:12] + '.mp3'
24
 
25
- # Rename the downloaded file
26
- os.rename(original_filename, new_filename)
27
 
28
- print("Download complete! Audio saved to:", new_filename)
29
- return new_filename
 
 
 
 
 
 
30
 
31
  except Exception as e:
32
  print("An error occurred:", e)
@@ -34,4 +41,4 @@ def download_youtube_audio(youtube_url):
34
 
35
  # Example usage
36
  youtube_url = "https://www.youtube.com/watch?v=your_video_id"
37
- download_youtube_audio(youtube_url)
 
1
  import pytube
2
  import os
3
  from pytube import YouTube
4
+ from pydub import AudioSegment
5
 
6
+ def download_and_compress_youtube_audio(youtube_url):
7
+ """Downloads the audio from a YouTube video, compresses it, renames it to the first twelve characters, and returns the downloaded file path."""
8
 
9
  try:
10
  # Create a YouTube object
 
21
  original_filename = audio.default_filename
22
 
23
  # Extract the first twelve characters and change the file extension to .mp3
24
+ compressed_filename = original_filename[:12] + '_compressed.mp3'
25
 
26
+ # Load the downloaded audio file
27
+ audio_segment = AudioSegment.from_file(original_filename)
28
 
29
+ # Compress the audio file (e.g., by reducing the bitrate)
30
+ audio_segment.export(compressed_filename, format="mp3", bitrate="64k")
31
+
32
+ # Remove the original downloaded file
33
+ os.remove(original_filename)
34
+
35
+ print("Download and compression complete! Audio saved to:", compressed_filename)
36
+ return compressed_filename
37
 
38
  except Exception as e:
39
  print("An error occurred:", e)
 
41
 
42
  # Example usage
43
  youtube_url = "https://www.youtube.com/watch?v=your_video_id"
44
+ download_and_compress_youtube_audio(youtube_url)