Skip to content

Commit

Permalink
requirements test added
Browse files Browse the repository at this point in the history
  • Loading branch information
neonwatty committed Jul 17, 2024
1 parent 91510ad commit ed21a6a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
46 changes: 14 additions & 32 deletions youtube_downloader/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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="⚠️",
Expand All @@ -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"]
Expand All @@ -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="⚠️",
Expand Down Expand Up @@ -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)",
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
12 changes: 5 additions & 7 deletions youtube_downloader/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
Expand Down
18 changes: 4 additions & 14 deletions youtube_downloader/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,18 @@ 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):
# load in video
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 = (
Expand All @@ -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
Expand Down

0 comments on commit ed21a6a

Please sign in to comment.