Spaces:
Sleeping
Sleeping
| import os | |
| from glob import glob | |
| import yt_dlp | |
| import string | |
| import streamlit as st | |
| # Config | |
| if "videos" not in st.session_state: | |
| st.session_state["videos"] = "" | |
| if "downloaded" not in st.session_state: | |
| st.session_state["downloaded"] = [] | |
| # Callbacks | |
| def _remove_video(): | |
| if st.session_state["videos"]: | |
| try: | |
| os.remove(st.session_state["videos"]) | |
| st.session_state["videos"] = "" | |
| except FileNotFoundError: | |
| print("Couldn't delete file") | |
| def _remove_all_videos(): | |
| videos = glob(os.path.join(".", "data", "*.mp4")) \ | |
| + glob(os.path.join(".", "data", "*.webm")) \ | |
| + glob(os.path.join(".", "data", "*.mov")) \ | |
| + glob(os.path.join(".", "data", "*.m4a")) | |
| for video in videos: | |
| try: | |
| os.remove(video) | |
| except FileNotFoundError: | |
| print("Couldn't delete file:", video) | |
| # Main function | |
| def download_video(url: str, save_path: str, **kwargs): | |
| video_id = url.split("/")[-1] | |
| if len(video_id) < 1: | |
| video_id = url.split("/")[-2] | |
| ydl_opts = { | |
| 'outtmpl': f'{save_path}/{video_id}.%(ext)s', | |
| } | |
| if st.session_state["ig_cookie"]: | |
| ydl_opts['cookies'] = st.session_state["ig_cookie"] | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| ydl.download(url) | |
| videos = glob(os.path.join(".", "data", "*.mp4")) \ | |
| + glob(os.path.join(".", "data", "*.webm")) \ | |
| + glob(os.path.join(".", "data", "*.mov")) \ | |
| + glob(os.path.join(".", "data", "*.m4a")) | |
| print("videos found:", videos) | |
| for video in videos: | |
| if video_id in video: | |
| video_path = video | |
| return video_path | |
| st.title("Download Any* Video") | |
| # Side bar content | |
| with st.sidebar: | |
| st.subheader("Cookies! :cookie:") | |
| st.write( | |
| "For some websites that have a more strict bot control it's necessary to upload a cookie file to download content from them") | |
| st.file_uploader(label="**Upload Instagram Cookie** :cookie:", type=["txt", "json"], | |
| accept_multiple_files=False, key="ig_cookie") | |
| st.file_uploader(label="**Upload TikTok Cookie** :cookie:", type=["txt", "json"], | |
| accept_multiple_files=False, key="tt_cookie") | |
| st.button("Clear Videos", on_click=_remove_all_videos, type="secondary", help="Clear all downloaded videos") | |
| # Main content | |
| st.text_input("Video URL", key="url", placeholder="Enter Video URL and Hit Enter", on_change=_remove_video) | |
| if st.session_state["url"]: | |
| download = st.button("Load Video") | |
| if download: | |
| video_path = download_video(st.session_state["url"], save_path=os.path.join(".", "data")) | |
| st.session_state["videos"] = video_path | |
| if st.session_state["videos"]: | |
| st.write("**Title**:", st.session_state["videos"].split("/")[-1]) | |
| try: | |
| st.video(st.session_state["videos"]) | |
| except Exception as e: | |
| st.write("Couldn't show video") | |
| try: | |
| video_file = open(st.session_state["videos"], "rb") | |
| st.download_button("**Download Video ๐**", | |
| data=video_file, | |
| file_name=f"{st.session_state['videos'].lower().translate(str.maketrans('', '', string.punctuation)).replace(' ', '_')}.mp4", | |
| mime="video/mp4", | |
| type="primary") | |
| except Exception as e: | |
| st.error("Failed downloading the video", icon="๐") | |