Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytube as pt
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def second_to_timecode(x: float) -> str:
|
| 5 |
+
"""Float x second to HH:MM:SS.DDD format."""
|
| 6 |
+
hour, x = divmod(x, 3600)
|
| 7 |
+
minute, x = divmod(x, 60)
|
| 8 |
+
second, x = divmod(x, 1)
|
| 9 |
+
millisecond = int(x * 1000.)
|
| 10 |
+
|
| 11 |
+
return '%.1d:%.2d:%.2d.%.3d' % (hour, minute, second, millisecond)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def download_from_youtube(youtube_link: str) -> str:
|
| 15 |
+
yt = pt.YouTube(youtube_link)
|
| 16 |
+
available_streams = yt.streams.filter(only_audio=True)
|
| 17 |
+
print('available streams:')
|
| 18 |
+
print(available_streams)
|
| 19 |
+
stream = available_streams.first()
|
| 20 |
+
# , audio_codec='wav'
|
| 21 |
+
|
| 22 |
+
stream.download(filename="audio.wav")
|
| 23 |
+
return "audio.wav"
|