-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
191 lines (168 loc) · 5.74 KB
/
streamlit_app.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from __future__ import unicode_literals
from requests import post
import streamlit as st
from qrcode.main import QRCode
from qrcode import ERROR_CORRECT_H
import yt_dlp as yt
from os import remove
from os.path import isfile
from ffmpeg import FFmpeg
import webvtt
from subprocess import call
st.set_page_config(
page_title="URL 도구",
layout="wide",
initial_sidebar_state="collapsed",
)
ct = st.container()
ct.title('URL 도구')
bar = None
status = None
filename: str = ""
def url_shorten():
if url == "" or url is None:
st.error("URL을 입력해주세요!")
return
response = post(url="http://www.buly.kr/api/shoturl.siso",
data={"customer_id": st.secrets["ID"], "partner_api_id": st.secrets["API_key"], "org_url": url})
data = response.json()
print(data)
st.info("단축된 URL : " + data['url'])
return
def qr_code():
if url == "" or url is None:
st.error("URL을 입력해주세요!")
return
q = QRCode(
version=1,
error_correction=ERROR_CORRECT_H,
box_size=10,
border=4,
)
q.add_data(url)
q.make(fit=True)
img = q.make_image(fill_color="black", back_color="transparent")
st.image(img.get_image())
img.save("qrcode.png")
st.download_button("QR코드 PNG 다운로드", open("qrcode.png", "rb").read(), "QRcode.png", "image/png")
remove("qrcode.png")
return
def rm():
global filename
remove(filename)
st.empty()
def audio():
if url == "" or url is None:
st.error("URL을 입력해주세요!")
return
global bar, filename, status
bar = st.progress(0, text="파일 준비 중입니다...잠시만 기다려주세요...")
status = st.status(label="파일 준비 중입니다...잠시만 기다려주세요...", expanded=True)
ydl_opts = {
'ignoreerrors': True,
'nooverwrites': True,
'format': '251',
'outtmpl': "%(fulltitle)s.mp3",
'noplaylist': True,
'lazy_playlist': True,
'merge_output_format': 'mp3',
'audioquality': '0',
'audioformat': 'mp3',
'addmetadata': True,
'extractaudio': True,
'embedthumnail': True,
'quiet': True,
'progress_hooks': [hook],
}
with yt.YoutubeDL(ydl_opts) as ydl:
ydl.download([str(url)])
while not isfile(filename):
pass
ffmpeg = (
FFmpeg()
.input(filename)
.output("temp.wav")
)
ffmpeg.execute()
bar.empty()
status.update(label="다운로드 완료", state="complete", expanded=False)
st.audio("temp.wav")
st.download_button("다운로드", data=open(filename, "rb").read(), file_name=filename, mime="audio/mpeg", on_click=rm)
return
def hook(d):
global bar, filename, status
if d['status'] == 'error':
status.update(label="오류 발생", state="error", expanded=False)
st.rerun()
return
try:
filename = d['filename']
except Exception:
pass
try:
bar.progress(float(d["_percent_str"][:-1]) / 100.0, "파일 준비 중입니다...잠시만 기다려주세요...")
status.write("[download]" + d['_default_template'])
except Exception:
pass
def video():
if url == "" or url is None:
st.error("URL을 입력해주세요!")
return
global bar, filename, status
bar = st.progress(0, text="파일 준비 중입니다...잠시만 기다려주세요...")
status = st.status(label="파일 준비 중입니다...잠시만 기다려주세요...", expanded=True)
ydl_opts = {
'ignoreerrors': True,
'nooverwrites': True,
'format': 'bestvideo[vcodec=h264]+bestaudio[acodec=aac]/best/best',
'outtmpl': '%(fulltitle)s.mp4',
'noplaylist': True,
'lazy_playlist': True,
'merge_output_format': 'mp4',
'audioquality': '0',
'audioformat': 'best',
'addmetadata': False,
'extractaudio': False,
'embedthumnail': False,
'quiet': True,
'progress_hooks': [hook],
}
with yt.YoutubeDL(ydl_opts) as ydl:
ydl.download([str(url)])
while not isfile(filename):
pass
bar.empty()
status.update(label="다운로드 완료", state="complete", expanded=False)
st.video(data=filename)
st.download_button("다운로드", data=open(filename, "rb").read(), file_name=filename, mime="video/mp4", on_click=rm)
return
def subtitle():
if url == "" or url is None:
st.error("URL을 입력해주세요!")
return
call(['yt-dlp', '-ciw', '-q', '--skip-download', '--write-sub', '--sub-lang', 'ko', '-o', 'temp', str(url)])
while not isfile("temp.ko.vtt"):
pass
text = "> "
filetext = ""
for caption in webvtt.read('temp.ko.vtt'):
text += caption.text
filetext += caption.text
text += "<br/>"
filetext += "\n"
st.markdown(text, unsafe_allow_html=True)
st.download_button(label="다운로드", data=filetext, file_name="스크립트.txt", mime="text/plain")
remove("temp.ko.vtt")
return
url = ct.text_input("URL을 입력하세요")
col1, col2, col3, col4, col5 = st.columns([3.5, 4, 5, 5, 6])
with col1:
button1 = st.button("URL 단축하기", on_click=url_shorten, use_container_width=True)
with col2:
button2 = st.button("QR코드 생성하기", on_click=qr_code, use_container_width=True)
with col3:
button3 = st.button("유튜브 동영상 다운로드", on_click=video, use_container_width=True, disabled=True)
with col4:
button4 = st.button("유튜브 오디오 다운로드", on_click=audio, use_container_width=True, disabled=True)
with col5:
button5 = st.button("유튜브 자막 스크립트 다운로드", on_click=subtitle, use_container_width=True)