Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error converting to AAC with time_base of Fraction(1, 1000) #1182

Closed
4 of 6 tasks
daveisfera opened this issue Oct 20, 2023 · 1 comment · Fixed by #1209
Closed
4 of 6 tasks

Error converting to AAC with time_base of Fraction(1, 1000) #1182

daveisfera opened this issue Oct 20, 2023 · 1 comment · Fixed by #1209
Labels

Comments

@daveisfera
Copy link
Contributor

daveisfera commented Oct 20, 2023

Overview

Converting to AAC with a time_base of Fraction(1, 1000) doesn't work

Expected behavior

Conversion would work regardless of time_base

Actual behavior

Reported length of the file is incorrect and the file doesn't play back correctly

Investigation

If you scale the time_base and the pts/dts values by 8, then the conversion works

(NOTE: requires the second commit from #1180 to be able to do this)

Reproduction

Run the following with and observe the error:

python3 convert_to_aac_error.py audio_test_long.mkv output_bad.m4a

Then run with the adjustment and it will work:

python3 convert_to_aac_error.py audio_test_long.mkv output_good.m4a y

With this file:
https://drive.google.com/file/d/1mlL51WHC4iiTa4ibiMsTNYNmjzV4005-/view?usp=sharing

import sys
from fractions import Fraction

import av

def _main():
    adjust = len(sys.argv) >= 4 and sys.argv[3] == "y"
    with av.open(sys.argv[1]) as in_container:
        in_stream = in_container.streams.audio[0]
        with av.open(sys.argv[2], "w") as out_container:
            print(in_stream.time_base, in_stream.codec_context.layout)
            out_stream = out_container.add_stream(
                "aac", rate=in_stream.rate, layout=in_stream.codec_context.layout.name
            )
            num_frames = 0
            num_packets = 0
            for frame in in_container.decode(in_stream):
                num_frames += 1
                if adjust:
                    frame.pts *= 8
                    frame.dts *= 8
                    frame.time_base = Fraction(1, 8000)
                if num_frames == 1:
                    print(
                        type(frame),
                        frame.sample_rate,
                        frame.pts,
                        frame.dts,
                        frame.time_base,
                        len(frame.planes),
                        frame.planes[0].buffer_size,
                    )
                for packet in out_stream.encode(frame):
                    num_packets += 1
                    out_container.mux(packet)

            print(num_frames, num_packets)

            for packet in out_stream.encode(None):
                num_packets += 1
                out_container.mux(packet)

            print(num_packets)


if __name__ == "__main__":
    _main()

Versions

  • OS: Debian Bookworm
  • PyAV runtime: 10.0.0
PyAV v10.0.0
library configuration: --prefix=/usr --extra-version=1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libglslang --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librist --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --disable-sndio --enable-libjxl --enable-pocketsphinx --enable-librsvg --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-librav1e --enable-shared
library license: GPL version 2 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:
    Installed from the branch in the mentioned PR
  • FFmpeg:
    N/A using the OS libraries without ffmpeg itself being installed

Research

I have done the following:

Additional context

ffmpeg has known issues with AAC output but this appears to be specific to PyAV because doing the conversion with ffmpeg directly works

@daveisfera
Copy link
Contributor Author

Moved to maintained fork:
WyattBlue#11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant