我正在使用a previously asked question a few years ago中的代码,但是,我相信这已经过时了。尝试运行代码时,收到上面的错误。我还是一个Python新手,所以我无法从类似的问题中得到太多的澄清。有人知道为什么会这样吗?
import subprocess
def getLength(filename):
result = subprocess.Popen(["ffprobe", filename],
stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
return [x for x in result.stdout.readlines() if "Duration" in x]
print(getLength('bell.mp4'))
回溯
Traceback (most recent call last):
File "B:\Program Files\ffmpeg\bin\test3.py", line 7, in <module>
print(getLength('bell.mp4'))
File "B:\Program Files\ffmpeg\bin\test3.py", line 6, in getLength
return [x for x in result.stdout.readlines() if "Duration" in x]
File "B:\Program Files\ffmpeg\bin\test3.py", line 6, in <listcomp>
return [x for x in result.stdout.readlines() if "Duration" in x]
TypeError: a bytes-like object is required, not 'str'
默认情况下,
subprocess
返回stdout或stderr流的bytes
对象。这意味着您还需要在对这些对象的操作中使用bytes
对象。"Duration" in x
使用str
对象。使用字节文字(注意b
前缀):或者,如果您知道使用的编码(通常是区域设置默认值,但您可以为子进程set ^{} or more specific locale environment variables ),请先对数据进行解码:
另一种方法是告诉
subprocess.Popen()
通过将encoding
参数设置为合适的编解码器将数据解码为Unicode字符串:如果设置了
text=True
(Python 3.7及更高版本,在以前的版本中,此版本称为universal_newlines
),则还可以使用system default codec启用解码,与用于open()
调用的解码相同。在这种模式下,默认情况下管道是行缓冲的。就像错误者说的,“持续时间”是一个字符串。然而,X是一个类似字节的对象,因为
results.stdout.readlines()
将输出中的行读取为字节码而不是字符串。因此,将“Duration”存储在变量中,比如str_var,并使用
str_var.encode('utf-8')
将其编码为字节数组对象。请参阅[此][1]。
[1]:Best way to convert string to bytes in Python 3?
相关问题 更多 >
编程相关推荐