Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Nitan Alexandru Marcel
    @nitanmarcel
    Thanks @quantotto I'll try this. Btw I can't use AudioFifo with av.open(AudioFifo)? I tried that and it says that NoneType has no attribute sample_rate even tho AudioFifo was initiated
    Also is the conversion right? Because I just did length / 8 (8 being the bytes/sample) and even tho the length of bytes was correct the sound came distorted with white noises (hints to a bad number of bytes or bad format)
    Nitan Alexandru Marcel
    @nitanmarcel
    @quantotto done I did it
    ~~~async def main():
    async with client:
        fifo: av.AudioFifo = av.AudioFifo(format="s16le")
        resampler: av.AudioResampler = av.AudioResampler(
            format="s16", layout="stereo", rate=48000)
    
        def on_played_data(_, length):
            data = fifo.read(length / 4)
            if data:
                data = data.to_ndarray().tobytes()
            return data
    
        group_call_factory = GroupCallFactory(client, CLIENT_TYPE)
        group_call = group_call_factory.get_raw_group_call(
            on_played_data=on_played_data)
    
        await group_call.start(PEER)
        while not group_call.is_connected:
            await asyncio.sleep(1)
    
        input_ = av.open(STREAM)
        for frame in input_.decode():
            if frame:
                print(frame)
                frame = resampler.resample(frame)
                fifo.write(frame)
    
        await client.run_until_disconnected()~~~
    This is the working code for me
    Nitan Alexandru Marcel
    @nitanmarcel
    Thanks to everyone who put up with me and gave me tips. Now I just need to know about the AudioFifo. If I have the radio stream and let it run for a long time will the stored bytes increase the ram usage or once they are read they are removed?
    Because if not I might need to create my own implementation
    quantotto
    @quantotto
    if you are reading fast enough, memory shouldn't increase indefinitely.
    Nitan Alexandru Marcel
    @nitanmarcel
    nice, thanks
    got a weird error when I'm using it from a file name
        _input = av.open(SOURCE, metadata_errors="ignore")
        for frame in _input.decode():
            fifo.write(frame)
    gives Invalid data found when processing input; last error log: [mp3float] Header missing
    quantotto
    @quantotto
    File format might be not supported by AV. Is it MP3? Try checking if it is identified correctly by ffmpeg: ffprobe <filename>
    Nitan Alexandru Marcel
    @nitanmarcel
    it is
    Nitan Alexandru Marcel
    @nitanmarcel
    Another error now: Input frame pts 487305 != expected 0; fix or set to None.
    but this in resampler
    quantotto
    @quantotto
    @nitanmarcel sometimes decoders have issues in the middle of the stream. Some are safe to ignore, some not. Try to run some re-encoding through command line with ffmpeg. I bet it will have those errors too. I don't think it has anything to do with PyAV library.
    Nitan Alexandru Marcel
    @nitanmarcel
    @quantotto The commandline worked: ffmpeg -i input.mp3 -f s16le -acodec pcm_s16le output.raw
    Nitan Alexandru Marcel
    @nitanmarcel
    oh I fixed it by setting pts to None
    Nitan Alexandru Marcel
    @nitanmarcel
    Could anyone point me to the part in the ffmpeg code where it handles the command line input? Thanks.
    marcelnitan
    @marcelnitan:envs.net
    [m]
    Nice, I can use matrix instead of the bad gitter app
    Zeyu Dong
    @dong-zeyu

    @dong-zeyu yes, you have to calculate pts / dts yourself. Something like below worked for me:

    import av
    avin = av.open("test.264", "r", "h264")
    avout = av.open("test.mp4", "w", format="mp4")
    avout.add_stream(template=avin.streams[0])
    time_base = int(1 / avin.streams[0].time_base)
    rate = avin.streams[0].base_rate
    ts_inc = int(time_base / rate)
    ts = 0
    for pkt in avin.demux():
        pkt.pts = ts
        pkt.dts = ts
        avout.mux(pkt)
        ts += ts_inc
    avin.close()
    avout.close()

    That works! Thank you!

    1 reply
    NewUserHa
    @NewUserHa
    isn't pts increase as 1? I see testsrc=fps=30 output pts as 1,2,3,4,....
    quantotto
    @quantotto
    @NewUserHa PTS increases in time_base units. For example, if time_base for the video is 1/1000, then 1 unit of pts is actually a millisecond. Time base is something arbitrary that video creator (or software creating the video) chooses and then other timestamps (like pts, dts) align. Going with example of 1/1000 as timebase, if a video has 2 FPS frame rate, each packet / frame will have a step of half second, which is 500 in terms of pts / dts increases.
    NewUserHa
    @NewUserHa
    testsrc=fps=30 in filter graph output pts as 1,2,3,4,.... I guess if it's because of this testsrc..
    tested via pypy. still has that latency issue.
    oczkoisse
    @oczkoisse

    Hi everyone, I could use some help regarding trimming the padding from a video's frames. I've created an issue here with more details: PyAV-Org/PyAV#802. But basically, if I reshape numpy array of a frame's plane along line size and then slice along frame width to get a frame without padding, I'm not getting a properly aligned frame. However, when I try accounting for memory alignment, results are better which leads me to suspect it might be something to do with PyAv ignoring memory alignment issues in its frame width property.

    def _remove_padding(self, plane):
        """Remove padding from a video plane.
    
        Args:
            plane (av.video.plane.VideoPlane): the plane to remove padding from
    
        Returns:
            numpy.array: an array with proper memory aligned width
        """
        buf_width = plane.line_size
        bytes_per_pixel = 1
        frame_width = plane.width * bytes_per_pixel
        arr = np.frombuffer(plane, np.uint8)
        if buf_width != frame_width:
            align_to = 16
            # Frame width that is aligned up with a 16 bit boundary
            frame_width = (frame_width + align_to - 1) & ~(align_to - 1)
            # Slice (create a view) at the aligned boundary
            arr = arr.reshape(-1, buf_width)[:, :frame_width]
        return arr.reshape(-1, frame_width)

    Although the above manual alignment kind of works, it's not correct for every format. In my case, the above works correctly for luma plane but not chroma planes. I'm not sure how to proceed so any advice would be really helpful. Thanks.

    oczkoisse
    @oczkoisse
        # Frame width that is aligned up with a 16 bit boundary
    Correction: it's supposed to be 16 pixel boundary
    Kevin Lin
    @kevinlinxc
    hey guys, when I write a frame using output.mux it squishes it vertically
    output = av.open(output_name, 'w')
    stream = output.add_stream('h264', fps)
    ... #later in a loop
    frame = av.VideoFrame.from_ndarray(frame, format='bgr24')
    print(frame)
    packet = stream.encode(frame)
    output.mux(packet)
    the printout says its the right size <av.VideoFrame #0, pts=None bgr24 1080x2400 at 0x7fc15a027868>
    so I think its something wrong with stream.encode or output.mux lines?
    Kevin Lin
    @kevinlinxc
    ah i needed to set stream.height and stream.width
    Roj
    @roj1512
    Hi, can someone help me redoing a specific Ffmpeg command with PyAV?
    1 reply
    @nitanmarcel said people might be able to help here
    quantotto
    @quantotto
    @rojserbest let's try
    Yunkai
    @lyksdu
    does pyav have new commits after Jan 2021?
    mhtrinh
    @mhtrinh
    I am trying to record my microphone with PyAV. the ffmpeg command is ffmpeg -f pulse -i 4 out.wav . What would be the equivalent in pyav ?
    1 reply
    jackiexiao
    @Jackiexiao

    hi, does anyone know how to change audio stream frame size ?

    import av
    container = av.open('8k.wav')
    for frame in container.decode(audio=0):
        print(frame.samples)

    I get 2048(256ms), but I want to get 160 (20ms) sample, how can I change it?

    dcordb
    @dcordb:matrix.org
    [m]
    Hello everyone
    How can I pass the -protocol_whitelist flag to PyAV
    I want to get the frames of an RTP stream, of which I have the SDP file.
    In ffmpeg to do this I have to pass -whitelist_protocol with rtp, udp,file if not it fails. How to do this in PyAV?
    Marcel Alexandru Nitan
    @marcelnitan:envs.net
    [m]
    How do you send Http Headers with .open?
    Or it isn't possible?
    quantotto
    @quantotto
    @dcordb you can use options argument of av.open
    dcordb
    @dcordb:matrix.org
    [m]
    But do I pass this literally? Like: av.open('foo', options=['-protocol_whitelist rtp,udp,file'])
    In general, can all ffmpeg options be passed to option keyword argument literally?
    quantotto
    @quantotto
    Options expects a dict
    Keys are parameter names (without -) and values are parameters' values. { "protocol_whitelist": "rdp,udp,file" }