Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,10 @@ from pydub import AudioSegment
|
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Function to convert video to audio
|
| 10 |
def video_to_audio(video_file):
|
|
@@ -51,12 +55,53 @@ def transcribe_audio(audio_file):
|
|
| 51 |
except sr.RequestError:
|
| 52 |
return "Could not request results from Google Speech Recognition service."
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Streamlit app layout
|
| 55 |
st.title("Video and Audio to Text Transcription")
|
| 56 |
-
st.write("Upload a video or audio file to convert it to transcription.")
|
| 57 |
|
| 58 |
-
# Create tabs to separate video and
|
| 59 |
-
tab = st.selectbox("Select the type of file to upload", ["Video", "Audio"])
|
| 60 |
|
| 61 |
if tab == "Video":
|
| 62 |
# File uploader for video
|
|
@@ -173,4 +218,19 @@ elif tab == "Audio":
|
|
| 173 |
data=st.session_state.wav_audio_file_audio,
|
| 174 |
file_name="converted_audio_audio.wav",
|
| 175 |
mime="audio/wav"
|
| 176 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
import io
|
| 8 |
+
import requests
|
| 9 |
+
import execjs
|
| 10 |
+
import re
|
| 11 |
+
import json
|
| 12 |
|
| 13 |
# Function to convert video to audio
|
| 14 |
def video_to_audio(video_file):
|
|
|
|
| 55 |
except sr.RequestError:
|
| 56 |
return "Could not request results from Google Speech Recognition service."
|
| 57 |
|
| 58 |
+
# Function to get HTML content for extracting video URL
|
| 59 |
+
def gethtml(url):
|
| 60 |
+
headers = {
|
| 61 |
+
"cache-Control": "no-cache",
|
| 62 |
+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
|
| 63 |
+
"accept-encoding": "gzip, deflate, br",
|
| 64 |
+
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
|
| 65 |
+
"content-type": "application/x-www-form-urlencoded",
|
| 66 |
+
"cookie": "lang=en; country=CN; uid=fd94a82a406a8dd4; sfHelperDist=72; reference=14;",
|
| 67 |
+
"origin": "https://en.savefrom.net",
|
| 68 |
+
"referer": "https://en.savefrom.net/1-youtube-video-downloader-4/",
|
| 69 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
| 70 |
+
}
|
| 71 |
+
kv = {"sf_url": url, "sf_submit": "", "new": "1", "lang": "en", "app": "", "country": "cn", "os": "Windows", "browser": "Chrome"}
|
| 72 |
+
r = requests.post(url="https://en.savefrom.net/savefrom.php", headers=headers, data=kv)
|
| 73 |
+
r.raise_for_status()
|
| 74 |
+
return r.text
|
| 75 |
+
|
| 76 |
+
# Function to extract the video download URL
|
| 77 |
+
def extract_video_url(youtube_url):
|
| 78 |
+
reo = gethtml(youtube_url)
|
| 79 |
+
reo = reo.split("<script type=\"text/javascript\">")[1].split("</script>")[0]
|
| 80 |
+
reo = reo.replace("(function(){", "(function(){\nthis.alert=function(){};")
|
| 81 |
+
reA = reo.split("\n")
|
| 82 |
+
name = reA[len(reA) - 3].split(";")[0] + ";"
|
| 83 |
+
addition = """
|
| 84 |
+
const jsdom = require("jsdom");
|
| 85 |
+
const { JSDOM } = jsdom;
|
| 86 |
+
const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
|
| 87 |
+
window = dom.window;
|
| 88 |
+
document = window.document;
|
| 89 |
+
XMLHttpRequest = window.XMLHttpRequest;
|
| 90 |
+
"""
|
| 91 |
+
ct = execjs.compile(addition + reo, cwd=r'C:\Users\19308\AppData\Roaming\npm\node_modules')
|
| 92 |
+
text = ct.eval(name.split("=")[1].replace(";", ""))
|
| 93 |
+
result = re.search('show\((.*?)\);;', text, re.I | re.M).group(0).replace("show(", "").replace(");;", "")
|
| 94 |
+
j = json.loads(result)
|
| 95 |
+
num = 1
|
| 96 |
+
downurl = j["url"][num]["url"]
|
| 97 |
+
return downurl
|
| 98 |
+
|
| 99 |
# Streamlit app layout
|
| 100 |
st.title("Video and Audio to Text Transcription")
|
| 101 |
+
st.write("Upload a video or audio file to convert it to transcription, or enter a YouTube URL to download the video.")
|
| 102 |
|
| 103 |
+
# Create tabs to separate video, audio, and YouTube download options
|
| 104 |
+
tab = st.selectbox("Select the type of file to upload or download", ["Video", "Audio", "YouTube"])
|
| 105 |
|
| 106 |
if tab == "Video":
|
| 107 |
# File uploader for video
|
|
|
|
| 218 |
data=st.session_state.wav_audio_file_audio,
|
| 219 |
file_name="converted_audio_audio.wav",
|
| 220 |
mime="audio/wav"
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
elif tab == "YouTube":
|
| 224 |
+
youtube_url = st.text_input("Enter YouTube Video URL", "https://www.youtube.com/watch?v=YPvtz1lHRiw")
|
| 225 |
+
|
| 226 |
+
if st.button("Get Download Link"):
|
| 227 |
+
if youtube_url:
|
| 228 |
+
try:
|
| 229 |
+
download_url = extract_video_url(youtube_url)
|
| 230 |
+
st.success("Download link generated successfully!")
|
| 231 |
+
st.write("Click below to download the video:")
|
| 232 |
+
st.markdown(f"[Download Video]({download_url})", unsafe_allow_html=True)
|
| 233 |
+
except Exception as e:
|
| 234 |
+
st.error(f"Error occurred: {e}")
|
| 235 |
+
else:
|
| 236 |
+
st.error("Please enter a valid YouTube URL.")
|