jonghwanhyeon / python-ffmpeg

A python binding for FFmpeg which provides sync and async APIs
MIT License
302 stars 53 forks source link

ValueError: invalid literal for int() with base 10: "2097152'" #34

Closed baltic-tea closed 11 months ago

baltic-tea commented 1 year ago

I encountered this error while deploying a Docker application on Debian (python:3.11-slim-bullseye).

Full output of error:

ERROR asyncio.base_events:default_exception_handler:1771 Task exception was never retrieved
future: <Task finished name='Task-7186' coro=<FFmpeg._handle_stderr() done, defined at /root/.local/lib/python3.11/site-packages/ffmpeg/asyncio/ffmpeg.py:190> exception=ValueError('invalid literal for int() with base 10: "2097152\'"')>
Traceback (most recent call last):

> File "/root/.local/lib/python3.11/site-packages/ffmpeg/asyncio/ffmpeg.py", line 194, in _handle_stderr
    self.emit("stderr", line.decode())
    │    │              │    └ <method 'decode' of 'bytes' objects>
    │    │              └ b"  configuration: --pkg-config-flags=--static --extra-cflags=-fopenmp --extra-ldflags='-fopenmp -Wl,-z,stack-size=2097152' -...
    │    └ <function EventEmitter.emit at 0x7fe3f88579c0>
    └ <ffmpeg.asyncio.ffmpeg.FFmpeg object at 0x7fe3f725fa90>
  File "/root/.local/lib/python3.11/site-packages/pyee/base.py", line 208, in emit
    handled = self._call_handlers(event, args, kwargs)
              │    │              │      │     └ {}
              │    │              │      └ ("  configuration: --pkg-config-flags=--static --extra-cflags=-fopenmp --extra-ldflags='-fopenmp -Wl,-z,stack-size=2097152' -...
              │    │              └ 'stderr'
              │    └ <function EventEmitter._call_handlers at 0x7fe3f8857920>
              └ <ffmpeg.asyncio.ffmpeg.FFmpeg object at 0x7fe3f725fa90>
  File "/root/.local/lib/python3.11/site-packages/pyee/base.py", line 184, in _call_handlers
    self._emit_run(f, args, kwargs)
    │    │         │  │     └ {}
    │    │         │  └ ("  configuration: --pkg-config-flags=--static --extra-cflags=-fopenmp --extra-ldflags='-fopenmp -Wl,-z,stack-size=2097152' -...
    │    │         └ <bound method Tracker._on_stderr of <ffmpeg.progress.Tracker object at 0x7fe3f851f6d0>>
    │    └ <function AsyncIOEventEmitter._emit_run at 0x7fe3f8618040>
    └ <ffmpeg.asyncio.ffmpeg.FFmpeg object at 0x7fe3f725fa90>
  File "/root/.local/lib/python3.11/site-packages/pyee/asyncio.py", line 53, in _emit_run
    self.emit("error", exc)
    │    └ <function EventEmitter.emit at 0x7fe3f88579c0>
    └ <ffmpeg.asyncio.ffmpeg.FFmpeg object at 0x7fe3f725fa90>
  File "/root/.local/lib/python3.11/site-packages/pyee/base.py", line 208, in emit
    handled = self._call_handlers(event, args, kwargs)
              │    │              │      │     └ {}
              │    │              │      └ (ValueError('invalid literal for int() with base 10: "2097152\'"'),)
              │    │              └ 'error'
              │    └ <function EventEmitter._call_handlers at 0x7fe3f8857920>
              └ <ffmpeg.asyncio.ffmpeg.FFmpeg object at 0x7fe3f725fa90>
  File "/root/.local/lib/python3.11/site-packages/pyee/base.py", line 184, in _call_handlers
    self._emit_run(f, args, kwargs)
    │    │         │  │     └ {}
    │    │         │  └ (ValueError('invalid literal for int() with base 10: "2097152\'"'),)
    │    │         └ <function EventEmitter.once.<locals>._wrapper.<locals>.g at 0x7fe3f72eff60>
    │    └ <function AsyncIOEventEmitter._emit_run at 0x7fe3f8618040>
    └ <ffmpeg.asyncio.ffmpeg.FFmpeg object at 0x7fe3f725fa90>
  File "/root/.local/lib/python3.11/site-packages/pyee/asyncio.py", line 53, in _emit_run
    self.emit("error", exc)
    │    └ <function EventEmitter.emit at 0x7fe3f88579c0>
    └ <ffmpeg.asyncio.ffmpeg.FFmpeg object at 0x7fe3f725fa90>
  File "/root/.local/lib/python3.11/site-packages/pyee/base.py", line 211, in emit
    self._emit_handle_potential_error(event, args[0] if args else None)
    │    │                            │      │          └ (ValueError('invalid literal for int() with base 10: "2097152\'"'),)
    │    │                            │      └ (ValueError('invalid literal for int() with base 10: "2097152\'"'),)
    │    │                            └ 'error'
    │    └ <function EventEmitter._emit_handle_potential_error at 0x7fe3f8857880>
    └ <ffmpeg.asyncio.ffmpeg.FFmpeg object at 0x7fe3f725fa90>
  File "/root/.local/lib/python3.11/site-packages/pyee/base.py", line 169, in _emit_handle_potential_error
    raise error
          └ ValueError('invalid literal for int() with base 10: "2097152\'"')
  File "/root/.local/lib/python3.11/site-packages/pyee/asyncio.py", line 51, in _emit_run
    coro: Any = f(*args, **kwargs)
                │  │       └ {}
                │  └ (ValueError('invalid literal for int() with base 10: "2097152\'"'),)
                └ <function EventEmitter.once.<locals>._wrapper.<locals>.g at 0x7fe3f72eff60>
  File "/root/.local/lib/python3.11/site-packages/pyee/base.py", line 238, in g
    return f(*args, **kwargs)
           │  │       └ {}
           │  └ (ValueError('invalid literal for int() with base 10: "2097152\'"'),)
           └ <bound method FFmpeg._reraise_exception of <ffmpeg.asyncio.ffmpeg.FFmpeg object at 0x7fe3f725fa90>>
  File "/root/.local/lib/python3.11/site-packages/ffmpeg/asyncio/ffmpeg.py", line 197, in _reraise_exception
    raise exception
          └ ValueError('invalid literal for int() with base 10: "2097152\'"')
  File "/root/.local/lib/python3.11/site-packages/pyee/asyncio.py", line 51, in _emit_run
    coro: Any = f(*args, **kwargs)
                │  │       └ {}
                │  └ ("  configuration: --pkg-config-flags=--static --extra-cflags=-fopenmp --extra-ldflags='-fopenmp -Wl,-z,stack-size=2097152' -...
                └ <bound method Tracker._on_stderr of <ffmpeg.progress.Tracker object at 0x7fe3f851f6d0>>
  File "/root/.local/lib/python3.11/site-packages/ffmpeg/progress.py", line 37, in _on_stderr
    statistics = Statistics.from_line(line)
                 │          │         └ "  configuration: --pkg-config-flags=--static --extra-cflags=-fopenmp --extra-ldflags='-fopenmp -Wl,-z,stack-size=2097152' --...
                 │          └ <classmethod(<function Statistics.from_line at 0x7fe3f864a660>)>
                 └ <class 'ffmpeg.statistics.Statistics'>
  File "/root/.local/lib/python3.11/site-packages/ffmpeg/statistics.py", line 41, in from_line
    fields = {key: _field_factory[key](value) for key, value in statistics.items()}
                   │                                            │          └ <method 'items' of 'dict' objects>
                   │                                            └ {'size': "2097152'"}
                   └ {'frame': <class 'int'>, 'fps': <class 'float'>, 'size': <function <lambda> at 0x7fe3f8649940>, 'time': <function parse_time ...
  File "/root/.local/lib/python3.11/site-packages/ffmpeg/statistics.py", line 41, in <dictcomp>
    fields = {key: _field_factory[key](value) for key, value in statistics.items()}
              │    │              │    │          │    └ "2097152'"
              │    │              │    │          └ 'size'
              │    │              │    └ "2097152'"
              │    │              └ 'size'
              │    └ {'frame': <class 'int'>, 'fps': <class 'float'>, 'size': <function <lambda> at 0x7fe3f8649940>, 'time': <function parse_time ...
              └ 'size'
  File "/root/.local/lib/python3.11/site-packages/ffmpeg/statistics.py", line 19, in <lambda>
    "size": lambda item: int(item.replace("kB", "")) * 1024,
                   │         │    └ <method 'replace' of 'str' objects>
                   │         └ "2097152'"
                   └ "2097152'"

ValueError: invalid literal for int() with base 10: "2097152'"

I fixed this error by changing the one line in the ./ffmpeg/statistics.py.

_field_factory = {
    "frame": int,
    "fps": float,
    "size": lambda item: int(item.rstrip(r"'kB ")) * 1024,
    "time": parse_time,
    "bitrate": lambda item: float(item.replace("kbits/s", "")),
    "speed": lambda item: float(item.replace("x", "")),
}

Before: "size": lambda item: int(item.replace("kB")) * 1024 After: "size": lambda item: int(item.rstrip("'kB ")) * 1024

jonghwanhyeon commented 1 year ago

Hi! I solved this issue on v2.0.6. Thanks for reporting the bug.

aa-reznik commented 11 months ago

@jonghwanhyeon sorry for late comment, but face same error on ffprobe 6.0 and 6.1. On 4.4.1 and 4.4.2 all is ok. Binary ffmpeg and ffprobe from here: mwader/static-ffmpeg:6.0 and mwader/static-ffmpeg:6.1

jonghwanhyeon commented 11 months ago

@aa-reznik Can you show me a code you use?

aa-reznik commented 11 months ago

@jonghwanhyeon yes, it's very simple function.

import json
import os

from ffmpeg.asyncio import FFmpeg

async def get_local_media_metadata(local_file_path: str) -> dict:
    """
    Retrieves metadata for a local media file.

    Args:
        local_file_path: The file path of the local media file.

    Returns:
        dict with results.
    """
    ffprobe = FFmpeg(executable="ffprobe").input(
        local_file_path,
        print_format="json",
        show_streams=None,
        show_format=None,
    )

    media = json.loads(await ffprobe.execute())
    metadata = {
        "duration": round(float(media["format"]["duration"]), 2),
        "size": int(media["format"]["size"]),
        "size_mb": round(float(media["format"]["size"]) / 1024 / 1024, 2),
        "bitrate_kbps": round(float(media["format"]["bit_rate"]) / 1024, 2),
    }

    try:
        video_streams = [
            stream for stream in media["streams"] if stream["codec_type"] == "video"
        ]
        width, height = video_streams[0]["width"], video_streams[0]["height"]
        metadata.update({"width": width, "height": height})
    except Exception:
        pass  # No video stream, it's ok

    return metadata

used python-ffmpeg==2.0.7 and

COPY --from=mwader/static-ffmpeg:6.0 /ffmpeg /usr/local/bin/
COPY --from=mwader/static-ffmpeg:6.0 /ffprobe /usr/local/bin/

in docker file.

Same exception as above, no matter what mp4 file is on input:

File "/usr/local/lib/python3.10/site-packages/ffmpeg/statistics.py", line 19, in <lambda>
    "size": lambda item: int(item.replace("kB", "")) * 1024,
    ValueError: invalid literal for int() with base 10: "2097152'"
jonghwanhyeon commented 11 months ago

Fixed! Please use version 2.0.8. Thanks for helping me 🙏

udithj commented 9 months ago

Hi, I got a very similar issue on Windows 11 with ffmpeg.

python-ffmpeg 2.0.10

Traceback (most recent call last):
  File "D:\proj\python\using-python-ffmpeg\convert.py", line 91, in <module>
    convert_file(input=sys.argv[1], output=sys.argv[2])
  File "D:\proj\python\using-python-ffmpeg\convert.py", line 85, in convert_file
    run_ffmpeg(str(input_path), str(output_path), preset, crf)
  File "D:\proj\python\using-python-ffmpeg\utils.py", line 79, in run_ffmpeg
    ffmpeg.execute()
  File "D:\proj\python\using-python-ffmpeg\.venv\Lib\site-packages\ffmpeg\ffmpeg.py", line 197, in execute
    raise exception
  File "C:\Users\uj95\AppData\Local\Programs\Python\Python312\Lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\proj\python\using-python-ffmpeg\.venv\Lib\site-packages\ffmpeg\ffmpeg.py", line 256, in _handle_stderr
    self.emit("stderr", line.decode())
  File "D:\proj\python\using-python-ffmpeg\.venv\Lib\site-packages\pyee\base.py", line 208, in emit
    handled = self._call_handlers(event, args, kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\proj\python\using-python-ffmpeg\.venv\Lib\site-packages\pyee\base.py", line 184, in _call_handlers
    self._emit_run(f, args, kwargs)
  File "D:\proj\python\using-python-ffmpeg\.venv\Lib\site-packages\pyee\base.py", line 160, in _emit_run
    f(*args, **kwargs)
  File "D:\proj\python\using-python-ffmpeg\.venv\Lib\site-packages\ffmpeg\progress.py", line 37, in _on_stderr
    statistics = Statistics.from_line(line)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\proj\python\using-python-ffmpeg\.venv\Lib\site-packages\ffmpeg\statistics.py", line 41, in from_line
    fields = {key: _field_factory[key](value) for key, value in statistics.items() if value != "N/A"}
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\proj\python\using-python-ffmpeg\.venv\Lib\site-packages\ffmpeg\statistics.py", line 19, in <lambda>
    "size": lambda item: int(item.replace("kB", "")) * 1024,
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '0KiB'