|
import os |
|
import shutil |
|
import streamlit as st |
|
from pytube import YouTube |
|
import platform |
|
|
|
def get_default_download_folder(): |
|
system_platform = platform.system() |
|
if system_platform == "Windows": |
|
return os.path.join(os.path.expanduser("~"), "Downloads") |
|
elif system_platform == "Linux": |
|
return os.path.join(os.path.expanduser("~"), "Downloads") |
|
elif system_platform == "Darwin": |
|
return os.path.join(os.path.expanduser("~"), "Downloads") |
|
elif system_platform == "Android": |
|
return "/storage/emulated/0/Download" |
|
else: |
|
raise NotImplementedError("Platform not supported") |
|
|
|
def download_video(url): |
|
try: |
|
yt = YouTube(url) |
|
streams = yt.streams.filter(progressive=True, file_extension="mp4") |
|
highest_res_stream = streams.get_highest_resolution() |
|
|
|
save_path = os.path.join(os.getcwd(), yt.title + '.mp4') |
|
highest_res_stream.download(output_path=os.getcwd(), filename=yt.title) |
|
|
|
default_download_folder = get_default_download_folder() |
|
shutil.move(save_path, os.path.join(default_download_folder, yt.title + '.mp4')) |
|
st.success("Video downloaded successfully!") |
|
except Exception as e: |
|
st.error(f"An error occurred: {e}") |
|
|
|
def main(): |
|
st.title("YouTube Video Downloader") |
|
|
|
|
|
video_url = st.text_input("Enter YouTube URL:") |
|
|
|
|
|
if st.button("Download"): |
|
if video_url.strip() == "": |
|
st.warning("Please enter a valid YouTube URL.") |
|
else: |
|
|
|
download_video(video_url) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|