Video-Analysis-Tool / streamlit_app.py
Matias Macias Gomez
Added project files
9d331e0
raw
history blame
1.59 kB
import os
import yt_dlp
import string
import streamlit as st
# Callbacks
if "videos" not in st.session_state:
st.session_state["videos"] = ""
if "downloaded" not in st.session_state:
st.session_state["downloaded"] = []
def download_video(url:str, save_path:str , download:bool, **kwargs):
ydl_opts = {
'outtmpl': f'{save_path}/video.%(ext)s',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=download)
video_title = info_dict.get('title', None)
video_title = f"{video_title}"
return video_title
st.title("Download Any Video")
st.text_input("Video URL", key="url", placeholder="Enter Video URL and Hit Enter")
if st.session_state["url"]:
download = False if st.session_state["url"] in st.session_state["downloaded"] else True
video_path = download_video(st.session_state["url"], save_path=os.path.join(".", "data"), download=download)
st.session_state["videos"] = video_path
if not download:
st.session_state["downloaded"].append(st.session_state["url"])
if st.session_state["videos"]:
st.write("**Title**:", st.session_state["videos"])
st.video(os.path.join(".", "data", "video.mp4"))
video_file = open("data/video.mp4", "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")