I'm trying to get a stream from Youtube and stream it via HTTP, but I have to use separate audio and video streams to get good quality. I don't know how to merge the streams on the fly with ffmpeg
. (I'm using pytube
)
I know I can do this by saving the streams, then merging them, and then streaming the output, but I would like to do it on the fly. This is my current code (which uses the old progressive streams):
yt = YouTube(f"https://www.youtube.com/watch?v={url}")
video = yt.streams.filter(file_extension='mp4', progressive=True).first()
async def stream(response):
for chunk in pytube_request.stream(video.url):
await response.write(chunk)
return ResponseStream(stream, content_type="audio/mp4")