Ffmpeg-python: Add logger to ffmpeg call

Created on 9 Jul 2017  路  10Comments  路  Source: kkroening/ffmpeg-python

It would be very useful to have a logger for the ffmpeg call.
It would be easier to integrate in a system of loggers.

Is it possible to have in the future?

enhancement

All 10 comments

You can manually run stream.get_args() before stream.run() and to log the command line arguments.

@Depaulicious is right. But I like the idea of using a logger as well. I'll put it on my list of things to add.

Thanks for the suggestion

hello, is there anything update?

Also want to vote for this feature. Right now I am using this workaround:

import logging

out, err = ffmpeg.(...).run(quiet=True)
logging.info('ffmpeg stdout: %s', out.decode())
logging.info('ffmpeg stderr: %s', err.decode())

Problem is that all logs are buffered when the ffmpeg is running.
So the whole bunch of logs will have the same timestamp, and it may cause memory error if the ffmpeg logs are too large.

Going to be using the workaround suggested by @canyousayyes . Are there any updates to this?

This would be very useful

Also want to vote for this feature. Right now I am using this workaround:

import logging

out, err = ffmpeg.(...).run(quiet=True)
logging.info('ffmpeg stdout: %s', out.decode())
logging.info('ffmpeg stderr: %s', err.decode())

Problem is that all logs are buffered when the ffmpeg is running.
So the whole bunch of logs will have the same timestamp, and it may cause memory error if the ffmpeg logs are too large.

This does not work sadly. In my example I was missing a codec, so if we have quiet=False we see;

(thrianalysis) ben@ben-asus-strix /data/gym_ai/ThriWorkspace/workspace/ThriAnalysis (feature/refactor_speeduplogging) $ python tmp.py 
ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (crosstool-NG fa8859cb)
  configuration: --prefix=/home/ben/anaconda3/envs/thrianalysis --cc=/opt/conda/conda-bld/ffmpeg_1531088893642/_build_env/bin/x86_64-conda_cos6-linux-gnu-cc --disable-doc --enable-shared --enable-static --enable-zlib --enable-pic --enable-gpl --enable-version3 --disable-nonfree --enable-hardcoded-tables --enable-avresample --enable-libfreetype --disable-openssl --disable-gnutls --enable-libvpx --enable-pthreads --enable-libopus --enable-postproc --disable-libx264
  libavutil      56. 14.100 / 56. 14.100
  libavcodec     58. 18.100 / 58. 18.100
  libavformat    58. 12.100 / 58. 12.100
  libavdevice    58.  3.100 / 58.  3.100
  libavfilter     7. 16.100 /  7. 16.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  1.100 /  5.  1.100
  libswresample   3.  1.100 /  3.  1.100
  libpostproc    55.  1.100 / 55.  1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'tmp.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.35.100
  Duration: 00:00:06.93, start: 0.000000, bitrate: 316 kb/s
    Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 360x270 [SAR 1:1 DAR 4:3], 314 kb/s, 30 fps, 30 tbr, 15360 tbn, 30 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Unknown encoder 'h264'
Traceback (most recent call last):
  File "tmp.py", line 7, in <module>
    .run(quiet=False)
  File "/home/ben/anaconda3/envs/thrianalysis/lib/python3.7/site-packages/ffmpeg/_run.py", line 325, in run
    raise Error('ffmpeg', out, err)
ffmpeg._run.Error: ffmpeg error (see stderr output for detail)

Now if we try quiet=True

import ffmpeg
out, err = (
        ffmpeg #pylint: disable = no-member
            .input("tmp.mp4")
            .output("out.mp4", vcodec="h264")
            .overwrite_output()
            .run(quiet=True)
    )
print("here")
print(err)

Gives

(thrianalysis) ben@ben-asus-strix /data/gym_ai/ThriWorkspace/workspace/ThriAnalysis (feature/refactor_speeduplogging) $ python tmp.py 
Traceback (most recent call last):
  File "tmp.py", line 7, in <module>
    .run(quiet=True)
  File "/home/ben/anaconda3/envs/thrianalysis/lib/python3.7/site-packages/ffmpeg/_run.py", line 325, in run
    raise Error('ffmpeg', out, err)
ffmpeg._run.Error: ffmpeg error (see stderr output for detail)

With no standard out logged due to the exception, indeed even with quiet=True an exception is thrown. If we catch the exception, the err message is still not returned, so we cannot log it.

A workaround that does seem to work is:

    try:
        out, err = (
            ffmpeg #pylint: disable = no-member
                .input(input_filename)
                .output(output_filename, vcodec="h264")
                .overwrite_output()
                .run(quiet=True)
        )
    except ffmpeg._run.Error as exc:
        LOGGER.error("%s: captured stderror from ffmpeg: %s. \n\n Captured stdout from ffmpeg: %s",
                     exc, exc.stderr, exc.stdout, exc_info=True)
        raise

    LOGGER.info('ffmpeg stdout: %s', out.decode())
    LOGGER.info('ffmpeg stderr: %s', err.decode())

not sure it helps but found this code that has capture_stderr=True
try:
(
ffmpeg
.input(outputMovie,vcodec=_NIVIDIA_DECODER, hwaccel=_NIVIDIA_ACCELERATOR, hwaccel_output_format='cuda')
.output(render_file, **output_params)
.run(capture_stderr=True)
)
except ffmpeg.Error as ex:
print("FFMPEG: error converting video to images")
print(ex.stderr.decode('utf8'))
raise Exception("Failed transcode")
print("Running command: {}".format(command))

I also think this feature would be really helpful.
For those interested, I strongly recommend to look at this project: python-ffmpeg-video-streaming
It's not a really strong project yet, but you can look at this example.
This package uses python subprocess.popen (with stdout=PIPE), so then you can check fffmpeg output with subprocess.stdout.
I'm currently doing this to write a log file, and calling an API (via http) to report frontend users about the transcoding progress (on real time).

What I have not yet achieved, is to show to users friendly updates of transcoding process. Something like: _Input video loaded, transcoding video (profile # 1), transcoding video (profile # 2), ..., output video generated, end_.

But for now I could only show to users main ffmpg outputs (quite confusing) like these:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'inputs/base_video_short.mp4'
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 155 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'inputs/base_video_short.mp4':
Stream #1:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 155 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Stream #0:0 -> #1:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #1:1 (aac (native) -> aac (native))
Stream #0:0(und): Video: h264 (libx264), yuv420p, 640x360 [SAR 1:1 DAR 16:9], q=-1--1, 276 kb/s, 25 fps, 90k tbn, 25 tbc (default)
Stream #0:1(und): Audio: aac (LC), 44100 Hz, stereo, fltp, 128 kb/s (default)
Stream #1:0(und): Video: h264 (libx264), yuv420p, 854x480 [SAR 1280:1281 DAR 16:9], q=-1--1, 750 kb/s, 25 fps, 90k tbn, 25 tbc (default)
Stream #1:1(und): Audio: aac (LC), 44100 Hz, stereo, fltp, 192 kb/s (default)

hello, is there anything update with logging in ffmpeg-python?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

znorris picture znorris  路  4Comments

Baa14453 picture Baa14453  路  5Comments

Matttman picture Matttman  路  6Comments

monokal picture monokal  路  3Comments

Sangkwun picture Sangkwun  路  4Comments