前言
在使用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