[Python] How to properly convert Pytube bufferIO to numpy array representing sine wave?

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    Problem: I'm trying to download audio from Youtube via Pytube, save it to buffer (I don't want to save the audio to drive) and then convert it to numpy array in order to plot a raw audio wave. After conversion I'm trying to convert it back to sound and instead of 12 minutes of audio from a video, I'm getting 12 seconds of static noise. When saving buffer to .mp4 everything seems to be working just fine. I'm quite lost and would appreciate any help.

    Program:

    from IPython.display import Audio
    from pytube import YouTube
    import numpy as np
    from io import BytesIO

    yt = YouTube('https://www.youtube.com/watch?v=zrqqrQmeQS4')

    # getting audio stream
    audioStreams = list(map(lambda streamObj: streamObj.itag, yt.streams.filter(only_audio=True)))
    activeStream = yt.streams.get_by_itag(audioStreams[0])

    # saving to buffer
    bufferObj = BytesIO()
    activeStream.stream_to_buffer(bufferObj)

    # saving buffer to ndarray
    bufferObj.seek(0)
    points_array = np.frombuffer(bufferObj.read())

    # preprocessing raw numpy data
    points_array = np.round(points_array,10)
    points_array[np.isinf(points_array) | np.isnan(points_array)] = 0

    return Audio(points_array, rate=44.1 * 1000)

    Continue reading...

Compartilhe esta Página