diff --git a/README.md b/README.md index 372ebfb..da4af80 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![HuggingFace Space](https://img.shields.io/badge/🤗-HuggingFace%20Space-cyan.svg)](https://huggingface.co/spaces/neonwatty/youtube_downloader) [Python application](https://github.com/neonwatty/youtube_downloader/actions/workflows/python-app.yml/badge.svg)](https://github.com/neonwatty/youtube_downloader/actions/workflows/python-app.yml/python-app.yml) +[![HuggingFace Space](https://img.shields.io/badge/🤗-HuggingFace%20Space-cyan.svg)](https://huggingface.co/spaces/neonwatty/youtube_downloader)[Python application](https://github.com/neonwatty/youtube_downloader/actions/workflows/python-app.yml/badge.svg)](https://github.com/neonwatty/youtube_downloader/actions/workflows/python-app.yml/python-app.yml) # a simple youtube_downloader app diff --git a/youtube_downloader/app.py b/youtube_downloader/app.py index c0e301f..f27db4d 100644 --- a/youtube_downloader/app.py +++ b/youtube_downloader/app.py @@ -105,9 +105,7 @@ def reset_session_state(): col2, col3, col4 = st.columns([3, 2, 3]) with col2: - check_button_val = st.button( - label="fetch available streams", type="primary" - ) + check_button_val = st.button(label="fetch available streams", type="primary") with col3: panel = st.container() with col4: @@ -157,15 +155,9 @@ def get_set_streams(url: str) -> None: def download_button_logic(download_button_val: bool): if download_button_val: if st.session_state["a_v_selection_index"] == 0: - if ( - st.session_state["a_selection_index"] == 0 - and st.session_state["v_selection_index"] == 0 - ): + if st.session_state["a_selection_index"] == 0 and st.session_state["v_selection_index"] == 0: st.warning("please make a selection", icon="⚠️") - elif ( - st.session_state["a_selection_index"] == 0 - or st.session_state["v_selection_index"] == 0 - ): + elif st.session_state["a_selection_index"] == 0 or st.session_state["v_selection_index"] == 0: st.warning( "if video only value chosen so must audio only value and vice-versa", icon="⚠️", @@ -174,7 +166,7 @@ def download_button_logic(download_button_val: bool): with st.spinner(text="download in progress..."): savedir = os.path.expanduser("~/Downloads") vid_col, img_col = st.columns([10, 1]) - + # download audio/video jointly audio_index = st.session_state["a_selection_index"] audio_only_streams = st.session_state["audio_only_streams"] @@ -200,13 +192,10 @@ def download_button_logic(download_button_val: bool): st.subheader(st.session_state["yt_title"]) video_file = open(video_savepath, "rb") video_bytes = video_file.read() - st.video(video_bytes) - + st.video(video_bytes) + else: - if ( - st.session_state["a_selection_index"] != 0 - or st.session_state["v_selection_index"] != 0 - ): + if st.session_state["a_selection_index"] != 0 or st.session_state["v_selection_index"] != 0: st.warning( "cannot chose option for audio/video joint, video only, and audio only", icon="⚠️", @@ -234,11 +223,12 @@ def download_button_logic(download_button_val: bool): video_bytes = video_file.read() st.video(video_bytes) + def render_panel(): my_panel = st.empty() with my_panel.container(border=True): a_selection, v_selection, a_v_selection = None, None, None - + with col_a: a_v_selection = st.selectbox( "joint selection (fps)", @@ -247,9 +237,7 @@ def render_panel(): placeholder="Select video fps", ) if a_v_selection: - st.session_state["a_v_selection_index"] = list( - st.session_state["audio_video_choices"] - ).index(a_v_selection) + st.session_state["a_v_selection_index"] = list(st.session_state["audio_video_choices"]).index(a_v_selection) else: st.session_state["a_v_selection_index"] = 0 @@ -261,9 +249,7 @@ def render_panel(): placeholder="Select video fps", ) if v_selection: - st.session_state["v_selection_index"] = list( - st.session_state["video_only_choices"] - ).index(v_selection) + st.session_state["v_selection_index"] = list(st.session_state["video_only_choices"]).index(v_selection) else: st.session_state["v_selection_index"] = 0 @@ -275,16 +261,12 @@ def render_panel(): placeholder="Select audio kbps", ) if a_selection: - st.session_state["a_selection_index"] = list( - st.session_state["audio_only_choices"] - ).index(a_selection) + st.session_state["a_selection_index"] = list(st.session_state["audio_only_choices"]).index(a_selection) else: st.session_state["a_selection_index"] = 0 # download button - download_button_val = st.button( - label="download selected streams", type="primary" - ) + download_button_val = st.button(label="download selected streams", type="primary") download_button_logic(download_button_val) st.session_state["panel"] = my_panel @@ -306,5 +288,5 @@ def streams_button_logic(url: str) -> None: with st.spinner(text="streams pull in progress..."): try: streams_button_logic(st.session_state["url"]) - except: + except: # noqa E722 pass diff --git a/youtube_downloader/download.py b/youtube_downloader/download.py index 31c76f6..b98e47a 100644 --- a/youtube_downloader/download.py +++ b/youtube_downloader/download.py @@ -8,22 +8,20 @@ def download_joint_stream(yt: YouTube, itag: int, save_dir: str, yt_title: str) -> str: try: final_save_path = save_dir + "/" + yt_title + ".mp4" - final_save_path = re.sub(r'[^a-zA-Z0-9./]', ' ', final_save_path) + final_save_path = re.sub(r"[^a-zA-Z0-9./]", " ", final_save_path) yt.streams.get_by_itag(itag).download(filename=final_save_path) return final_save_path except Exception as e: raise ValueError(f"download_joint_stream failed with exception {e}") -def download_separate_streams_and_join( - yt: YouTube, audio_itag: int, video_itag: int, save_dir: str, yt_title: str -) -> str: +def download_separate_streams_and_join(yt: YouTube, audio_itag: int, video_itag: int, save_dir: str, yt_title: str) -> str: with tempfile.TemporaryDirectory() as tmpdirname: tmpaudiopath = tmpdirname + "/" + yt_title + "_audio.mp4" - tmpaudiopath = re.sub(r'[^a-zA-Z0-9./]', ' ', tmpaudiopath) + tmpaudiopath = re.sub(r"[^a-zA-Z0-9./]", " ", tmpaudiopath) tmpvideopath = tmpdirname + "/" + yt_title + "_video.mp4" - tmpvideopath = re.sub(r'[^a-zA-Z0-9./]', ' ', tmpvideopath) + tmpvideopath = re.sub(r"[^a-zA-Z0-9./]", " ", tmpvideopath) yt.streams.get_by_itag(audio_itag).download(filename=tmpaudiopath) yt.streams.get_by_itag(video_itag).download(filename=tmpvideopath) @@ -33,7 +31,7 @@ def download_separate_streams_and_join( audio_clip = AudioFileClip(tmpaudiopath) video_clip.audio = audio_clip final_save_path = save_dir + "/" + yt_title + ".mp4" - final_save_path = re.sub(r'[^a-zA-Z0-9./]', ' ', final_save_path) + final_save_path = re.sub(r"[^a-zA-Z0-9./]", " ", final_save_path) video_clip.write_videofile( final_save_path, codec="libx264", diff --git a/youtube_downloader/streams.py b/youtube_downloader/streams.py index c104647..62c5642 100644 --- a/youtube_downloader/streams.py +++ b/youtube_downloader/streams.py @@ -13,9 +13,7 @@ def is_valid_youtube_url(url: str) -> bool: return re.match(pattern, url) is not None -def get_yt_streams( - url: str, my_proxies: dict = {} -) -> Tuple[YouTube, str, str, StreamQuery, StreamQuery, StreamQuery]: +def get_yt_streams(url: str, my_proxies: dict = {}) -> Tuple[YouTube, str, str, StreamQuery, StreamQuery, StreamQuery]: try: # validate url if is_valid_youtube_url(url): @@ -23,18 +21,10 @@ def get_yt_streams( yt = YouTube(url, proxies=my_proxies) # audio only streams - audio_only_streams = ( - yt.streams.filter(file_extension="mp4", only_audio=True, type="audio") - .order_by("abr") - .asc() - ) + audio_only_streams = yt.streams.filter(file_extension="mp4", only_audio=True, type="audio").order_by("abr").asc() # video only streams - video_only_streams = ( - yt.streams.filter(file_extension="mp4", only_video=True, type="video") - .order_by("resolution") - .asc() - ) + video_only_streams = yt.streams.filter(file_extension="mp4", only_video=True, type="video").order_by("resolution").asc() # audio and video joint streams audio_video_streams = ( @@ -48,7 +38,7 @@ def get_yt_streams( .order_by("resolution") .asc() ) - + # get title and thumbnail yt_title = yt.title.replace("/", " ") yt_thumbnail_url = yt.thumbnail_url