PyAV icon indicating copy to clipboard operation
PyAV copied to clipboard

OverflowError when adding stream with float framerate

Open caspervdw opened this issue 8 years ago • 8 comments

When adding a stream with a floating point framerate, as follows:

    import av

output = av.open('test.avi', 'w')
output.add_stream('rawvideo', rate=30.000001)

I receive the following error message: --------------------------------------------------------------------------- OverflowError Traceback (most recent call last) in () 4 5 output = av.open('test.avi', 'w', format=None, options=dict()) ----> 6 output.add_stream('rawvideo', rate=30.)

av/container/output.pyx in av.container.output.OutputContainer.add_stream (src\av\container\output.c:2656)()

av/container/output.pyx in av.container.output.OutputContainer.add_stream (src\av\container\output.c:2322)()

OverflowError: Python int too large to convert to C long

My av version is 0.3.3, on Win64, Python 3.5.2. My guess is that it gets converted to a fraction, which contains an integer that is too large for a Clong. Is there a possibility to get around this?

caspervdw avatar Jun 14 '17 11:06 caspervdw

30.000001 is interpreted as 30.000001000000001 in Fraction. This error does not occur when the argument is passed as a string.

>>> from fractions import Fraction
>>> Fraction(30.000001)
Fraction(8444249582794657, 281474976710656)
>>> Fraction('30.000001')
Fraction(30000001, 1000000)
output.add_stream('rawvideo', rate='30.000001')

vbkaisetsu avatar Jun 14 '17 14:06 vbkaisetsu

@vbkaisetsu thanks, the following solved this for me. I limit the rate to 4 decimals, as I noted that MPEG-4 only allows a timebase of max 66535.

import av

output = av.open('test.avi', 'w')
output.add_stream('rawvideo', rate='{0:.4f}'.format(30.000001))

caspervdw avatar Jun 15 '17 14:06 caspervdw

In that case, I think you can also use limit_denominator:

stream = output.add_stream(codec, rate=Fraction(export_rate).limit_denominator(65535))

vbkaisetsu avatar Jun 16 '17 02:06 vbkaisetsu

Ok thanks, that works too indeed

caspervdw avatar Jun 20 '17 18:06 caspervdw

Does anyone know if there is a way to detect what the maximum denominator is for a codec except for hard-coding one?

We could bypass the overflow error by limiting to 2**32-1, or (I'm leaning towards) throwing an error of our own that is more descriptive.

mikeboers avatar Jun 21 '17 14:06 mikeboers

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Mar 31 '22 02:03 github-actions[bot]

Let's keep this alive.

jlaine avatar Mar 31 '22 05:03 jlaine

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Jul 30 '22 03:07 github-actions[bot]