Spaces:
Sleeping
Sleeping
Update Milestone5API.py
Browse files- Milestone5API.py +43 -43
Milestone5API.py
CHANGED
|
@@ -1,48 +1,48 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
try:
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if url:
|
| 25 |
-
captions = Milestone5API.download_video_transcript(url)
|
| 26 |
-
if captions:
|
| 27 |
-
# Parse captions from the API response
|
| 28 |
-
for line in captions.split("\n"):
|
| 29 |
-
if line.strip():
|
| 30 |
-
timestamp, text = line.split(" ", 1)
|
| 31 |
-
captions_list.append({"Timestamp": timestamp, "Original": text})
|
| 32 |
-
else:
|
| 33 |
-
st.write("Provided URL did not contain captions")
|
| 34 |
-
|
| 35 |
-
start_index = st.session_state.get('start_index', 0)
|
| 36 |
-
end_index = start_index + 6
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pytube import YouTube
|
| 2 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 3 |
+
import re
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
def download_video_transcript(video_url):
|
| 7 |
try:
|
| 8 |
+
video_id = re.search(r"(?<=v=)[\w-]+", video_url)
|
| 9 |
+
if video_id:
|
| 10 |
+
video_id = video_id.group()
|
| 11 |
+
yt = YouTube(video_url)
|
| 12 |
+
stream = yt.streams.get_highest_resolution()
|
| 13 |
|
| 14 |
+
print(f'Downloading {yt.title}...')
|
| 15 |
+
modified_title = yt.title.replace(" ", "_")
|
| 16 |
+
download_path = 'CS370_Milestone5/videos' # Updated path to CS370_Milestone5 repository
|
| 17 |
+
captions_path = 'CS370_Milestone5/captions' # Updated path to CS370_Milestone5 repository
|
| 18 |
|
| 19 |
+
# Create directories if they don't exist
|
| 20 |
+
os.makedirs(download_path, exist_ok=True)
|
| 21 |
+
os.makedirs(captions_path, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
video_file = f'{download_path}/{modified_title}.mp4'
|
| 24 |
+
stream.download(output_path=download_path, filename=modified_title + '.mp4')
|
| 25 |
+
print('Download completed!')
|
| 26 |
+
|
| 27 |
+
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 28 |
+
transcript = transcript_list.find_generated_transcript(['en']).fetch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
captions = ""
|
| 31 |
+
for i, line in enumerate(transcript):
|
| 32 |
+
start_time = line['start']
|
| 33 |
+
formatted_time = f"{int(start_time // 60):02d}:{int(start_time % 60):02d}"
|
| 34 |
+
captions += f"{formatted_time} {line['text']}\n"
|
| 35 |
+
|
| 36 |
+
with open(f'{captions_path}/{modified_title}.txt', 'w', encoding='utf-8') as file:
|
| 37 |
+
file.write(captions)
|
| 38 |
+
|
| 39 |
+
# Delete video file after captions are saved
|
| 40 |
+
os.remove(video_file)
|
| 41 |
+
|
| 42 |
+
return captions
|
| 43 |
+
else:
|
| 44 |
+
print("Video ID not found in URL.")
|
| 45 |
+
return None
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print("Error:", e)
|
| 48 |
+
return None
|