Upload 2 files
Browse files- app.py +126 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import yt_dlp
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Streamlit μ λͺ©
|
| 6 |
+
st.title('μ νλΈ μμ λ€μ΄λ‘λ')
|
| 7 |
+
st.write("<br>", unsafe_allow_html=True) # 1μ€ λμ΄μ°κΈ°
|
| 8 |
+
st.write("μ¬μ©λ°©λ²")
|
| 9 |
+
st.write("<br>", unsafe_allow_html=True) # 1μ€ λμ΄μ°κΈ°
|
| 10 |
+
st.write("1. λ€μ΄λ°μΌλ €λ μ νλΈ μμμ λ§ν¬λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ")
|
| 11 |
+
st.write("2. λ§ν¬λ₯Ό μ
λ ₯νκ³ νμΈλ²νΌμ λλ₯΄λ©΄ ν΄λΉ μμμ μΈλ€μΌμ΄ λ±μ₯ν©λλ€")
|
| 12 |
+
st.write("3. μΈλ€μΌ λ°μ μ νλμ ν΅ν΄ ν΄μλμ μμ μ©λμ νμΈν ν μνλ ν΄μλλ₯Ό μ ννκ³ μΆμΆ λ²νΌμ λλ¬μ£ΌμΈμ")
|
| 13 |
+
st.write("4. μ μ ν λ€μ΄λ°μΌλ €λ μμμ΄ λ°μ λ±μ₯νκ³ μ¬μνκ±°λ λ€μ΄λ‘λ λ°μ μ μμ΅λλ€ (μμμ μ©λμ΄ ν΄μλ‘ μΆμΆνλλ° μκ°μ΄ λ 걸립λλ€. 20λΆ μμ μΆμΆνλλ° 1λΆλ΄λ‘ μμ)")
|
| 14 |
+
|
| 15 |
+
# μ μ λ‘λΆν° μ νλΈ λ§ν¬ μ
λ ₯λ°κΈ°
|
| 16 |
+
col1, col2 = st.columns([4, 1])
|
| 17 |
+
with col1:
|
| 18 |
+
youtube_url = st.text_input('Enter YouTube video URL')
|
| 19 |
+
|
| 20 |
+
def fetch_video_info(youtube_url):
|
| 21 |
+
ydl_opts = {'nocheckcertificate': True}
|
| 22 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 23 |
+
info_dict = ydl.extract_info(youtube_url, download=False)
|
| 24 |
+
return info_dict
|
| 25 |
+
|
| 26 |
+
def get_available_formats(info_dict):
|
| 27 |
+
formats = {}
|
| 28 |
+
for fmt in info_dict['formats']:
|
| 29 |
+
if fmt.get('height') and fmt.get('ext') == 'mp4':
|
| 30 |
+
filesize = fmt.get('filesize', 0)
|
| 31 |
+
if filesize:
|
| 32 |
+
filesize_mb = round(filesize / (1024 * 1024), 2)
|
| 33 |
+
resolution = f"{fmt['height']}p"
|
| 34 |
+
formats[resolution] = {
|
| 35 |
+
'id': fmt['format_id'],
|
| 36 |
+
'info': f"{resolution} - {filesize_mb} MB"
|
| 37 |
+
}
|
| 38 |
+
return formats
|
| 39 |
+
|
| 40 |
+
def reset_session_state():
|
| 41 |
+
st.session_state.pop('info_dict', None)
|
| 42 |
+
st.session_state.pop('thumbnail_url', None)
|
| 43 |
+
st.session_state.pop('formats', None)
|
| 44 |
+
st.session_state.pop('youtube_url', None)
|
| 45 |
+
st.session_state.pop('confirmed', None)
|
| 46 |
+
st.session_state.pop('selected_format_id', None)
|
| 47 |
+
|
| 48 |
+
def delete_downloaded_video():
|
| 49 |
+
if os.path.exists('downloaded_video.mp4'):
|
| 50 |
+
os.remove('downloaded_video.mp4')
|
| 51 |
+
|
| 52 |
+
# "νμΈ" λ²νΌμ λλ₯΄λ©΄ μ²λ¦¬ν λ‘μ§
|
| 53 |
+
if st.button('νμΈ'):
|
| 54 |
+
if youtube_url:
|
| 55 |
+
try:
|
| 56 |
+
# κΈ°μ‘΄ λ€μ΄λ‘λλ μμ μμ
|
| 57 |
+
delete_downloaded_video()
|
| 58 |
+
|
| 59 |
+
# μΈμ
μν μ΄κΈ°ν λ° URL λ³κ²½ νμΈ
|
| 60 |
+
if 'youtube_url' in st.session_state and st.session_state.youtube_url != youtube_url:
|
| 61 |
+
reset_session_state()
|
| 62 |
+
|
| 63 |
+
st.session_state.youtube_url = youtube_url
|
| 64 |
+
info_dict = fetch_video_info(youtube_url)
|
| 65 |
+
st.session_state.info_dict = info_dict
|
| 66 |
+
st.session_state.thumbnail_url = info_dict.get('thumbnail')
|
| 67 |
+
st.session_state.formats = get_available_formats(info_dict)
|
| 68 |
+
st.session_state.confirmed = True
|
| 69 |
+
except Exception as e:
|
| 70 |
+
st.error(f"An error occurred: {e}")
|
| 71 |
+
else:
|
| 72 |
+
st.warning('Please enter a valid YouTube video URL')
|
| 73 |
+
|
| 74 |
+
if 'confirmed' in st.session_state and st.session_state.confirmed:
|
| 75 |
+
st.image(st.session_state.thumbnail_url, caption='Video Thumbnail', use_column_width=True)
|
| 76 |
+
|
| 77 |
+
# ν΄μλ μ ν
|
| 78 |
+
format_options = [value['info'] for value in st.session_state.formats.values()]
|
| 79 |
+
format_selection = st.selectbox('Select resolution', format_options)
|
| 80 |
+
|
| 81 |
+
# μ νλ ν΄μλμ format_id μ μ₯
|
| 82 |
+
st.session_state.selected_format_id = next(
|
| 83 |
+
(value['id'] for key, value in st.session_state.formats.items() if value['info'] == format_selection), None
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def download_video(youtube_url, format_id):
|
| 87 |
+
ydl_opts = {
|
| 88 |
+
'format': f"{format_id}+bestaudio/best",
|
| 89 |
+
'merge_output_format': 'mp4',
|
| 90 |
+
'outtmpl': 'downloaded_video.%(ext)s',
|
| 91 |
+
'nocache': True, # μΊμ λΉνμ±ν
|
| 92 |
+
'force_generic_extractor': True # νμ μλ‘μ΄ URL μ¬μ©
|
| 93 |
+
}
|
| 94 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 95 |
+
ydl.download([youtube_url])
|
| 96 |
+
|
| 97 |
+
# "μΆμΆ" λ²νΌ μΆκ°
|
| 98 |
+
if st.button('μΆμΆ'):
|
| 99 |
+
format_id = st.session_state.selected_format_id
|
| 100 |
+
if format_id is None:
|
| 101 |
+
st.error('Selected format not found.')
|
| 102 |
+
else:
|
| 103 |
+
try:
|
| 104 |
+
# μΈμ
μνμ youtube_url μ¬μ©
|
| 105 |
+
download_video(st.session_state.youtube_url, format_id)
|
| 106 |
+
st.success('Video downloaded successfully!')
|
| 107 |
+
st.video('downloaded_video.mp4')
|
| 108 |
+
|
| 109 |
+
# λ€μ΄λ‘λλ νμΌ μ 곡 (optional)
|
| 110 |
+
with open('downloaded_video.mp4', 'rb') as file:
|
| 111 |
+
st.download_button(
|
| 112 |
+
label="Download Video File",
|
| 113 |
+
data=file,
|
| 114 |
+
file_name='downloaded_video.mp4',
|
| 115 |
+
mime='video/mp4'
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# μΆμΆλ μμ μ 보 μΆλ ₯
|
| 119 |
+
st.write(f"**μ λͺ©:** {st.session_state.info_dict['title']}")
|
| 120 |
+
st.write(f"**μ±λλͺ
:** {st.session_state.info_dict['uploader']}")
|
| 121 |
+
st.write(f"**μ
λ‘λ λ μ§:** {st.session_state.info_dict['upload_date']}")
|
| 122 |
+
|
| 123 |
+
# λ€μ΄λ‘λ ν μΈμ
μν 리μ
|
| 124 |
+
reset_session_state()
|
| 125 |
+
except Exception as e:
|
| 126 |
+
st.error(f"An error occurred during download: {e}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
yt-dlp
|
| 3 |
+
moviepy
|