JaMugen commited on
Commit
cc00532
·
1 Parent(s): 662dff2

Update Milestone5API.py

Browse files
Files changed (1) hide show
  1. Milestone5API.py +43 -43
Milestone5API.py CHANGED
@@ -1,48 +1,48 @@
1
- import streamlit as st
2
- import subprocess
 
 
3
 
4
- def install_libraries():
5
  try:
6
- subprocess.check_call(['pip', 'install', 'pytube'])
7
- subprocess.check_call(['pip', 'install', 'youtube_transcript_api'])
8
- print("Libraries installed successfully!")
9
- except subprocess.CalledProcessError as e:
10
- print("Error during installation:", e)
11
 
12
- install_libraries()
 
 
 
13
 
14
- def main():
15
- # Importing after ensuring dependencies are installed
16
- import Milestone5API
17
-
18
- # Title
19
- st.title("Translate YouTube Video to French")
20
-
21
- url = st.text_input("Enter YouTube Video URL")
22
- captions_list = [] # Initialize empty list for captions
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
- st.write(
39
- "<link rel='stylesheet' href='styles.css'>", unsafe_allow_html=True
40
- )
41
- st.write(
42
- "<table><tr><th class='timestamp'>Timestamp</th><th class='original'>Original</th></tr>",
43
- unsafe_allow_html=True,
44
- )
45
-
46
- for i in range(start_index, min(end_index, len(captions_list))):
47
- st.write(
48
- f"<tr><td class=
 
 
 
 
 
 
 
 
 
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