FFmpeg的log日志打印控制(命令行或Python)

前言

在使用FFmpeg处理音视频时,这个工具会默认自动在命令行窗口打印log日志信息。如下所示:
在这里插入图片描述
很多时候我们可能在实际应用中并不需要这个打印的信息,特别是在音视频批量任务处理时,过多的打印信息可能会刷屏或者造成输出堵塞。另外打印操作也是需要耗时的。于是我们考虑关掉它或者只打印关键信息。

思路

通过查看FFmpeg官网,可以看到,直接通过传参“-loglevel”可以控制log的输出。
在这里插入图片描述
可供选择的选项如下:

loglevel is a string or a number containing one of the following values:

‘quiet, -8’
Show nothing at all; be silent.

‘panic, 0’
Only show fatal errors which could lead the process to crash, such as an assertion failure. This is not currently used for anything.

‘fatal, 8’
Only show fatal errors. These are errors after which the process absolutely cannot continue.

‘error, 16’
Show all errors, including ones which can be recovered from.

‘warning, 24’
Show all warnings and errors. Any message related to possibly incorrect or unexpected events will be shown.

‘info, 32’
Show informative messages during processing. This is in addition to warnings and errors. This is the default value.

‘verbose, 40’
Same as info, except more verbose.

‘debug, 48’
Show everything, including debugging information.

‘trace, 56’

以上可选项中info是默认值,即打印所有informative messages。

命令行使用方式

使用命令行方式传参示例如下:

(1)带前缀

For example to enable repeated log output, add the level prefix, and set loglevel to warning:

ffmpeg -loglevel repeat+level+warning -i input output

其中两个前缀(flags prefix)的意义如下:
‘repeat’
Indicates that repeated log output should not be compressed to the first line and the “Last message repeated n times” line will be omitted.
重复的log输出不应被压缩到一行,且"Last message repeated n times"将被省略。

‘level’
Indicates that log output should add a [level] prefix to each message line. This can be used as an alternative to log coloring, e.g. when dumping the log to file.
log输出需要加上[level]前缀到每一条消息上。还能被用于控制log颜色。

Flags can also be used alone by adding a ’+’/’-’ prefix to set/reset a single flag without affecting other flags or changing loglevel. When setting both flags and loglevel, a ’+’ separator is expected between the last flags value and before loglevel.

(2)不带前缀

Another example that enables repeated log output without affecting current state of level prefix flag or loglevel:

ffmpeg [...] -loglevel +repeat

Python使用方式

以ffmpeg-python使用为例,在其官网的API列表中没有找到相关的资料。通过在其Github的issues上搜索找到了一些信息
在这里插入图片描述
直接上手测试:

def change_fps(source_video_path, output_file_path, target_fps = 24):
    source_video = ffmpeg.input(source_video_path)
    out = source_video.output(output_file_path, r=24)
    # out.run()#修改前
    out.global_args('-loglevel', 'warning').run()#增加log输出控制后,其中loglevel设为warning

测试效果符合预期,只输出warning级别的信息,不再有本文开头截图中的输出信息。

在这里插入图片描述

参考资料

[1] ffmpeg Documentation
[2] ffmpeg-python documentation »
[3] ffmpeg-python github
[4] ffmpeg-python Github issue: Add a way to pass arbitrary arguments to ffmpeg.run() #30

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TracelessLe

❀点个赞加个关注再走吧❀

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值