Example
Downloading video
import MyTube
import asyncio
def mb(bytes_value):
return f"{round(bytes_value / (1024 * 1024))} MB"
async def progress(current, total):
print(mb(current), "/", mb(total), end="\r")
async def ffmpeg_progress(current, total):
print("Merging:", round(current * 100 / total), "%", end="\r")
async def main():
yt = MyTube.YouTube("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
video = yt.streams.best_video()
audio = yt.streams.best_audio()
print(video)
print(audio)
file = await yt.download(video=video, audio=audio)(
"downloads", on_progress=progress, ffmpeg_progress=ffmpeg_progress
)
print(file)
asyncio.run(main())
Video(1920x1080.mp4 [25fps] av01.0.08M.08)
Audio(129.m4a [en] mp4a.40.2)
0 MB / 31 MB
[pause]
10 MB / 31 MB
[pause]
20 MB / 31 MB
[pause]
28 MB / 31 MB
[pause]
31 MB / 31 MB
Merging: 0 %
[pause]
Merging: 15 %
[pause]
Merging: 30 %
[pause]
Merging: 46 %
[pause]
Merging: 61 %
[pause]
Merging: 77 %
[pause]
Merging: 94 %
[pause]
Merging: 100 %
C:\Python\MyTube\downloads\Rick Astley - Never Gonna Give You Up (Official Music Video).mp4
Downloading music
import MyTube
import asyncio
def mb(bytes_value):
return f"{round(bytes_value / (1024 * 1024))} MB"
async def progress(current, total):
print(mb(current), "/", mb(total), end="\r")
async def main():
yt = MyTube.YouTube("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
audio = yt.streams.best_audio()
print(audio)
file = await yt.download(audio=audio)("downloads", "music", on_progress=progress)
print(file)
asyncio.run(main())
Audio(129.m4a [en] mp4a.40.2)
0 MB / 3 MB
[pause]
3 MB / 3 MB
C:\Python\MyTube\downloads\music.mp3
Downloading muxed
import MyTube
import asyncio
def mb(bytes_value):
return f"{round(bytes_value / (1024 * 1024))} MB"
async def progress(current, total):
print(mb(current), "/", mb(total), end="\r")
async def main():
yt = MyTube.YouTube("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
stream = yt.streams.filter(only_muxed=True).first()
print(stream)
file = await yt.download(video=stream)("downloads", on_progress=progress)
print(file)
asyncio.run(main())
Muxed(640x360.mp4 [25fps] [en] avc1.42001E+mp4a.40.2)
0 MB / 9 MB
[pause]
9 MB / 9 MB
C:\Python\MyTube\downloads\Rick Astley - Never Gonna Give You Up (Official Music Video) (1).mp4
Downloading stream
import MyTube
import asyncio
def mb(bytes_value):
return f"{round(bytes_value / (1024 * 1024))} MB"
async def progress(current, total):
print(mb(current), "/", mb(total), end="\r")
async def main():
yt = MyTube.YouTube("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
stream = yt.streams.filter(
only_video=True, no_muxed=True, max_res=720
).order_by("res").first()
print(stream)
file = await stream.download("downloads", on_progress=progress)
print(file)
asyncio.run(main())
Video(1280x720.mp4 [25fps] av01.0.05M.08)
0 MB / 15 MB
[pause]
10 MB / 15 MB
[pause]
15 MB / 15 MB
C:\Python\MyTube\downloads\Rick Astley - Never Gonna Give You Up (Official Music Video) (2).mp4
Aborting process
import MyTube
import asyncio
from threading import Timer
def mb(bytes_value):
return f"{round(bytes_value / (1024 * 1024))} MB"
async def progress(current, total):
print(mb(current), "/", mb(total), end="\r")
async def main():
yt = MyTube.YouTube("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
video = yt.streams.best_video()
audio = yt.streams.best_audio()
print(video)
print(audio)
downloader = yt.download(video=video, audio=audio)
Timer(2, lambda: downloader.abort()).start()
file = await downloader("downloads", on_progress=progress)
asyncio.run(main())