Spaces:
Runtime error
Runtime error
tomaseo2022
commited on
Commit
•
710506c
1
Parent(s):
fcf76e3
Upload convert.py
Browse files- convert.py +58 -0
convert.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytube
|
2 |
+
from moviepy.editor import VideoFileClip
|
3 |
+
import pywhisper
|
4 |
+
import os
|
5 |
+
|
6 |
+
def download_video(url):
|
7 |
+
video = pytube.YouTube(url)
|
8 |
+
stream = video.streams.get_by_itag(18)
|
9 |
+
stream.download()
|
10 |
+
return stream.default_filename
|
11 |
+
|
12 |
+
def convert_to_mp3(filename):
|
13 |
+
clip = VideoFileClip(filename)
|
14 |
+
clip.audio.write_audiofile(filename[:-4] + ".mp3")
|
15 |
+
clip.close()
|
16 |
+
|
17 |
+
def AudiotoText(filename):
|
18 |
+
model = pywhisper.load_model("base")
|
19 |
+
result = model.transcribe(filename)
|
20 |
+
print(result["text"])
|
21 |
+
sonuc = result["text"]
|
22 |
+
return sonuc
|
23 |
+
|
24 |
+
def main(url):
|
25 |
+
print('''
|
26 |
+
This tool will convert Youtube videos to mp3 files and then transcribe them to text using Whisper.
|
27 |
+
''')
|
28 |
+
|
29 |
+
print("Downloading video... Please wait.")
|
30 |
+
try:
|
31 |
+
filename = download_video(url)
|
32 |
+
print("Downloaded video as " + filename)
|
33 |
+
except:
|
34 |
+
print("Not a valid link..")
|
35 |
+
return
|
36 |
+
try:
|
37 |
+
convert_to_mp3(filename)
|
38 |
+
print("Converted video to mp3")
|
39 |
+
except:
|
40 |
+
print("Error converting video to mp3")
|
41 |
+
return
|
42 |
+
try:
|
43 |
+
model = pywhisper.load_model("base")
|
44 |
+
result = model.transcribe(filename[:-4] + ".mp3")
|
45 |
+
print(result["text"])
|
46 |
+
result = result["text"]
|
47 |
+
os.remove(filename)
|
48 |
+
os.remove(filename[:-4] + ".mp3")
|
49 |
+
print("Removed video and audio files")
|
50 |
+
print("Done!")
|
51 |
+
return result
|
52 |
+
except:
|
53 |
+
print("Error transcribing audio to text")
|
54 |
+
return
|
55 |
+
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
main()
|