ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

How do I read back errors?

Open roman-ku opened this issue 7 years ago • 8 comments

When I get this back from ffmpeg ffprobe error (see stderr output for detail) I do not know how to check the stderr output for the details.

import sys

import ffmpeg
from ffmpeg import Error as FFmpegError

try:
    duration = ffmpeg.probe('https://www.youtube.com/watch?v=cv4123_39ddfa324LB23MsYg')['format']['duration']
    
except FFmpegError as e:
    print(e)

    # doesn't work
    # for line in sys.stderr:
        # print(line)

    # doesn't work
    # data = sys.stderr.readlines()
    # print(data)

Couldn't find an example of this in the documentation and couldn't figure it out myself.

roman-ku avatar Jan 18 '19 05:01 roman-ku

With some finagling I came up with this:

  1. Pass args capture_stdout=True, capture_stderr=True to run()
  2. Theoretically the run() func returns tuple (out, err) so you can receive those; however...
  3. If the return code is nonzero the function throws so you should probably catch that and print:
            try:
                ffmpeg.input(...) \
                    .output(...) \
                    .run(capture_stdout=True, capture_stderr=True)
            except ffmpeg.Error as e:
                print('stdout:', e.stdout.decode('utf8'))
                print('stderr:', e.stderr.decode('utf8'))
                raise e

evictor avatar May 17 '19 20:05 evictor

tanx was usefull for me gave this.. what this error means ...

stdout: stderr: ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers

built with gcc 9.1.1 (GCC) 20190807

configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt

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

libswscale 5. 5.100 / 5. 5.100

libswresample 3. 5.100 / 3. 5.100

libpostproc 55. 5.100 / 55. 5.100

[image2 @ 05fe3ec0] Pattern type 'glob' was selected but globbing is not supported by this libavformat build

1.jpg: Function not implemented

AfcEagle avatar Jan 27 '20 03:01 AfcEagle

Hi, I also run into this problem, have you fixed it?

wdrink avatar Dec 01 '20 02:12 wdrink

Just ran into this as well (could not figure out how to read stderr coming from probe). Finally (after a lot of unsuccessful googling) I found the answer in a nicely placed comment in the the source code:

From ffmpeg-python/ffmpeg/_probe.py:

    Raises:
        :class:`ffmpeg.Error`: if ffprobe returns a non-zero exit code,
            an :class:`Error` is returned with a generic error message.
            The stderr output can be retrieved by accessing the
            ``stderr`` property of the exception.

So this is what worked for me:

try:
    probe = ffmpeg.probe(video_path)
    video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
    width = int(video_stream['width'])
    height = int(video_stream['height'])
    duration = float(video_stream['duration'])
except ffmpeg.Error as e:
    print(e.stderr)

CodeStrumpet avatar Jan 08 '21 17:01 CodeStrumpet

            except ffmpeg.Error as e:
                print('stdout:', e.stdout.decode('utf8'))
                print('stderr:', e.stderr.decode('utf8'))
                raise e

AttributeError: 'NoneType' object has no attribute 'decode'

Keramatfar avatar Feb 01 '21 11:02 Keramatfar

@Keramatfar You might be using .run(). If so, use .run(capture_stdout=True, capture_stderr=True) instead.

Ann-Zen avatar Feb 09 '21 01:02 Ann-Zen

This wrapper is so fantastic and yet the error handling and logging is so poorly implemented, it seems. I wish some attention was payed to this. for example, passing the loglevel of the function to ffprobe funciton, so you can suppress the headers in the error, and fetch the actual error. Also , does this lib allow tracking of progress? I know ffmpeg spits this info out. This is a way to use this to generate a progress bar.

KaranTrivedi avatar Mar 02 '22 02:03 KaranTrivedi

It might be slightly off-topic, but running $ conda update ffmpeg from https://github.com/YuvalNirkin/fsgan/issues/174#issuecomment-2267555438 solved the ffprobe error (see stderr output for detail) in my case.

Just mentioning this in case anyone is looking for a solution : )

HyeJu99 avatar Dec 06 '24 07:12 HyeJu99