Skip to content

[av v10.0] The output container does not put audio stream under audio but under other instead #1044

Closed

Description

@YosuaMichael

Overview

In av==10.0, when we do add stream for audio steam in output container: container.add_stream('aac', rate=44100), we notice that container.streams.audio is still empty tuple, and instead the audio steam actually goes to container.streams.other instead (see reproduction code below).

Note that this bug is not happening in av==9.2.

Expected behavior

When we do container.add_stream('aac', rate=44100) we expect container.streams.audio to be not empty

Actual behavior

When we do container.add_stream('aac', rate=44100) we expect container.streams.audio is empty, and container.streams.other is not empty instead.

Reproduction

# Simplified from torchvision code: https://github.com/pytorch/vision/blob/main/torchvision/io/video.py#L99
import av
import torch
import numpy as np

audio_fps = 44100
audio_codec = 'aac'
audio_layout = "stereo"
audio_array = torch.rand((2, 44100))

container = av.open("test_write.mp4", mode="w")
a_stream = container.add_stream(audio_codec, rate=audio_fps)
audio_format_dtypes = {
    "dbl": "<f8",
    "dblp": "<f8",
    "flt": "<f4",
    "fltp": "<f4",
    "s16": "<i2",
    "s16p": "<i2",
    "s32": "<i4",
    "s32p": "<i4",
    "u8": "u1",
    "u8p": "u1",
}
audio_sample_fmt = container.streams[0].format.name
format_dtype = np.dtype(audio_format_dtypes[audio_sample_fmt])
audio_array = torch.as_tensor(audio_array).numpy().astype(format_dtype)
frame = av.AudioFrame.from_ndarray(audio_array, format=audio_sample_fmt, layout=audio_layout)

frame.sample_rate = audio_fps

for packet in a_stream.encode(frame):
    container.mux(packet)

for packet in a_stream.encode():
    container.mux(packet)
    
container.close()

print(f"container.streams.audio: {container.streams.audio}")
print(f"container.streams.other: {container.streams.other}")

# Using av==10.0
# container.streams.audio: ()
# container.streams.other: (<av.Stream #0 audio/aac at 0x12551fe20>,)


# Using av==9.2
# container.streams.audio: (<av.AudioStream #0 aac at 44100Hz, stereo, fltp at 0x1275920e0>,)
# container.streams.other: ()

Versions

  • OS: macOS 12.6
  • PyAV runtime:
PyAV v10.0.0
library configuration: --disable-static --enable-shared --libdir=/tmp/vendor/lib --prefix=/tmp/vendor --arch=arm64 --enable-cross-compile --disable-alsa --disable-doc --disable-mediafoundation --enable-fontconfig --enable-gmp --disable-gnutls --enable-gpl --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --disable-libxcb --enable-libxml2 --enable-libxvid --enable-lzma --enable-version3 --enable-zlib
library license: GPL version 3 or later
libavcodec     59. 37.100
libavdevice    59.  7.100
libavfilter     8. 44.100
libavformat    59. 27.100
libavutil      57. 28.100
libswresample   4.  7.100
libswscale      6.  7.100
  • PyAV build:
Install from `pip install av==10.0`
  • FFmpeg:
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
built with clang version 12.0.0
configuration: --prefix=/Users/yosuamichael/opt/miniconda3/envs/tv --cc=arm64-apple-darwin20.0.0-clang --disable-doc --enable-avresample --enable-gmp --enable-hardcoded-tables --enable-libfreetype --enable-libvpx --enable-pthreads --enable-libopus --enable-postproc --enable-pic --enable-pthreads --enable-shared --enable-static --enable-version3 --enable-zlib --enable-libmp3lame --disable-nonfree --enable-gpl --enable-gnutls --disable-openssl --enable-libopenh264 --enable-libx264
libavutil      56. 31.100 / 56. 31.100
libavcodec     58. 54.100 / 58. 54.100
libavformat    58. 29.100 / 58. 29.100
libavdevice    58.  8.100 / 58.  8.100
libavfilter     7. 57.100 /  7. 57.100
libavresample   4.  0.  0 /  4.  0.  0
libswscale      5.  5.100 /  5.  5.100
libswresample   3.  5.100 /  3.  5.100
libpostproc    55.  5.100 / 55.  5.100

Research

I have done the following:

Additional context

This behaviour breaks the torchvision when it try to write video with pyav==10.0, here is a related issue: pytorch/vision#6814.

Activity

  1. HanzCEO commented on Dec 25, 2022

    @HanzCEO
    Contributor

    For reference, these are the lines that consider the stream placement:

    cdef add_stream(self, Stream stream):
    assert stream.ptr.index == len(self._streams)
    self._streams.append(stream)
    if stream.ptr.codecpar.codec_type == lib.AVMEDIA_TYPE_VIDEO:
    self.video = self.video + (stream, )
    elif stream.ptr.codecpar.codec_type == lib.AVMEDIA_TYPE_AUDIO:
    self.audio = self.audio + (stream, )
    elif stream.ptr.codecpar.codec_type == lib.AVMEDIA_TYPE_SUBTITLE:
    self.subtitles = self.subtitles + (stream, )
    elif stream.ptr.codecpar.codec_type == lib.AVMEDIA_TYPE_DATA:
    self.data = self.data + (stream, )
    else:
    self.other = self.other + (stream, )

  2. HanzCEO commented on Dec 25, 2022

    @HanzCEO
    Contributor

    This is where stream are created in InputContainer:

    lib.avformat_new_stream(self.ptr, codec)

  3. HanzCEO commented on Dec 25, 2022

    @HanzCEO
    Contributor

    There's just 16 commits between 9.2.0 and 10.0.0:
    4cf0953...bc4eedd

  4. HanzCEO commented on Dec 25, 2022

    @HanzCEO
    Contributor

    git bisect result:

    18704658487ea25e5202ac18438d836dfe65b9d0 is the first bad commit
    commit 18704658487ea25e5202ac18438d836dfe65b9d0
    Author: Jeremy Lainé <jeremy.laine@m4x.org>
    Date:   Fri Mar 11 16:30:43 2022 +0100
    
        [streams] stop using deprecated Stream.codec, it's gone in FFmpeg 5
        
        We now allocate and populate an AVCodecContext ourselves.
        avcodec_copy_context is also gone, so stop using it.
        
        We relax the Stream.average_rate tests for older FFmpeg, as the videos
        output by these older FFmpeg's seem to give a slightly wrong FPS since
        the switch to our own AVCodecContext.
    
     av/codec/context.pxd             |  5 +--
     av/codec/context.pyx             |  7 ++--
     av/container/input.pyx           | 38 ++++++++++++------
     av/container/output.pyx          | 28 +++++--------
     av/container/streams.pyx         | 10 ++---
     av/data/stream.pyx               |  2 +-
     av/packet.pyx                    |  2 +-
     av/stream.pxd                    | 10 ++---
     av/stream.pyx                    | 85 +++++++++++++++++-----------------------
     av/video/stream.pyx              |  4 +-
     include/libavcodec/avcodec.pxd   | 14 +++++--
     include/libavformat/avformat.pxd |  1 -
     tests/common.py                  |  1 +
     tests/test_codec_context.py      |  4 +-
     tests/test_encode.py             | 50 ++++++++++++++++++-----
     15 files changed, 143 insertions(+), 118 deletions(-)
  5. HanzCEO commented on Dec 25, 2022

    @HanzCEO
    Contributor

    Using your script, this line returns with -1 (a.k.a. unknown AVMedia type)

    print(stream.ptr.codecpar.codec_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions