Hide and unhide files in your system.
March 9, 2025Tortoise-tts voice cloning
March 9, 2025Most youtube downloaders fail when the videos are past 20 mins. This script allows you to download lengthier videos, with the option of using moviepy to convert them to audios.
This can be good if you intend to download podcasts for transcription purpose(s).
While, I didn't put in much effort to refine the code, it does what I want.
Now, if you continue to download - say 100s of files, youtube might place a temporary ban on your IP.
The hack to get behind this is to use google colab. In that way, you are accessing youtube via google's server.
So you can run this code on google colab. To run it on your pc, then you might have to adjust the path location.
#LONG VIDEO TO AUDIO
from pytubefix import YouTube, Playlist
from moviepy.editor import *
import os
import time
import IPython
from IPython.display import Audio
newname = []
def download_video_from_youtube(link):
yt = YouTube(link)
#video = yt.streams.get_highest_resolution()
#video = yt.streams.filter(only_audio=True)
#stream = yt.streams.filter(only_audio=True).first()
streams = yt.streams.get_highest_resolution()
newname.append(streams.title)
three = streams.download('/content/')
time.sleep(3)
os.rename(f'{three}', f'{streams.title}.mp4')
time.sleep(3)
#video.download('/content/')
return f'{streams.title}.mp4'
# Return the correct file path
def convert_video_to_audio(video_path, output_file_name):
try:
video = VideoFileClip(video_path)
audio = video.audio
audio.write_audiofile(output_file_name)
# Load audio file and get data and sampling rate
#audio_data, sr = librosa.load(output_file_name, sr=None)
# Display audio player
print(f"Audio file '{output_file_name}' successfully created.")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
while True:
try:
youtube_link = input("Enter your YouTube playlist link here: ")
try:
video_path = download_video_from_youtube(youtube_link)
jack = ''.join([str(elem) for elem in newname[-1]])
output_file_format = jack + ".mp3"
convert_video_to_audio(video_path, output_file_format)
time.sleep(2)
os.remove(video_path)
except:
print("Error")
new_download = input("Do you want to download another song? Yes/No: ")
if new_download.lower() == "yes":
continue
else:
print("Thanks for using Uncle Phil's youtube downloader.")
break
except ValueError as ve:
print(f"ValueError: {ve}")
So now, let's say you intend to download a playlist. From music.youtube.com then this script does it perfectly. It downloads each file from the youtube music playlist. Please note, this will only work for playlists.
!pip install pytubefix moviepy
from pytubefix import YouTube, Playlist
from moviepy.editor import *
import os
import time
import IPython
from IPython.display import Audio
newname = []
def download_video_from_youtube(link):
yt = YouTube(link)
#video = yt.streams.get_highest_resolution()
#video = yt.streams.filter(only_audio=True)
#stream = yt.streams.filter(only_audio=True).first()
streams = yt.streams.get_highest_resolution()
newname.append(streams.title)
three = streams.download('/content/')
time.sleep(3)
os.rename(f'{three}', f'{streams.title}.mp4')
time.sleep(3)
#video.download('/content/')
return f'{streams.title}.mp4'
# Return the correct file path
def convert_video_to_audio(video_path, output_file_name):
try:
video = VideoFileClip(video_path)
audio = video.audio
audio.write_audiofile(output_file_name)
# Load audio file and get data and sampling rate
#audio_data, sr = librosa.load(output_file_name, sr=None)
# Display audio player
print(f"Audio file '{output_file_name}' successfully created.")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
while True:
try:
youtube_link = input("Enter your YouTube playlist link here: ")
playlist = Playlist(youtube_link)
print(f" playlist: {playlist}")
for youtube_link in playlist.video_urls:
try:
video_path = download_video_from_youtube(youtube_link)
jack = ''.join([str(elem) for elem in newname[-1]])
output_file_format = jack + ".mp3"
convert_video_to_audio(video_path, output_file_format)
time.sleep(2)
os.remove(video_path)
except:
print("Error")
continue
new_download = input("Do you want to download another song? Yes/No: ")
if new_download.lower() == "yes":
continue
else:
print("Thanks for using Uncle Phil's youtube downloader.")
break
except ValueError as ve:
print(f"ValueError: {ve}")
