Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
| 4 |
-
from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclips
|
| 5 |
|
| 6 |
# Streamlit μ λͺ©
|
| 7 |
st.title('YouTube Video Downloader')
|
| 8 |
-
st.write("<br>", unsafe_allow_html=True)
|
| 9 |
st.write("μ¬μ©λ°©λ²")
|
| 10 |
-
st.write("<br>", unsafe_allow_html=True)
|
| 11 |
st.write("1. λ€μ΄λ°κ³ μνλ μ νλΈ μμμ λ§ν¬λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ")
|
| 12 |
st.write("2. λ§ν¬λ₯Ό μ
λ ₯νκ³ νμΈλ²νΌμ λλ₯΄λ©΄ ν΄λΉ μμμ μΈλ€μΌμ΄ λ±μ₯ν©λλ€")
|
| 13 |
st.write("3. μΈλ€μΌ λ°μ μ νλμ ν΅ν΄ ν΄μλμ μμ μ©λμ νμΈν ν μνλ ν΄μλλ₯Ό μ ννκ³ μΆμΆ λ²νΌμ λλ¬μ£ΌμΈμ")
|
|
@@ -25,18 +24,15 @@ def fetch_video_info(youtube_url):
|
|
| 25 |
info_dict = ydl.extract_info(youtube_url, download=False)
|
| 26 |
return info_dict
|
| 27 |
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
formats = {height: None for height in target_heights}
|
| 31 |
-
|
| 32 |
for fmt in info_dict['formats']:
|
| 33 |
-
if fmt.get('height')
|
| 34 |
filesize = fmt.get('filesize', 0)
|
| 35 |
if filesize:
|
| 36 |
filesize_mb = round(filesize / (1024 * 1024), 2)
|
| 37 |
-
formats
|
| 38 |
-
|
| 39 |
-
return [fmt for fmt in formats.values() if fmt is not None]
|
| 40 |
|
| 41 |
if st.button('νμΈ'):
|
| 42 |
if youtube_url:
|
|
@@ -53,7 +49,7 @@ if st.button('νμΈ'):
|
|
| 53 |
info_dict = fetch_video_info(youtube_url)
|
| 54 |
st.session_state.info_dict = info_dict
|
| 55 |
st.session_state.thumbnail_url = info_dict.get('thumbnail')
|
| 56 |
-
st.session_state.formats =
|
| 57 |
st.session_state.confirmed = True
|
| 58 |
except Exception as e:
|
| 59 |
st.error(f"An error occurred: {e}")
|
|
@@ -66,46 +62,32 @@ if st.session_state.confirmed:
|
|
| 66 |
# ν΄μλ μ ν
|
| 67 |
format_selection = st.selectbox('Select resolution', st.session_state.formats)
|
| 68 |
|
| 69 |
-
def
|
| 70 |
-
|
| 71 |
-
'format': format_id,
|
|
|
|
| 72 |
'outtmpl': 'downloaded_video.%(ext)s',
|
| 73 |
-
'
|
| 74 |
-
|
| 75 |
-
audio_opts = {
|
| 76 |
-
'format': 'bestaudio',
|
| 77 |
-
'outtmpl': 'downloaded_audio.%(ext)s',
|
| 78 |
-
'nocheckcertificate': True,
|
| 79 |
}
|
| 80 |
-
|
| 81 |
-
with yt_dlp.YoutubeDL(video_opts) as ydl:
|
| 82 |
ydl.download([youtube_url])
|
| 83 |
-
|
| 84 |
-
with yt_dlp.YoutubeDL(audio_opts) as ydl:
|
| 85 |
-
ydl.download([youtube_url])
|
| 86 |
-
|
| 87 |
-
def combine_video_audio(video_path, audio_path, output_path):
|
| 88 |
-
video_clip = VideoFileClip(video_path)
|
| 89 |
-
audio_clip = AudioFileClip(audio_path)
|
| 90 |
-
final_clip = video_clip.set_audio(audio_clip)
|
| 91 |
-
final_clip.write_videofile(output_path, codec="libx264")
|
| 92 |
|
| 93 |
# "μΆμΆ" λ²νΌ μΆκ°
|
| 94 |
if st.button('μΆμΆ'):
|
| 95 |
-
format_id = format_selection.split('
|
| 96 |
try:
|
| 97 |
# μΈμ
μνμ youtube_url μ¬μ©
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
st.
|
| 101 |
-
st.video('final_video.mp4')
|
| 102 |
|
| 103 |
# λ€μ΄λ‘λλ νμΌ μ 곡 (optional)
|
| 104 |
-
with open('
|
| 105 |
st.download_button(
|
| 106 |
label="Download Video File",
|
| 107 |
data=file,
|
| 108 |
-
file_name='
|
| 109 |
mime='video/mp4'
|
| 110 |
)
|
| 111 |
except Exception as e:
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import yt_dlp
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
# Streamlit μ λͺ©
|
| 6 |
st.title('YouTube Video Downloader')
|
| 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. μΈλ€μΌ λ°μ μ νλμ ν΅ν΄ ν΄μλμ μμ μ©λμ νμΈν ν μνλ ν΄μλλ₯Ό μ ννκ³ μΆμΆ λ²νΌμ λλ¬μ£ΌμΈμ")
|
|
|
|
| 24 |
info_dict = ydl.extract_info(youtube_url, download=False)
|
| 25 |
return info_dict
|
| 26 |
|
| 27 |
+
def get_available_formats(info_dict):
|
| 28 |
+
formats = []
|
|
|
|
|
|
|
| 29 |
for fmt in info_dict['formats']:
|
| 30 |
+
if fmt.get('height') and fmt.get('ext') == 'mp4':
|
| 31 |
filesize = fmt.get('filesize', 0)
|
| 32 |
if filesize:
|
| 33 |
filesize_mb = round(filesize / (1024 * 1024), 2)
|
| 34 |
+
formats.append(f"{fmt['height']}p - {filesize_mb} MB")
|
| 35 |
+
return formats
|
|
|
|
| 36 |
|
| 37 |
if st.button('νμΈ'):
|
| 38 |
if youtube_url:
|
|
|
|
| 49 |
info_dict = fetch_video_info(youtube_url)
|
| 50 |
st.session_state.info_dict = info_dict
|
| 51 |
st.session_state.thumbnail_url = info_dict.get('thumbnail')
|
| 52 |
+
st.session_state.formats = get_available_formats(info_dict)
|
| 53 |
st.session_state.confirmed = True
|
| 54 |
except Exception as e:
|
| 55 |
st.error(f"An error occurred: {e}")
|
|
|
|
| 62 |
# ν΄μλ μ ν
|
| 63 |
format_selection = st.selectbox('Select resolution', st.session_state.formats)
|
| 64 |
|
| 65 |
+
def download_video(youtube_url, format_id):
|
| 66 |
+
ydl_opts = {
|
| 67 |
+
'format': f"{format_id}+bestaudio/best",
|
| 68 |
+
'merge_output_format': 'mp4',
|
| 69 |
'outtmpl': 'downloaded_video.%(ext)s',
|
| 70 |
+
'nocache': True, # μΊμ λΉνμ±ν
|
| 71 |
+
'force_generic_extractor': True # νμ μλ‘μ΄ URL μ¬μ©
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
|
|
| 74 |
ydl.download([youtube_url])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# "μΆμΆ" λ²νΌ μΆκ°
|
| 77 |
if st.button('μΆμΆ'):
|
| 78 |
+
format_id = format_selection.split('p')[0]
|
| 79 |
try:
|
| 80 |
# μΈμ
μνμ youtube_url μ¬μ©
|
| 81 |
+
download_video(st.session_state.youtube_url, format_id)
|
| 82 |
+
st.success('Video downloaded successfully!')
|
| 83 |
+
st.video('downloaded_video.mp4')
|
|
|
|
| 84 |
|
| 85 |
# λ€μ΄λ‘λλ νμΌ μ 곡 (optional)
|
| 86 |
+
with open('downloaded_video.mp4', 'rb') as file:
|
| 87 |
st.download_button(
|
| 88 |
label="Download Video File",
|
| 89 |
data=file,
|
| 90 |
+
file_name='downloaded_video.mp4',
|
| 91 |
mime='video/mp4'
|
| 92 |
)
|
| 93 |
except Exception as e:
|