-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTranscribeYouTubeVideo.py
84 lines (71 loc) · 2.88 KB
/
TranscribeYouTubeVideo.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
from datetime import timedelta
import os
import whisper
from pytube import Playlist
from pytube import YouTube
import torch
import sys
def transcribe_audio(path, file_name):
model = whisper.load_model("large") # Change this to your desired model
print("Whisper model loaded.")
transcribe = model.transcribe(audio=path)
segments = transcribe['segments']
for segment in segments:
startTime = str(0)+str(timedelta(seconds=int(segment['start'])))+',000'
endTime = str(0)+str(timedelta(seconds=int(segment['end'])))+',000'
text = segment['text']
segmentId = segment['id']+1
segment = f"{segmentId}\n{startTime} --> {endTime}\n{text[1:] if text[0] == ' ' else text}\n\n"
srtFilename = f'{file_name}.srt'
with open(srtFilename, 'a', encoding='utf-8') as srtFile:
srtFile.write(segment)
del model.encoder
del model.decoder
torch.cuda.empty_cache()
return srtFilename
def download_audio_as_wav(yt, video_title):
stream = yt.streams.filter(only_audio=True).first()
if stream:
print(f"Downloading audio for '{video_title}'...")
stream.download(filename=f"{video_title}.mp3")
# Here you can convert the downloaded mp3 file to WAV if you prefer, using a library like pydub.
# The conversion process requires the pydub library and ffmpeg.
# Example code for conversion:
# from pydub import AudioSegment
# audio = AudioSegment.from_mp3(f"{video_title}.mp3")
# audio.export(f"{video_title}.wav", format="wav")
print(f"Audio downloaded for '{video_title}', starts converting..")
transcribe_audio(f"{video_title}.mp3", video_title)
os.remove(f"{video_title}.mp3")
else:
print(f"No audio available for '{video_title}'")
def main():
if len(sys.argv) > 1:
playlist_link = sys.argv[1]
all = True
else:
playlist_link = input("Enter the link to the YouTube playlist: ")
all = False
playlist = Playlist(playlist_link)
playlist._video_regex = r"\"url\":\"(/watch\?v=[\w-]*)"
videos = playlist.video_urls
for video_url in videos:
choice = ""
yt = YouTube(video_url)
video_title = yt.title
if not all:
print(f"Video Title: {video_title}")
choice = input("Do you want to (s)kip, (p)roceed or download (a)ll? (s/p/a): ").lower()
if choice == "a":
all = True
if choice == "s":
print(f"Skipping '{video_title}'")
elif choice == "p" or all:
download_audio_as_wav(yt, video_title)
else:
print("Invalid choice. Skipping...")
if __name__ == "__main__":
main()
#model = whisper.load_model("large")
#result = model.transcribe("output_000.wav")
#print(result["text"])