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