Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Mike Boers
    Experiment with giving the time relative to different streams as well

    I am trying to use Apache Spark (pyspark) with Pyav. To distribute objects to workers, pyspark pickle objects instances. And guess what? Pickle on a Packet instance fail with this error message :

      File "stringsource", line 2, in av.packet.Packet.__reduce_cython__
    TypeError: no default __reduce__ due to non-trivial __cinit__

    Is there a solution for pickle to be able to serialize an instance of a class not declared in python ? Can I contribute to create class stubs for pickle to succeed ?

    Till Schulte
    Is there any guide / tutorial / information out there about how timecodes should be set in PyAv / libav? It either does this: WARNING:libav.opus:Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly or this: WARNING:libav.opus:Encoder did not produce proper pts, making some up. or if I set the timecodes manually it gets a segmentation fault on seek...
    Sam Mesterton-Gibbons

    Hi folks, I'm running into a bit of a problem installing on High Sierra: import av returns:

    ImportError: dlopen(/Users/samn/local-projects/tenpercent/rd-hot-desk-radio-studio/rd-recording/split_shows/venv/lib/python3.6/site-packages/av/_core.cpython-36m-darwin.so, 2): Symbol not found: ____chkstk_darwin
      Referenced from: /Users/samn/local-projects/tenpercent/rd-hot-desk-radio-studio/rd-recording/split_shows/venv/lib/python3.6/site-packages/av/.dylibs/libavutil.56.31.100.dylib (which was built for Mac OS X 10.15)
      Expected in: /usr/lib/libSystem.B.dylib
     in /Users/samn/local-projects/tenpercent/rd-hot-desk-radio-studio/rd-recording/split_shows/venv/lib/python3.6/site-packages/av/.dylibs/libavutil.56.31.100.dylib

    I think that's down to an ABI change in newer MacOS versions (and is the same problem as pyca/cryptography#5215) - I'm halfway through writing an Issue but the template says I should ask on here first.
    Anyone come across this already?

    Joanna Bitton

    Hi there! I'm running into a bit of a problem trying to write to a video file both an audio and video stream (from pytorch tensors) ---

    This is my code so far:

    def write_video(
        video_array = torch.as_tensor(video_array, dtype=torch.uint8).numpy()
        container = av.open(filename, mode="w")
        stream = container.add_stream(video_codec, rate=video_fps)
        stream.width = video_array.shape[2]
        stream.height = video_array.shape[1]
        stream.pix_fmt = "yuv420p" if video_codec != "libx264rgb" else "rgb24"
        stream.options = options or {}
        for img in video_array:
            frame = av.VideoFrame.from_ndarray(img, format="rgb24")
            frame.pict_type = "NONE"
            for packet in stream.encode(frame):
        # Flush stream
        for packet in stream.encode():
        if audio_array is not None:
            audio_array = torch.as_tensor(audio_array).numpy()
            a_stream = container.add_stream(audio_codec, rate=audio_fps)
            a_stream.options = {}
            frame = av.AudioFrame.from_ndarray(
                audio_array, format=sample_format, layout=audio_layout
            for packet in a_stream.encode(frame):
            for packet in a_stream.encode():
        # Close the file

    this is how im calling the function:

    video_tensor, audio_tensor, info = torchvision.io.video.read_video("2.mp4", pts_unit="sec")

    However, I keep getting the following error:

    Requested input sample rate 0 is invalid
    Traceback (most recent call last):
      File "write.py", line 80, in <module>
      File "write.py", line 59, in write_video
        for packet in a_stream.encode(frame):
      File "av/stream.pyx", line 152, in av.stream.Stream.encode
      File "av/codec/context.pyx", line 466, in av.codec.context.CodecContext.encode
      File "av/audio/codeccontext.pyx", line 40, in av.audio.codeccontext.AudioCodecContext._prepare_frames_for_encode
      File "av/audio/resampler.pyx", line 106, in av.audio.resampler.AudioResampler.resample
      File "av/audio/resampler.pyx", line 103, in av.audio.resampler.AudioResampler.resample
      File "av/error.pyx", line 336, in av.error.err_check
    av.error.ValueError: [Errno 22] Invalid argument; last error log: [SWR] Requested input sample rate 0 is invalid
    Traceback (most recent call last):
      File "av/container/output.pyx", line 25, in av.container.output.close_output
    TypeError: 'NoneType' object is not iterable
    Exception ignored in: 'av.container.output.OutputContainer.__dealloc__'
    Traceback (most recent call last):
      File "av/container/output.pyx", line 25, in av.container.output.close_output
    TypeError: 'NoneType' object is not iterable

    I'm not sure why it says that the sample rate is set to zero when I set it as I add streams, so I'm not sure how to move forward. The video is still being produced, but it just has no audio. Would really appreciate any help!

    Sam Mesterton-Gibbons
    Asking the obvious; I assume you've checked info['audio_fps'] is a sensible audio sample rate in your call to write_video?
    Joanna Bitton
    @samdbmg yes, it's 44100
    Joanna Bitton

    additionally, when i print out print(container.streams.video[0].rate, container.streams.audio[0].rate), this is the output: 29 44100

    so im not sure why it thinks the sample rate is 0

    Sam Mesterton-Gibbons
    What happens if you create the audio stream before the first call to container.mux(...)? I think start_encoding() is only run once on the first mux call to write file headers, so the audio stream won't get written into the file headers after that (which tallies up with the libavformat docs). However I've not actually tested that I'm afraid!
    Joanna Bitton
    @samdbmg Just tried creating the audio stream before the first call to container.mux-- that actually seemed to get rid of the TypeErrors that you can see in the error above. However, the Invalid argument error is still appearing
    Michael Noronha
    Hey, I'm a bit confused about why encoding a "bgr24" frame to "yuv420p" and back doesn't preserve the data. Is this expected behavior? Minimal example: https://gist.github.com/mtn/086dfd0ed7d673690ce5c12983c43634
    Sam Mesterton-Gibbons
    @jbitton Looking at what the resampler actually does, it seems to copy your first AudioFrame and read the input sample rate from it. Since AudioFrame.from_ndarray doesn't set the sample rate, try setting it directly after you create the frame, which is what the tests seem to do
    Hi there, I have got a question about the H264 codec. I would like to extract packets which are I-frame and convert them to frame and chang them and again convert to a packet and write them with packets which aren't I-frame in the container. On the other hand, I have got some packets that I encoded frames and packets which have been obtained by container.demux() and I would like to write them together in a container. I have got a python code that does this task but finally, I have only I-frames in the video. This problem is in my thesis. anybody can help me? Thanks
    Joanna Bitton
    @samdbmg you are so right, thank you so much!! it worked :)
    Hello, I am trying to run PyAV + x11grab to record the screen on ubuntu. I have installed PyAV using conda-forge. Here is the code.
    container = av.open(':0.0', format='x11grab', options={'s':'800x600', 'i':'0:0'})
    and the error
    Traceback (most recent call last):
    File "t07record.py", line 3, in <module>
    container = av.open(':0.0', format='x11grab', options={'s':'800x600', 'i':'0:0'})
    File "av/container/core.pyx", line 354, in av.container.core.open
    File "av/container/core.pyx", line 131, in av.container.core.Container.cinit
    File "av/format.pyx", line 90, in av.format.ContainerFormat.cinit
    ValueError: no container format 'x11grab'
    Do I have to recompile from source and add x11grab as a format or I am missing something?
    Thank you very much for any ideas!
    I think the ffmpeg for conda-forge does not have the x11grab ( whereis ffmpeg and which ffmpeg )
    LI Zhennan
    Hi, I am trying to play opus encoded audio stream from TCP, I wonder if there's a way to realize it sololy by PyAV? I have read this issue. I knew PyAV has got libavdevice support, but failed to find an example.
    LI Zhennan
    Also, is there a way to make playing cross platform? Maybe using alsa on Linux, avfondation on Mac? What should I use on Windows? Or is there a better way?
    Thanks in advance, any help would be appreciate.
    Hermes Zhang
    Hi, I'm a new to PyAV, I try to plot some basic info of video frame, e.g. frame size, but I don't find such property from VideoFrame, I only find the packet.size, but the value seems not what I want, e.g. the I frame and P frame are similar, but actually the I frame should much bigger than P frame, does anyone know how to get the frame size by PyAV? Thanks.
    Any ideas on how I could sync two HLS playlists? I'm generating my own HLS playlist from an MPEGTS feed (doing object detection on it and reconstructing). The provider of the MPEGTS feed has an HLS version as well. I want to make sure that my feed would match the providers. Thoughts?
    can you get the segment name from PyAV while parsing an HLS feed?
    Joe Martinez

    I am having trouble installing PyAV.

    $ pip install av
    Collecting av
    Using cached https://files.pythonhosted.org/packages/4d/fe/170a64b51c8f10df19fd2096388e51f377dfec07f0bd2f8d25ebdce6aa50/av-8.0.1.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-build-4yV5M1/av/setup.py", line 9, in <module>
    from shlex import quote
    ImportError: cannot import name quote


    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-4yV5M1/av/

    Any ideas?
    Razvan Grigore
    Hey, is there a way to use a copy "dummy" codecContext? I'm opening a container with av.open(file="/dev/video0", format="v4l2", mode="r", options={"video_size": "1920x1080", "framerate": "30", "input_format": "h264"}) since RPi camera and Logitech C920 can give HW accelerated H264 stream, but then I have trouble working with the Packet object to bitstream over webRTC. Any clues?
    I was thinking about a project to manipulate the chapter/menu data in an audiobook with m4b format. I'm trying to work out if pyav is going to be able help me with that. As a test, I have a file with 56 chapters in the metadata (that shows up via ffmpeg -i book.m4b -f ffmetadata book.metadata.txt). With pyav, I can see a data stream that has 56 'frames', but I'm uncertain how (or if) I can process that chapter info. Does anyone know if this is possible as things stand now?
    Ramon Caldeira
    Hello! I need to read NTP information from the RTCP packets in my RTSP stream. I managed to do it with libav in C, using private structures as RTSPState, RTSPStream and RTPDemuxContext, instatiated from the priv_data field of AvFormatContext.
    Now I would like to integrate it into my Python code, using PyAV. Are patching FFMPEG and maintaining a private PyAV fork the best way forward?
    Ramon Caldeira
    Alex Hutnik
    Any ideas on reading a h264 mp4 file while it's being written? i'm recording from a RTSP stream to a file and want to read it "up to the most recent write" so to speak. Preferably through httpd which doesn't work and I suspect I have to process the packets through pyav to apply some metadata. Hence why I'm here :)
    Ramon Caldeira
    @alexhutnik you can read the RTSP from pyav and from there write into the file and process the packets
    I'm trying to generate my own PyAV wheel but it looks like it's not including FFMPEG... I'm trying with $PYAV_PYTHON setup.py bdist_wheel...
    Could anybody help me fix it?
    Victor Sklyar

    @alexhutnik you can read the RTSP from pyav and from there write into the file and process the packets

    seems i have the same trouble, I need support https protocol but have to rebuild ffmpeg

    Victor Sklyar
    Now I trying build 8.0.1 such as located in pip repository, but cannot put ffmpeg .so to whl.
    Can someone point me to sample or something to help solve this trouble?
    Alex Hutnik
    @ramoncaldeira I've tried doing that:
    if __name__ == "__main__":
        fh = open('video_bytes.bin', 'rb')
        codec = av.CodecContext.create('h264', 'r')
        while True:
            chunk = fh.read(1 << 16)
            packets = codec.parse(str(chunk))
            print("Parsed {} packets from {} bytes:".format(len(packets), len(chunk)))
            for packet in packets:
                print('   ', packet)
                frames = codec.decode(packet)
                for frame in frames:
                    print('       ', frame)
    Can't parse any packets
    tried to just parse the chunk directly, but parse complained that chunk was not a str
    i tried writing the packets both as raw bytes (using packet.to_bytes()) and to a stream container
    Also note that I am parsing a closed file. So this isn't even being attempted on a file that is being written to
    Alex Hutnik
    maybe there is an issue with parse
    I am trying to apply FFmpeg filters to a .wav tempfile. I am getting the error: av.error.ValueError: [Errno 22] Invalid argument on the line for p in out_stream.encode(ofr): of the offending code.
    The offending code: