-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
43 lines (35 loc) Β· 1.52 KB
/
test.py
File metadata and controls
43 lines (35 loc) Β· 1.52 KB
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
import re
from youtube_transcript_api import (
YouTubeTranscriptApi,
TranscriptsDisabled,
NoTranscriptFound
)
def main():
url = input("π₯ μ νλΈ μμ λ§ν¬λ₯Ό μ
λ ₯νμΈμ: ").strip()
match = re.search(r"(?:v=|youtu\.be/)([A-Za-z0-9_-]{11})", url)
if not match:
print("β μλͺ»λ μ νλΈ λ§ν¬μ
λλ€.")
return
video_id = match.group(1)
try:
# 1. YouTubeTranscriptApi μΈμ€ν΄μ€ μμ± (v1.2.3 곡μ μ¬μ©λ²)
ytt_api = YouTubeTranscriptApi()
# 2. μΈμ€ν΄μ€ λ©μλμΈ fetch()λ₯Ό μ¬μ©νμ¬ μλ§μ κ°μ Έμ΅λλ€. (νκ΅μ΄, μμ΄ μ)
transcript_data = ytt_api.fetch(
video_id,
languages=['ko', 'en']
)
# π¨ μ΅μ’
μμ : FetchedTranscriptSnippet κ°μ²΄μμ 'text' μμ±μΌλ‘ μ κ·Όν©λλ€.
# item["text"] λμ item.textλ₯Ό μ¬μ©ν©λλ€.
script = "\n".join([item.text for item in transcript_data])
print("\nβ
μλ§μ μ±κ³΅μ μΌλ‘ κ°μ Έμμ΅λλ€.\n")
print("π μλ§ λ΄μ©:\n")
print(script)
except TranscriptsDisabled:
print("β μ€λ₯: ν΄λΉ μμμ μλ§μ΄ λΉνμ±νλμ΄ μμ΅λλ€.")
except NoTranscriptFound:
print("β μ€λ₯: μμ²ν μΈμ΄(νκ΅μ΄, μμ΄) μλ§μ μ°Ύμ μ μμ΅λλ€.")
except Exception as e:
print(f"β μλ§μ κ°μ Έμ€λ μ€ μ΅μ’
μ€λ₯ λ°μ: {e}")
if __name__ == "__main__":
main()