这让我疯了。我正在使用fabric通过SSH在远程服务器上执行一些视频处理任务。
我在远程计算机上调用了ffprobe,以便在我们输入此功能之前将信息返回到我将发送到该服务器的视频上。我试图解析有关持续时间的一些内容:从ffprobe的输出中,这样我就可以在几秒钟内得到视频的长度。这是函数的麻烦部分,我似乎无法将字符串转换为整数。
with cd("~/videos"):
command = "ffprobe "
command += videoNameOnly
output = run(command)
durationIndex = output.find("Duration:")
durationIndex = durationIndex + 10
duration = output[durationIndex:]
durationFrags = duration.split(":")
print "====================================="
print durationFrags[0].strip()
print durationFrags[1].strip()
print durationFrags[2].strip()
hours = int(durationFrags[0].strip())
其输出如下
00
00
52.90, start
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 712, in main
*args, **kwargs
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 298, in execute
multiprocessing
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 197, in _execute
return task.run(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 112, in run
return self.wrapped(*args, **kwargs)
File "/home/amyface/fabfile.py", line 26, in main
encode();
File "/home/amyface/fabfile.py", line 34, in encode
runJobs(jobs,cursor)
File "/home/amyface/fabfile.py", line 60, in runJobs
getMiddleFrame(sourceVideo,videoNameOnly,cursor)
File "/home/amyface/fabfile.py", line 96, in getMiddleFrame
hours = int(durationFrags[0].strip())
ValueError: invalid literal for int() with base 10: '\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m00'
Disconnecting from [I have removed the server name from here]
为什么它不会让我将“00”转换为整数?在“with base 10:”之后错误中的奇怪值是什么?
我对面料一无所知,但根据ValueError
,durationFrags[0].strip()
是'\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m00'
,而不是'00'
。这会产生设计用于终端的输出吗?那些看起来像ANSI转义码给我。
关键是:
ValueError: invalid literal for int() with base 10: '\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m\x1b[0m\x1b[0;39m00'
如上所示here这些是ANSI终端代码。在字符串中,\x1b
是ESC
字符
您可以使用上面链接的答案中显示的正则表达式来删除不需要的代码并提取最后一个'00'
执行的命令(ffprobe
)正在编写terminal color codes。
尝试通过设置AV_LOG_FORCE_NOCOLOR
环境变量来告诉该程序不要使用它们